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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵