2

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

示例:需要在subcategories2之后进入subcategories1之后在subcategories3之后,在subcategories2->subcategories3中返回,如果没有更多subcategories2进入subcategories1,如果该分类节点没有subcategories1则执行下一个。

<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...
   }
}

ps我的英语有点生疏。只是为了清楚我需要进入第一个“类别”标签,这里进入“subcategory1”这里“subcategory2”这里所有“subcategoory3”在下一个“subcategory2”中返回一个级别如果存在..如果不进入“子类别1”中的另一个级别,如果存在,则执行相同的规则,否则转到下一个“类别”标签。

4

1 回答 1

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 ){
                ......
            }
        }
    }

}
于 2013-03-15T11:32:09.680 回答