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 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程