PHP编程实战14-9






    
        snoopy
        brown
        beagel cross
    
    
        teddy
        brown
        tabby
    
    
        jade
        black
        lab cross
    

THE_XML;

$xml_object = simplexml_load_string($xml);

$names = $xml_object->xpath("*/name");
foreach ($names as $element) {
    $parent = $element->xpath("..");
    $type = $parent[0]->getName();
    echo "$element ($type)
"; } ?>

使用XPath的*/name匹配元素,而不考虑它是否被包含在元素中.
使用查询..获取当前SimpleXMLElement的根.

你可能感兴趣的:(PHP编程实战14-9)