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 为什么shp文件会有这种小方块?
  • ¥15 ecplise在连接数据库时显示加载驱动成功但是数据库连接失败
  • ¥15 visionmaster启动失败,提示为“机器不满足授权而被禁用”
  • ¥50 用logisim设计16位单时钟周期cpu
  • ¥15 IDEA中圈复杂度如何具体设置
  • ¥50 labview采集不了数据
  • ¥15 Multisim红外倒车雷达仿真中距离问题
  • ¥15 请上面代码做什么处理或什么混淆
  • ¥15 英雄联盟自定义房间置顶
  • ¥15 W5500网线插上无反应