php simple xml how to read multiple nodes with different child node levels

南楼画角 提交于 2019-12-10 20:49:04

问题


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

回答1:


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



回答2:


check this

$xml_file = 'file.xml';
$xmlobj = simplexml_load_file($xml_file);
echo $xmlobj->getName() . 'start_info<br />';
foreach($xmlobj->children() as $childs) {
  echo $childs->getName(). ': '. '<br />';
  if($childs->count()>1) {
    foreach($childs as $child) {
     echo $child->getName(). ': '. $child. '<br />';
    }
  }
}


来源:https://stackoverflow.com/questions/6611702/php-simple-xml-how-to-read-multiple-nodes-with-different-child-node-levels

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!