PHP:查找孩子并更新同一父母下的另一个孩子

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 />";
}

You Can do by using the it parentNode attribute and then loop on all childs. As follow:

$data = '<map><competency><level>5</level><skill name="Maths"><skillinfo>"some value"</skillinfo></skill></competency></map>';
$dom = new DOMDocument;
$dom->loadXML($data);
$xpath = new DomXpath($dom);
foreach ($xpath->query('//skill[@name="Maths"]') as $item) {
        foreach($item->parentNode->childNodes as $node)
                if ($node->tagName == 'level')
                        $node->nodeValue = @WhatEverValueYouWant@;
}
$xml_string = $dom->saveXML();
echo $xml_string;

You can fetch the level node directly and change its text content.

Xpath Expression:

  • Fetch competency elements inside the map documetn element...
    /map/competency
  • ... that have a skill element with with a name attribute "Maths" ...
    /map/competency[skill/@name="Maths"]
  • ... fetch the level child elements
    /map/competency[skill/@name="Maths"]/level

Example:

$data = <<<'XML'
<map>
<competency>
  <level>5</level>
  <skill name="Maths"> 
     <skillinfo> "some value" </skillinfo>
  </skill>
</competency>
</map>
XML;

$document = new DOMDocument;
$document->loadXML($data);
$xpath = new DomXpath($document);

$expression = '/map/competency[skill/@name="Maths"]/level';

foreach ($xpath->evaluate($expression) as $level) {
  $level->textContent += 1;
}

echo $document->saveXML();

Output:

<?xml version="1.0"?>
<map>
<competency>
  <level>6</level>
  <skill name="Maths"> 
     <skillinfo> "some value" </skillinfo>
  </skill>
</competency>
</map>

Output Values from DOM:

You can use Xpath to fetch scalar values for output as well.

foreach ($xpath->evaluate('/map/competency') as $competency) {
  echo $xpath->evaluate('string(skill/@name)', $competency), ': ';
  echo $xpath->evaluate('string(level)', $competency), "
";
}

Output:

Maths: 5