如何获取每个元素(父子子级)名称及其属性-PHP -XML

时间:2015-05-21 06:32:55

标签: php xml

检查我的代码,接下来该做什么:

http://3v4l.org/KAYVr

相关代码:

<?php
$file = <<<XML
<?xml version="1.0"?>
<?xml-stylesheet href="catalog.xsl" type="text/xsl"?>
<!DOCTYPE catalog SYSTEM "catalog.dtd">
<catalog>
   <product description="Cardigan Sweater" id="123" value="" product_image="cardigan.jpg">
      <catalog_item gender="Men's">
         <size description="Medium">
            <color_swatch image="red_cardigan.jpg" id="color" value="Red"/>
             <color_swatch image="burgundy_cardigan.jpg" id="color" value="burgundy"/>
         </size>
         <size description="Large">
           <color_swatch image="red_cardigan.jpg" id="color" value="Red"/>
            <color_swatch image="burgundy_cardigan.jpg" id="color" value="burgundy"/>
         </size>
      </catalog_item>
   </product>
</catalog>
XML;
echo ("<pre>");

  $xml=simplexml_load_string($file) or die("Error: Cannot create object");

//ROOT:

foreach($xml as $root=>$value) {

          echo $root. '<br/>' ;


//check child 

          foreach ($value as $secondtag=>$value1) {
                 print_r($secondtag);echo "<br/>";
                if($value1->children() != null) {
                $subchild = $value1->children();
print_r($subchild);
               }
}

echo ("</pre>");


}
?>

我想打印它 打印根元素     print(所有root属性,如果有的话) print子元素(如果有的话)     print(所有子属性,如果有的话) 打印子子(如果有的话)     print(所有子子属性,如果有的话)

这应该是文件的每个根元素的递归,它应该系统地打印每个元素的子元素及其整个xml的属性

预期产出

catalog
   product 
 description="Cardigan Sweater" 
 id="123"
 value="" 
product_image="cardigan.jpg"
  catalog_item
   gender="Men's"
     size 
    description="Medium"
    color_swatch
 image="red_cardigan.jpg" 
id="color"
 value="Red"
  color_swatch 
image="burgundy_cardigan.jpg" 
id="color" 
value="burgundy"
   size
 description="Large"
  color_swatch 
image="red_cardigan.jpg"
 id="color" 
value="Red"
            color_swatch
 image="burgundy_cardigan.jpg"
 id="color"
 value="burgundy"

先谢谢你

1 个答案:

答案 0 :(得分:1)

$file是您的XML字符串:

$sxi = new RecursiveIteratorIterator(
    new SimpleXMLIterator($file),
    RecursiveIteratorIterator::SELF_FIRST);

// print root element
echo $sxi->getName(), PHP_EOL;

foreach ($sxi as $node) {
    // print element names
    echo str_repeat("\t", $sxi->getDepth() +1),
    $node->getName(),
    PHP_EOL;
    // print attributes
    foreach ($node->attributes() as $name => $value) {
        echo str_repeat("\t", $sxi->getDepth() +2),
        "$name: $value",
        PHP_EOL;
    }
}

将输出:

catalog
    product
        description: Cardigan Sweater
        id: 123
        value: 
        product_image: cardigan.jpg
        catalog_item
            gender: Men's
            size
                description: Medium
                color_swatch
                    image: red_cardigan.jpg
                    id: color
                    value: Red
                color_swatch
                    image: burgundy_cardigan.jpg
                    id: color
                    value: burgundy
            size
                description: Large
                color_swatch
                    image: red_cardigan.jpg
                    id: color
                    value: Red
                color_swatch
                    image: burgundy_cardigan.jpg
                    id: color
                    value: burgundy