dongshi9526 2012-10-31 08:18
浏览 19
已采纳

XML解析为PHP [关闭]

I need help to parse XML with many level nodes into PHP as follows:

catalog.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<Catalog>
<Category>
<Name>Biscuit</Name>
<Type>
<Type1>Chocolate Chips</Type1>
<Ingredient>Flour, Chocolate Chips</Ingredient>
<Duration>15 minute Oven Bake</Duration>
</Type>
<Type>
<Type2>Lemon Puff</Type2>
<Ingredient>Flour, Lemon Extract</Ingredient>
<Duration>15 minute Oven Bake</Duration>
</Type>
</Category>
<Category>
<Name>Cake</Name>
<Type>
<Type1>Fruit Cake</Type1>
<Ingredient>Flour, Egg, Fresh Cream, Fruits</Ingredient>
<Duration>30 minute Conventional Oven Bake</Duration>
</Type>
<Type>
<Type2>Mango Cake</Type2>
<Ingredient>Flour, Egg, Fresh Cream, Mango, Nuts</Ingredient>
<Duration>30 minute Oven Bake</Duration>
</Type>
</Category>
</Catalog>

menu.php

<?php
$xml = simplexml_load_file("catalog.xml");
foreach ($xml->xpath('//Category') as $category)
{
echo $category->Name;
foreach($xml->xpath('//Site') as $site)
{
echo $site->ID;
}
}
?>

Something is not right in my php which I tried to solve going through examples but still having problem. Sorry this is my first php coding project. Thanks in advance to many experts.

  • 写回答

3条回答 默认 最新

  • douhuan3420 2012-10-31 08:38
    关注

    Please have a read of http://devzone.zend.com/240/simplexml/
    example:

    <?php
    // $xml = simplexml_load_file("catalog.xml");
    $catalog = new SimpleXMLElement(data());
    
    foreach ( $catalog->Category as $category ) {
        echo 'Name: ', $category->Name, "
    ";
        foreach( $category->Type as $type ) {
            if ( isset($type->Type1) ) {
                echo "  type1: ", $type->Type1, "
    ";
            }
            else if ( isset($type->Type2) ) {
                echo "  type2: ", $type->Type2, "
    ";
            }
            else {
                echo "  unknown type
    ";
            }
            echo '    Ingredient:', $type->Ingredient, "
    ";
            echo '    Duration: ', $type->Duration, "
    ";
        }
    }
    
    
    function data() {
        return <<< eox
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <Catalog>
    <Category>
    <Name>Biscuit</Name>
    <Type>
    <Type1>Chocolate Chips</Type1>
    <Ingredient>Flour, Chocolate Chips</Ingredient>
    <Duration>15 minute Oven Bake</Duration>
    </Type>
    <Type>
    <Type2>Lemon Puff</Type2>
    <Ingredient>Flour, Lemon Extract</Ingredient>
    <Duration>15 minute Oven Bake</Duration>
    </Type>
    </Category>
    <Category>
    <Name>Cake</Name>
    <Type>
    <Type1>Fruit Cake</Type1>
    <Ingredient>Flour, Egg, Fresh Cream, Fruits</Ingredient>
    <Duration>30 minute Conventional Oven Bake</Duration>
    </Type>
    <Type>
    <Type2>Mango Cake</Type2>
    <Ingredient>Flour, Egg, Fresh Cream, Mango, Nuts</Ingredient>
    <Duration>30 minute Oven Bake</Duration>
    </Type>
    </Category>
    </Catalog>
    eox;
    }
    

    prints

    Name: Biscuit
      type1: Chocolate Chips
        Ingredient:Flour, Chocolate Chips
        Duration: 15 minute Oven Bake
      type2: Lemon Puff
        Ingredient:Flour, Lemon Extract
        Duration: 15 minute Oven Bake
    Name: Cake
      type1: Fruit Cake
        Ingredient:Flour, Egg, Fresh Cream, Fruits
        Duration: 30 minute Conventional Oven Bake
      type2: Mango Cake
        Ingredient:Flour, Egg, Fresh Cream, Mango, Nuts
        Duration: 30 minute Oven Bake
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大