2012-10-31 158 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