2012-08-08 74 views
1

讀取每個元素我從一個API的XML輸出以下解析XML和PHP中

<?xml version='1.0' encoding='UTF-8'?> 
<Response> 
    <ReturnRow Output1="11" Output2="12" Output3="13" Output4="14" Output5="15" /> 
    <ReturnRow Output1="21" Output2="22" Output3="23" Output4="24" Output5="25" /> 
    <Messages> 
     <Message Code="INFO" Msg="Your request is successful." Type="APP"/> 
    </Messages>  
</Response> 

我試圖解析使用PHP上面的XML,環子元素ReturnRown和回聲出Output1Output2轉換成HTML表格。

這裏是我的代碼目前:

$doc = new DOMDocument(); 
$doc->loadXML($test); //$test is holding the above XML 
$node = $doc->getElementsByTagName("ReturnRow"); 
foreach ($node as $book) { 
    var_dump($book); 
} 

但上面沒有實際給我的價值觀Ouput1Output2

我缺少什麼或者做錯了嗎?

的我上面的PHP代碼的結果是:

object(DOMElement)#3 (18) { ["tagName"]=> string(9) "ReturnRow" ["schemaTypeInfo"]=> NULL  ["nodeName"]=> string(9) "ReturnRow" ["nodeValue"]=> string(0) "" ["nodeType"]=> int(1)  ["parentNode"]=> string(22) "(object value omitted)" ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> NULL ["lastChild"]=> NULL ["previousSibling"]=> string(22) "(object value omitted)" ["nextSibling"]=> string(22) "(object value omitted)" ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(9) "ReturnRow" ["baseURI"]=> string(20) "file:///W:/ischools/" ["textContent"]=> string(0) "" } object(DOMElement)#5 (18) { ["tagName"]=> string(9) "ReturnRow" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(9) "ReturnRow" ["nodeValue"]=> string(0) "" ["nodeType"]=> int(1) ["parentNode"]=> string(22) "(object value omitted)" ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> NULL ["lastChild"]=> NULL ["previousSibling"]=> string(22) "(object value omitted)" ["nextSibling"]=> string(22) "(object value omitted)" ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(9) "ReturnRow" ["baseURI"]=> string(20) "file:///W:/ischools/" ["textContent"]=> string(0) "" } 
+0

它會給你什麼? – PeeHaa 2012-08-08 15:39:04

+0

[How to parse and process HTML with PHP?](http://stackoverflow.com/questions/3577641/how-to-parse-and-process-html-with-php) – PeeHaa 2012-08-08 15:39:16

+0

請編輯你OP信息:) – PeeHaa 2012-08-08 15:45:17

回答

2

在你的情況,我寧願用simplexml,因爲它可以返回一個節點的所有屬性在容易迭代格式,並使用XPath找到您是節點興趣:

$xml = '<'.'?xml version="1.0" encoding="UTF-8"?> 
<Response> 
    <ReturnRow Output1="11" Output2="12" Output3="13" Output4="14" Output5="15" /> 
    <ReturnRow Output1="21" Output2="22" Output3="23" Output4="24" Output5="25" /> 
    <Messages> 
     <Message Code="INFO" Msg="Your request is successful." Type="APP"/> 
    </Messages> 
</Response> 
'; 

$doc = simplexml_load_string($xml); 
$node = $doc->xpath('//ReturnRow'); 
foreach ($node as $book) { 
    foreach ($book->attributes() as $name => $value) { 
     var_dump((string)$name, (string)$value); 
    } 
} 

如果你的輸入一直有這些屬性可以使用的DOMNode的getAttribute()方法在你的原始代碼的循環以獲得的值:

foreach ($node as $book) { 
    var_dump($book->getAttribute('Output1'), $book->getAttribute('Output2')); 
} 
+0

感謝您的幫助。第一個代碼塊工作並轉儲出值。但第二個代碼塊導致一個PHP錯誤。有任何想法嗎? – 2012-08-08 16:03:36

+0

我不知道爲什麼會拋出錯誤,這是一個完整的演示,應該沒有erros工作:http://codepad.viper-7.com/tYNMzI – complex857 2012-08-08 16:08:58