tvks Posted December 1, 2006 Share Posted December 1, 2006 Hi,I have a simple PHP code $dom = new DomDocument;$dom->Load('test.xml');$xpath = new DOMXpath($dom);$node = $xpath->query('/contacts')->item(0);echo $node->nodeName . "\n";$GLOBALS['dom']->removeChild($GLOBALS['dom']->lastChild);$dom->save('test.xml'); and an XML file named 'test.xml' <?xml version="1.0" encoding="ISO-8859-1"?> <contacts> <entry uri="sip:vinayg@company.org"> <name>Vinay</name> <sip> sip:vinayg@company.org </sip> <Number> 9885821530 </Number> </entry> <entry uri="sip:vivek@company.org"> <name>Vivek</name> <sip> sip:vivek@company.org </sip> <Number> 9885821530 </Number> </entry> </contacts> My intention of this program is to delete only the entry tag with uri as 'sip:vivek@company.org' and all its child nodes (name, sip and Number). When I execute the above code the whole file is erased and there is no error or warning. Where am I wrong ?I would be happy for any help in correcting my code or redoing it as I am new to PHP and XML.My desired output of test.xml after execution is: <?xml version="1.0" encoding="ISO-8859-1"?> <contacts> <entry uri="sip:vinayg@company.org"> <name>Vinay</name> <sip> sip:vinayg@company.org </sip> <Number> 9885821530 </Number> </entry> </contacts> Thanx in advance.Regards,tvks Link to comment Share on other sites More sharing options...
boen_robot Posted December 1, 2006 Share Posted December 1, 2006 Try something like this: $dom = new DomDocument;$dom->Load('test.xml');$xpath = new DOMXpath($dom);$node = $xpath->query("//*[@uri='sip:vivek@company.org'");echo $node->nodeName . "\n";$dom->removeChild($node);$dom->save('test.xml'); If I'm guessing right, by selecting $GLOBALS['dom']->lastChild you've actually been removing the last child of root of the complete document, which as it so happens is the root element and all of it's content. Link to comment Share on other sites More sharing options...
kvnmck18 Posted December 1, 2006 Share Posted December 1, 2006 Err that PHP5...you know it, boen_robot Link to comment Share on other sites More sharing options...
tvks Posted December 5, 2006 Author Share Posted December 5, 2006 Hi Robot,My compiler is throwing me an error on execution of your program. Warning: DOMXPath::query() [function.DOMXPath-query]: Invalid predicate in try_me.php on line 5Warning: DOMNode::removeChild() expects parameter 1 to be DOMNode, boolean given in try_me.php on line 7 I am new to php and am hence dumb folded. Kindly help me.With regards,tvks Link to comment Share on other sites More sharing options...
boen_robot Posted December 5, 2006 Share Posted December 5, 2006 I am much of a noob with this myself .For the first error, I had a typo in the query. I missed a "]" that would close the predicate: $node = $xpath->query("//*[@uri='sip:vivek@company.org']"); and for the second error... maybe looping and selecting the item() would do: while($i<=$node->length) {$dom->removeChild($node->item($i)); $i++; } Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now