Retrieving a subset of XML nodes with PHP

前端 未结 5 1723
无人及你
无人及你 2020-12-20 00:48

Using PHP, how do I get an entire subset of nodes from an XML document? I can retrieve something like:


5条回答
  •  长情又很酷
    2020-12-20 01:15

    The answer turned out to be a combination of the xpath suggestion and outputting with asXML().

    Using the example given by Josh Davis:

    $people = simplexml_load_string(
          
            
              
                Jane Doe
                21
              
              
                John Smith
                34
              
            '
        );
    
        // get all  nodes
        $nodes = $people->xpath('/people/certain');
    
        foreach ( $nodes as $node ) {
          $result .= $node->asXML()."\n";
        }
        echo $result;
    

提交回复
热议问题