php简单的xml短语多级标签

时间:2013-03-13 08:10:36

标签: php xml simplexml

有可能用simpleXML来解析foreach“类别” - >所有“subCategories1”节点(不仅是每个“类别”中的第一个)?

示例:在subategories3之后的子类别2之后需要进入子类别1,返回子类别2->子类别3,如果没有更多子类别2,则转到子类别1,如果此类中没有子类别1 categories节点执行下一个。

<xml>
<categories>
    <tag11>value</tag11>
    <tag12>value</tag12>
    <subCategories1> 
        <tag21>value</tag21>
        <tag22>value</tag22>
        <subCategories2>
            <!-- ........ -->
            <tag31>....</tag31>
        </subCategories2>
    </subCategories1>
</categories>

<categories>
    <subCategories1> 
        <!-- ............... -->
    </subCategories1> 
    <subCategories1> 
        <!-- ............... -->
    </subCategories1> 
</categories>

<!-- ....... -->

<categories>
    <!-- ............ -->
</categories>

</xml>
foreach($xml->Categories as $categories){
   foreach ($categories->SubCategories1 as $cat => $value){ 
       //this took only the first SubCategories1 node from all Categories tag...
   }
}

P.S。我的英文有点生锈。要明确我需要进入第一个“类别”标签,这里进入“subcategory1”这里“subcategory2”这里所有“subcategoory3”返回下一个“subcategory2”中的一个级别如果如果存在,那么在“subcategory1”中不会再进入另一个级别。如果存在相同的rutine,则转到下一个“categories”标记。

1 个答案:

答案 0 :(得分:0)

我找到了DOMxml的答案:)

$xml = new DOMDocument();
$xml->load($destinationFile);
$categories = $xml->getElementsByTagName("Categories");
foreach( $categories as $category ){
    //..............
    foreach( $category->getElementsByTagName("SubCategories1") as $subcategory1 ){
        //...............
        foreach( $subcategory1->getElementsByTagName("SubCategories2") as $subcategory2 ){
            //............
            foreach( $subcategory2->getElementsByTagName("SubCategories3") as $subcategory3 ){
                ......
            }
        }
    }

}