2

I need to pick one tag from xml file and insert another tag before this tag. I'm doing this with method insertBefore in DOM, but the problem is, that if I want pick the tag before I want to add the another tag by method getElementById, it doesn't work.
It writes "using non-object". This is how the tag looks:

<item id="Flow_0" href="Flow_0.html" media-type="application/xhtml+xml"/>

Somewhere I read that it must look like this, but I can't edit all files:

<item xml:id="Flow_0" href="Flow_0.html" media-type="application/xhtml+xml"/>

Have you got any idea how to do that?

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Lukáš Jelič
  • 529
  • 3
  • 8
  • 22
  • *(related)* [Simplify PHP DOM XMP Parsing](http://stackoverflow.com/questions/3405117/simplify-php-dom-xml-parsing-how/3405651#3405651) has an explanation why `getElementById` doesnt work. In a nutshell: when there is no DTD or Schema that [defines id to be an IDAttribute](http://www.php.net/manual/en/domattr.isid.php), its not an ID in the ID sense but just an attribute. – Gordon Sep 24 '11 at 15:13

1 Answers1

2

A common workaround is to use XPath to get the element.

$item = $xpath->query('//item[@id="Flow_0"]')->item(0);
salathe
  • 51,324
  • 12
  • 104
  • 132