tags:

views:

25

answers:

1

So I have an XML file (XML export of a MS Word file). What I am just trying to do is to replace these two lines:

<w:t>Meno:</w:t>

And:

<w:t>Priezvisko:</w:t>

This is a longer XML excerpt:

<w:p w:rsidR="00CF175F" w:rsidRDefault="00CF175F">
−
<w:r>
<w:t>Meno:</w:t>
</w:r>
</w:p>
−
<w:p w:rsidR="00CF175F" w:rsidRDefault="00CF175F">
−
<w:r>
<w:t>Priezvisko:</w:t>
</w:r>
</w:p>

I am doing it like this:

$xml = file_get_contents('file.xml');

$doc = new DOMDocument();
$doc->loadXML($xml);
$doc->preserveWhiteSpace = false;

$wts = $doc->getElementsByTagNameNS('http://schemas.openxmlformats.org/wordprocessingml/2006/main','t');

foreach ($wts as $wt) {

 echo 1;

 //if ('Meno:' === substr($wt->nodevalue, 0, 5)) {
  //echo 1;
 //}

}

That script echoes nothing. Why does the getElementsByTagName not work? There are tens of w:t tags in the XML.

+1  A: 

I don't know php, but almost certainly the problem is that the xml DOM thinks that "w" refers to a namespace that you haven't yet defined. Instead of searching for a "tag" called "w:t", you need to search for a tag named "t" in the namespace that "w" refers to.

For a better explanation of xml namespaces and how to declare them (using "xmlns" in your document, see here.

Rob Levine
Thanks now it works. Check my updated answer :)
Richard Knop