2

The below is my sample xml data

 <?xml version="1.0"?>
<catalog>
   <book>
      <book NAME="ci_id">
        <PVAL><![CDATA[55190]]></PVAL>
      </book>
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
   </book>
   <book>
      <book NAME="ci_id">
        <PVAL><![CDATA[55191]]></PVAL>
      </book>
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
   </book>
   <book>
      <book NAME="ci_id">
        <PVAL><![CDATA[55192]]></PVAL>
      </book>
      <author>Corets, Eva</author>
      <title>Maeve Ascendant</title>
   </book>

   <book>
      <book NAME="ci_id">
        <PVAL><![CDATA[55194]]></PVAL>
      </book>
      <author>Corets, Eva</author>
      <title>The Sundered Grail</title>
   </book> 
</catalog>

my requirement is i have to remove parent node(i.e, < book > to < /book >) based on the pval id condition( ex: here i am deleting pval = 55192) and i need to update the new node.

In the below code i have successfully removed the parent node based on the pval, but i could not add the new node (i.e, its is a string value). Please guide me.

$xmlFileToLoad   =  'client_temp/endeca_xml_search_feed/product/testing.xml';
      $xmlFileToSave   =  'client_temp/endeca_xml_search_feed/product/testing-modified.xml';

      $cdata_text = '
      <book>
         <book NAME="ci_id">
            <PVAL><![CDATA[78965]]></PVAL>
         </book>
         <author>testing</author>
         <title>Md kaif khan</title>
         <genre>kkkk</genre>
         <price>5.95</price>
         <publish_date>2016-09-10</publish_date>
         <description>Just i am testing.</description>
      </book>';

      $dom = new DOMDocument();
      $dom->load($xmlFileToLoad);
      $xpath = new DOMXPath($dom);

    function findStopPointByName($xml, $query) {
      $upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZAZSCZCÓL";
      $lower = "abcdefghijklmnopqrstuvwxyzazscznól";
      $arg_query    = "translate(text(), '$upper', '$lower')";
      $q = "//book[book/PVAL[contains($arg_query, '$query')]]" ."\n";
      return $xml->query($q);
    }

    foreach(findStopPointByName($xpath,'55192') as $node)
    {
      $node->parentNode->removeChild($node);
       $node->appendChild( 
     $doc->createTextNode( $cdata_text ) );  
    }

    $dom->save($xmlFileToSave);

Please tell me how to add node (i.e,string data. here $cdata_text).

1 Answer 1

1
   foreach(findStopPointByName($xpath,'55192') as $node)
    {
      // Save parent - after removing node we will use it
      $parent = $node->parentNode;
      $parent->removeChild($node);

      // Load XML from text. You cann't append text, only node
      $data = new DOMDocument();
      $data->loadXML($cdata_text);

      // Import all new XML to our one and append to saved parent
      $ndata =  $dom->importNode($data->documentElement, true);   
      $parent->appendChild($ndata); 
    }
Sign up to request clarification or add additional context in comments.

7 Comments

Glad to help. Good luck!
Hi @splash, I need ur help. i am trying to add new node to the root node(i.e, <book>...</book>). Using the following code. $newNode = new DOMDocument(); $newNode->loadXML($trimData); $newNode->formatOutput = true; // Import all new XML to our one and append to saved parent $ndata = $dom->importNode($newNode->documentElement, true); $dom->documentElement->appendChild($ndata); Can you please help me on this
thanks for ur quick response. while i am executing the above code, i get the Fatal error: Call to a member function appendChild() on a non-object Can you please tell me how to resolve this
change last line $dom->appendChild($ndata);;
Hi @Slash, the code is working fine, but the performance is very poor. Example: It takes 55 seconds to complete for 10 nodes and the server load it too high, it took almost 99 percent.(i.,e <book>). Now i am not using findStopPointByName function. instead i am using $dom->getElementById(here record id. that is <book id=1>....</book>); Can you please give me suggestion, that how to improve the performance.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.