-2

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

目录.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>

菜单.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 编码项目。在此先感谢许多专家。

4

3 回答 3

0

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

      foreach ($xml as $row_xml){

      $category=$row_xml->Category();
      $site= $row_xml->site();
       }
于 2012-10-31T08:28:55.470 回答
0

我没有看你的代码,但是我有两段代码是我使用对象和函数来解析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);
于 2012-10-31T08:34:56.327 回答
0

请阅读http://devzone.zend.com/240/simplexml/
示例:

<?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
于 2012-10-31T08:38:48.820 回答