从XML文档中查找特定节点

时间:2012-01-02 18:51:05

标签: php xml

我有像

这样的XML
<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>

我想用php回显skype值。我该怎么做。我编写了如下代码,但它无法正常工作

<?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;
       }
   }
}
?>

2 个答案:

答案 0 :(得分:3)

很抱歉迟到的回复。我刚刚根据你发布的XML尝试了这个代码,但我认为你的XML包含标签Person的许多元素。根据{{​​3}}问题中的建议使用SimpleXML要简单得多。

<?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;
    }
}
?>

这适用于这样的XML:

<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>

但是,如果您想使用原始XML(只有一个人标记)尝试此操作,则可以使用:

<?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;
}
?>

答案 1 :(得分:2)

尝试使用xpath查询:

<?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;
    }

}

?>