Print XML node using XPath in PHP

hanyu

New Member
I'm trying to print complex XML's node values using XPath, I have attached an image for helping to see the path which I need to reach (red underline).
PLp2P.png
Original XML file can be found hereI was trying something like that: \[code\]<?php$xml = simplexml_load_file('document.xml'); echo "<strong>Using direct method...</strong><br />"; $names = $xml->xpath('/w:document/w:body/w:tbl[0]/w:tr[1]/w:tc[0]/w:p/w:r/w:t'); foreach($names as $name) { echo "Found $name<br />"; }?>\[/code\]This method I am using to replace this node:\[code\] $file = "document.xml"; $fp = fopen($file, "rb") or die("error"); $str = fread($fp, filesize($file)); $xml = new DOMDocument(); $xml->formatOutput = true; $xml->preserveWhiteSpace = false; $xml->loadXML($str) or die("Error"); $root = $xml->documentElement; $fnode = $root->childNodes->item(0); $ori = $fnode->childNodes->item(1); $ori1 = $ori->childNodes->item(3); $ori2 = $ori1->childNodes->item(1); $ori3 = $ori2->childNodes->item(1); $ori4 = $ori3->childNodes->item(1); $ori5 = $ori4->childNodes->item(1); $wt = $xml->createElement("w:t"); $wtText = $xml->createTextNode("".$name." ".$item.""); $wt->appendChild($wtText); $ori4->replaceChild($wt,$ori5); $xml->save("document.xml");\[/code\]
 
Back
Top