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).