从XML文档中查找特定节点

I have an XML like

<Person>
  <firstName>pradeep</firstName>
  <lastName>jain</lastName>
  <address>
    <doorNumber>287</doorNumber>
    <street>2nd block</street>
    <city>bangalore</city>
  </address>
  <phoneNums type="mobile">9980572765</phoneNums>
  <phoneNums type="landline">080 42056434</phoneNums>
  <phoneNums type="skype">123456</phoneNums>
</Person>

I want to echo the skype value using php. How do i do it. I wrote code like below but its not working

<?php
$doc = new DOMDocument();

if ($doc->load('new.xml')) 
{
   $userInfo = $doc->getElementsByTagName('Person');

   foreach($userInfo as $row)
   {
       $phoneInfo = $row->getElementsByTagName("phoneNums");

       foreach($phoneInfo as $row2)
       {
            // get the value from the first child
            $work = $row2->getElementsByTagName("mobile")->item(0)->nodeValue;
            $home = $row2->getElementsByTagName("landline")->item(0)->nodeValue;
            echo $work;
       }
   }
}
?>

Sorry for the late reply. I just tried this code based on the XML you posted, with the slight difference that I assumed your XML contains many elements of the tag Person. It is much simpler using SimpleXML as suggested in this question.

<?php
$xml = simplexml_load_file('path\to\doc.xml');
// With the following line you get all the Person tags
$people = $xml->Person;
foreach($people as $person) {
    // For each person you get all the phoneNums tags
    $phoneNumbers = $person->phoneNums;
    foreach($phoneNumbers as $key => $value) {
        $attributes = $value->attributes();
        // We get all of the attributes, and select the one on index 0 -the ONLY attribute in this given case
        if ($attributes[0]=="skype")
            echo $value;
    }
}
?>

This works for an XML like this:

<myXml>
<Person>
  <firstName>pradeep</firstName>
  <lastName>jain</lastName>
  <address>
    <doorNumber>287</doorNumber>
    <street>2nd block</street>
    <city>bangalore</city>
  </address>
  <phoneNums type="mobile">9980572765</phoneNums>
  <phoneNums type="landline">080 42056434</phoneNums>
  <phoneNums type="skype">123456</phoneNums>
</Person>
<Person>
  <firstName>pradeep</firstName>
  <lastName>jain</lastName>
  <address>
    <doorNumber>287</doorNumber>
    <street>2nd block</street>
    <city>bangalore</city>
  </address>
  <phoneNums type="mobile">1</phoneNums>
  <phoneNums type="landline">2</phoneNums>
  <phoneNums type="skype">3</phoneNums>
</Person>
</myXml>

However, if you want to try this with your original XML (with only one person tag), this works:

<?php
// The following loads the ROOT into $xml (in your case, the Person tag is the root)
$xml = simplexml_load_file('path\to\doc.xml');
// Then we get all its children (firstname, lastname, address, etc)
$children = $xml->children();
// Of all its children, we select the phoneNums tags and then iterate
$phoneNumbers = $children->phoneNums;
foreach($phoneNumbers as $key => $value) {
    $attributes = $value->attributes();
    // We get all of the attributes, and select the one on index 0 -the ONLY attribute in this given case
    if ($attributes[0]=="skype")
        echo $value;
}
?>

Try using an xpath query:

<?php

$doc = new DOMDocument();

if ($doc->load('xpath.xml')) {

    $xpath = new DOMXPath($doc);
    $query = '/Person/phoneNums[@type="skype"]';
    $results = $xpath->query($query);

    foreach ($results as $result){ // query may have more than one result
        echo $result->nodeValue;
    }

}

?>