Skip to content
Advertisement

Cannot add child to XML node in PHP

I am trying to add a child note to an XML file using PHP. I have used the same method about 5 times before and each one works as expected, however this method is using a different XML structure on a new file (which I think might be part of the problem).

The method causing me the problem is this:

JavaScript

The XML file looks like this:

JavaScript

And getRoles() looks like this:

JavaScript

I’m at a loss here, I have no idea why this is different from the other adding functions (see below).


Should you need it, here is an example of what my other methods look like:

JavaScript

and the getXML() method:

JavaScript

Advertisement

Answer

Turned out that the problem was caused by the fact that <project> element is nested within the XML document. You can use descendant-or-self axis (// for the abbreviated syntax) to get nested element like so :

JavaScript

or specify the complete path :

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement