PHP简单的XML如何读取不同的子节点级别的多个节点 [英] php simple xml how to read multiple nodes with different child node levels

查看:521
本文介绍了PHP简单的XML如何读取不同的子节点级别的多个节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有了不同的命名节点,多层次的子节点的XML文件(各节点之间是不同的。)我应该如何访问数据?这将需要很多嵌套的for循环?

下面是XML code的样本:

 < start_info>
          &所述;信息的tabindex =1>
                  &所述; infonumber→1&下; / infonumber>
                  <&树木GT;绿色< /棵>
           < /信息>
       < / start_info>          <人与GT;
                < PE>
                    < people_ages>
                       &所述;范围号码=1>
                          <年龄值=1> 1 LT; /年龄>
                          <年龄值=2> 2'; /年龄>
                        < /范围和GT;
                    < / people_ages>
                < / PE>
          < /人>

下面是我的code迄今:

  $ XML =使用simplexml_load_file(file.xml);回声$ XML的>的getName()。 start_info;的foreach($ XML的>儿童()为$子)
  {
  回声$儿童为>的getName()。 :。 $孩子。 < BR />中;
  }


解决方案

下面是一些例子code,我希望可以为您指出正确的方向。从本质上讲,它的走在 DOM文档 的呼应元素的名称和值。注意,元件之间的空白是显著,所以对于演示的目的,XML被压实。您可能会发现从一个文件类似的问题加载,所以如果你没有得到预期的输出可能需要剥离空白节点。

您可以替换 //根/ * 用不同的的XPath 例如 //人如果你只是想在<&人GT; 元素

 < PHP
    $ XML =<<< XML
    &LT;根和GT;&LT; start_info&GT;&LT;信息tabindex=\"1\"><infonumber>1</infonumber><trees>green</trees></info></start_info>
    &LT;人&GT;&LT; PE&GT;&LT; people_ages&GT;&LT;量程数=1&GT;&LT;年龄值=1&GT; 1&LT; /年龄&GT;&LT;年龄值=2&GT; 2&LT; /年龄&GT;&LT; /范围和GT;&LT; / people_ages&GT;&LT; / PE&GT;&LT; /人&GT;
    &LT; /根&GT;
    XML;    $ DOM =新的DOMDocument();
    $ dom-&GT;恢复= TRUE;
    $ dom-&GT;的loadXML($ XML);
    $ = XPath的新DOMXPath($ DOM);
    $节点列表= $ xpath-&GT;查询('//根/ *');
    的foreach($节点列表为$节点){
        回声\\ n $&于节点GT;变量名;
        的getData($节点);
    }    函数的getData($节点){
        的foreach($&于节点GT;子节点为$子){            如果($儿童为&GT;的nodeType == XML_ELEMENT_NODE){
                回波($儿童为&GT;的tagName ==='''?'\\ n)。$儿童为&GT;标签名;
            }            如果($儿童为&GT;的nodeType == XML_TEXT_NODE){
                回声 - &GT;'$&儿童为GT;的nodeValue。
            }            如果($儿童期&GT; hasChildNodes()){
                的getData($子女); //递归调用
            }
        }
    }
?&GT;

I have an xml file that has different named nodes and multi level child nodes (that are different between each node.) How should I access the data? Will it require many nested for loops?

Here is a sample of the xml code:

       <start_info>
          <info tabindex="1">
                  <infonumber>1</infonumber>
                  <trees>green</trees>
           </info>
       </start_info>

          <people>
                <pe>
                    <people_ages>
                       <range number="1">
                          <age value="1">1</age>
                          <age value="2">2</age>
                        </range>
                    </people_ages>
                </pe>
          </people>

Here is my code so far:

$xml = simplexml_load_file("file.xml");

echo $xml->getName() . "start_info";

foreach($xml->children() as $child)
  {
  echo $child->getName() . ": " . $child . "<br />";
  }

解决方案

Here is some example code that I hope can point you in the right direction. Essentially, it is walking the DOMDocument echoing the element name and values. Note that the whitespace between the elements is significant, so for the purposes of the demo, the XML is compacted. You may find a similar issue loading from a file, so if you are not getting the expected output you might need to strip whitespace nodes.

You could replace the //root/* with a different XPath for example //people if you only wanted the <people> elements.

<?php
    $xml = <<<XML
    <root><start_info><info tabindex="1"><infonumber>1</infonumber><trees>green</trees></info></start_info>
    <people><pe><people_ages><range number="1"><age value="1">1</age><age value="2">2</age></range></people_ages></pe></people>
    </root>
    XML;

    $dom = new DOMDocument();
    $dom->recover = true;
    $dom->loadXML($xml);
    $xpath = new DOMXPath($dom);
    $nodelist = $xpath->query('//root/*');
    foreach ($nodelist as $node) {
        echo "\n$node->tagName";
        getData($node);
    }

    function getData($node) {
        foreach ($node->childNodes as $child) {

            if ($child->nodeType == XML_ELEMENT_NODE) {
                echo ($child->tagName === '' ? '' : "\n").$child->tagName;
            }

            if ($child->nodeType == XML_TEXT_NODE) {
                echo '->'.$child->nodeValue;
            }

            if ($child->hasChildNodes()) {
                getData($child); // recursive call
            }
        }
    }
?>

这篇关于PHP简单的XML如何读取不同的子节点级别的多个节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆