I have find out how to replace specific node values with PHP string using PHP DOM, now I need print these XML values into form fields. For replacing I’m using this PHP code, and maybe somehow I can modify this code for printing specific values, because this XML file is very complex
$file = "../word/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." ".$surname."");
$wt->appendChild($wtText);
$ori4->replaceChild($wt,$ori5);
$xml->save("../word/document.xml");
I need to reach same value ($ori5) and print it to form field, but I’m not sure how to do that.
XML file can be found here:
<w:document mc:Ignorable="w14 wp14">
<w:body>
<w:tbl>
<w:tr w:rsidR="000171B5" w:rsidTr="00272F6E">
<w:tc>
<w:p w:rsidR="000171B5" w:rsidRPr="00075BEA" w:rsidRDefault="000171B5" w:rsidP="000171B5">
<w:r w:rsidRPr="00075BEA">
<w:t>Vardas pavarde</w:t>
</w:r>
</w:p>
</w:tc>
</w:tr>
</w:tbl>
</w:body>
</w:document>
Sorry I was not able to get to this last night, but I was able to get a copy of your XML to work with. In the future, please only provide a sample of the XML you are using. Especially since the one you provided was unformatted. It took longer for me to format up to where I needed than it did for me to answer the question. And most people would just leave it rather than digging through all that. Here’s the formatted XML relating to the question after having been sampled.
Using the path you provided (item(0), item(1), etc…) I came up with the value
Vardas pavarde. Which is in theW:telement above. The steps I took in the XML are below.The easier way of going about getting this value is with XPATH. You can read up on it here if you have any more questions about it. This is a vastly covered subject so i’m just going to give you the XPATH I think you are looking for. If its not exactly what you are looking for you’ll have to tweak it, just use that link its pretty straight forward.
So, Add the following after you have loaded your DOMDocument.
Since XPATH returns an array of results you’ll have to dump it to find the specific index you are looking for. But in this instance it should be
$elements[1]since there is only one other element that matches that path before the one you are looking for. So…Also, you might want to consider using SimpleXML for reading XML. DOM is good if you are editing it, which you appear to be doing here, but if you need to read it, SimpleXML is a little more straight forward. Here’s a link to using XPATH with SimpleXML. Hope this helps!