简体   繁体   中英

Remove Nodes from Xml not working in PHP

I can't seem to figure this out...

I have this Xml:

<?xml version="1.0" encoding="utf-8"?>
<report>
   <report-name name="SEARCH_QUERY_PERFORMANCE_REPORT"/>
   <date-range date="Jan 1, 2016-Jan 31, 2016"/>
   <table>
      <columns>
          <column name="campaignID" display="Campaign ID"/>
          <column name="campaign" display="Campaign"/>
          <column name="adGroupID" display="Ad group ID"/>
          <column name="campaignState" display="Campaign state"/>
          <column name="keyword" display="Keyword"/>
          <column name="searchTerm" display="Search term"/>
          <column name="convertedClicks" display="Converted clicks"/>
          <column name="impressions" display="Impressions"/>
          <column name="clicks" display="Clicks"/>
          <column name="ctr" display="CTR"/>
          <column name="avgCPC" display="Avg. CPC"/>
          <column name="avgPosition" display="Avg. position"/>
          <column name="cost" display="Cost"/>
          <column name="device" display="Device"/>
      </columns>
      <row device="Mobile devices with full browsers" cost="3940000" avgPosition="2.0" avgCPC="3940000" ctr="100.00%" clicks="1" impressions="1" convertedClicks="0" searchTerm="purple jeep near elgin for sale" keyword="jeepdealership" campaignState="enabled" adGroupID="7751248218" campaign="Zeigler CDJ_Dealer Campaign" campaignID="134270778"/>
      <row device="Mobile devices with full browsers" cost="3930000" avgPosition="1.0" avgCPC="3930000" ctr="100.00%" clicks="1" impressions="1" convertedClicks="0" searchTerm="jeep wrangler rubicon 2015" keyword="jeepdealership" campaignState="enabled" adGroupID="7751248218" campaign="Zeigler CDJ_Dealer Campaign" campaignID="134270778"/>
      <row device="Mobile devices with full browsers" cost="2010000" avgPosition="3.0" avgCPC="2010000" ctr="100.00%" clicks="1" impressions="1" convertedClicks="0" searchTerm="altitute jeep cherokee" keyword="2015 jeep Cherokee" campaignState="enabled" adGroupID="18188246418" campaign="Zeigler CDJ_Targeted Campaign" campaignID="134270898"/>
      <row device="Mobile devices with full browsers" cost="3990000" avgPosition="1.0" avgCPC="3990000" ctr="100.00%" clicks="1" impressions="1" convertedClicks="0" searchTerm="2012 jeep cherokee" keyword="2015 +Jeep +cherokee" campaignState="enabled" adGroupID="18188246418" campaign="Zeigler CDJ_Targeted Campaign" campaignID="134270898"/>
  </table>

I need to remove some of the nodes (and their children) in the Xml. I am using the following code:

$doc = new DOMDocument;
$doc->load('qreport.xml');
$node = $doc->documentElement;
// Retrieve the sub-nodes and remove from xml
$subnodes = $node->getElementsByTagName('columns')->item(0);
$remnodes = $node->removeChild($subnodes);
echo $doc->saveXML();

So, when put this in for the node names, they are deleted. For instance, this code works and I can delete table and its children, I can delete report-name , and date-range .

But I cannot delete columns and its children.

Unclear what I am doing wrong.

$subnodes = $node->getElementsByTagName('columns')->item(0);
$remnodes = $node->removeChild($subnodes);

You have to invoke ->removeChild($somenode) on the direct parent of $somenode. The element <columns> is a child of <table> , not of the documentElement (==$node) <report> .

Try

$subnodes = $node->getElementsByTagName('columns');
if ( $subnodes && $subnodes->length > 0 ) {
    $subnodes = $subnodes->item(0);
    $remnodes = $subnodes->parentNode->removeChild($subnodes);
}

instead.
see also: http://docs.php.net/class.domnode#domnode.props.parentnode

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM