Can someone please help me to get this working? I'm trying to find a child in the below XML and once it is found, I need to update another child within the same parent node. It can be either in simpleXML or XDOM - using both I managed to work out how to find the item but I can't figure out how to update another child within the same parent.
So in the below example, I want to first find the skill 'Maths' and update the 'level' to some other number.
XML:
<map>
<competency>
<level>5</level>
<skill name="Maths">
<skillinfo> "some value" </skillinfo>
</skill>
<competency>
</map>
So far I've:
$dom = new DOMDocument;
$dom->loadXML($data);
$xpath = new DomXpath($dom);
I don't really need a for loop as the skill only appears once in the XML, so more like IF this skill exists then update the level.
foreach ($xpath->query('//skill[@name="Maths"]') as $item) {
echo $item->nodeValue;
// how to update the child 'Level'?
}
Once the change is made I will then need to append the changes to the XML file.
2nd part of the question:
How to output everything inside an XML file using DOM.
Below works but only shows child VALUES under the parent however what I'd like to ouput is the name value which in this example is .
$dom = new DOMDocument;
$dom->loadXML($data);
$xpath = new DomXpath($dom);
$x = $dom->documentElement;
foreach ($x->childNodes AS $item){
print $item->nodeName . " = " . $item->nodeValue . "<br />";
}