代码之家  ›  专栏  ›  技术社区  ›  mattstuehler

PHP5:迭代子节点列表(相当于child_nodes())?

  •  2
  • mattstuehler  · 技术社区  · 17 年前

    我有一个XML文档,看起来像这样:

    <root>
        <profile>
            <childA>
            <childB>
            <childC>
        <profile>
        <blah>
        <blah>
        <foo>
        <bar>
    <root>
    

    $doc = new DomDocument();
    $doc->loadXML(file_get_contents("php://input"));
    $profile_node = $doc->getElementsByTagName("profile")->item(0);
    

    在PHP4中,我想你会做这样的事情:

    $childnodes = $profile_node->child_nodes();
    foreach ($childnodes as $node) {
        // do something with this node
    }
    

    2 回复  |  直到 13 年前
        1
  •  6
  •   Zed    17 年前

    根据 php manual

    foreach ($profile_node->childNodes as $node) {
        // do something with this node
    }
    
        2
  •  1
  •   Scharrels    17 年前

    simplexml 包裹:

    $xml = simplexml_load_file('php://input');
    $childnodes = $xml->xpath("//profile/child::*");
    foreach($childnodes as $node){
      // do something
    }
    

    dom_import_simplexml() .