doubi7739 2012-01-02 18:51
浏览 309
已采纳

从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;
       }
   }
}
?>
  • 写回答

2条回答 默认 最新

  • dsfgdsjfd78773 2012-01-03 01:15
    关注

    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;
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码