2012-10-31 109 views
-2

我需要帮助解析与许多级别的节点XML到PHP如下: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> 

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

+1

[ “东西是不正确” 是不是你的问题的说明(http://stackoverflow.com/questions/ask-advice) – Gordon

回答

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 
+0

Hi VolerK,函数data()的目的是什么,它的目的是从catalog.xml中读取 – user1787756

+0

它使脚本成为一个独立的例子,即你可以复制和粘贴脚本代码并运行它。我通常以“tiny init | |重要的东西|样板”的形式编写示例脚本 – VolkerK

0

试试这个我没有测试它,但你可以得到你应该做

 foreach ($xml as $row_xml){ 

     $category=$row_xml->Category(); 
     $site= $row_xml->site(); 
     } 
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); 
+0

喜邢佳,谢谢你的例子。我当然可以使用表格的格式来帮助我进行布局。 – user1787756