XML解析到PHP

XML解析到PHP,php,xml-parsing,Php,Xml Parsing,我需要帮助将具有多个级别节点的XML解析为PHP,如下所示: catalog.xml <?xml version="1.0" encoding="ISO-8859-1"?> <Catalog> <Category> <Name>Biscuit</Name> <Type> <Type1>Chocolate Chips</Type1> <Ingredient>Flour, Chocolate

我需要帮助将具有多个级别节点的XML解析为PHP,如下所示:

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>

饼干
巧克力片
面粉、巧克力片
15分钟烤箱烘烤
柠檬泡芙
面粉、柠檬汁
15分钟烤箱烘烤
糕饼
水果蛋糕
面粉、鸡蛋、鲜奶油、水果
30分钟常规烤箱烘焙
芒果蛋糕
面粉,鸡蛋,鲜奶油,芒果,坚果
30分钟烤箱烘烤
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;
}
}
?>


我的php有些地方不对劲,我通过示例试图解决,但仍然有问题。抱歉,这是我的第一个php编码项目。提前感谢许多专家。

试试这个,我还没有测试过,但是你可以大致了解你应该做什么

      foreach ($xml as $row_xml){

      $category=$row_xml->Category();
      $site= $row_xml->site();
       }

我没有阅读您的代码,但是我有两个代码,我曾经使用带有函数的对象来解析XML,您可以参考以下内容

注意使用中文,但不影响您阅读代码

<?php 
//不适合大本地文件跟远程文件
$dom=new DOMDocument();
$dom->load("user.xml");
$persons=$dom->getElementsByTagName("person");
//文本类型:3
/*
$person->item(0)->nodeType;//echo 输出节点类型
$person->item(0)->nodeName;//echo 输出节点名字
$person->item(0)->nodeValue;//echo 输出节点值
*/
//echo $person->item(0)->childNodes->item(0)->childNodes->item(0)->nodeType;
//找下一个的同胞节点
//echo $person->item(0)->childNodes->item(0)->nextSibling->nodeName;
echo '<table border="1" width="500" align="center">';
foreach($persons as $person){
    echo '<tr>';

    echo '<td>'.$person->getAttribute("id").'</td>';
    $usernames=$person->getElementsByTagName("username");
    echo '<td>'.$usernames->item(0)->childNodes->item(0)->nodeValue.'</td>';
    $phones=$person->getElementsByTagName("phone");
    echo '<td>'.$phones->item(0)->childNodes->item(0)->nodeValue.'</td>';
    echo '</tr>';
}
//header("Content-type:text/xml");
//echo $dom->saveXML();

<?php
//第一步 创建解析器 xml_parser_create(编码);
$xml = xml_parser_create('utf-8');
//xml_parser_set_option — 为指定 XML 解析进行选项设置
//xml_parser_set_option($xml,XML_OPTION_CASE_FOLDING,false); false原样输出
//注册事件,将遇到开始和结束表计时使用什么函数
xml_set_element_handler($xml,"starttag","endtag");

xml_set_character_data_handler($xml,"content");

function starttag($x,$tagName,$args){
    if($tagName=="USERS")
        echo "<table border='1' width='800' align='center' ><caption>{$tagName}";
    else if($tagName == "USER"){
        echo "<tr>";
        echo "<td>{$args['ID']}</td>";
    }else
        echo "<td>";
}

function endtag($x,$tagName){
    if($tagName=="USERS")
        echo "</caption></table>";
    else if($tagName == "USER")
        echo "</tr>";
    else
        echo "</td>";

}

function content($x,$content){
    echo $content;
}

$printerror=false;

$xmlfile="user.xml";
//第二步 读取数据
$fp=fopen($xmlfile,"r");

while(!feof($fp)){
    $data=fread($fp,1024);
    //开始解析
    if(!xml_parse($xml,$data)){
        $printerror=true;
    }
}

//关闭文件
fclose($fp);
if($printerror){
    $row=xml_get_current_line_number($xml);
    $col=xml_get_current_column_number($xml);
    $errormess=xml_error_string(xml_get_error_code($xml));

    echo "在文件{$xmlfile}中,[{$row}行,{$col}列]:{$errormess}.";
}
//关闭解析器
xml_parser_free($xml);
请阅读
例如:


您好,VolerK,从catalog.xml中读取函数data()的目的是什么?它使脚本成为一个自包含的示例,即您可以复制和粘贴脚本代码并运行它。我通常以
tinyinit |重要的东西|样板文件
Hi-Xingjia的形式编写示例脚本,谢谢您的示例。我当然可以使用表格的格式来帮助我进行布局。
<?php
// $xml = simplexml_load_file("catalog.xml");
$catalog = new SimpleXMLElement(data());

foreach ( $catalog->Category as $category ) {
    echo 'Name: ', $category->Name, "\n";
    foreach( $category->Type as $type ) {
        if ( isset($type->Type1) ) {
            echo "  type1: ", $type->Type1, "\n";
        }
        else if ( isset($type->Type2) ) {
            echo "  type2: ", $type->Type2, "\n";
        }
        else {
            echo "  unknown type\n";
        }
        echo '    Ingredient:', $type->Ingredient, "\n";
        echo '    Duration: ', $type->Duration, "\n";
    }
}


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