2013-04-30 29 views
0

爲什麼$xml->accommodation['id']可以工作,但$xml->information['items']不可用?用PHP解析XML不工作

這裏是我的xml:

<information items="192"> 
<accommodation id="41457" code="7565E59E-77D8-4E24-8F40-85ABBB99CCD0"/> 
<accommodation id="41597" code="1C858B57-F634-4FBB-877F-1D8831417A8B"/> 
(etc.) 

這裏是我的PHP:

$url = "URL TO XML FILE"; 
$xml = simplexml_load_file($url); 

(我很新的使用XML如果你不能告訴!)

+0

[PHP SimpleXML + Get Attribute]的可能重複(http://stackoverflow.com/questions/10537657/php-simplexml-get-attribute) - 您可以在[SimpleXML基本用法示例](http://php.net/simplexml.examples-basic.php) - 旁邊很多其他的東西。因此,在下次嘗試太久之前,如果不是解決方案,那麼在那裏看看可能會給你一些提示。 – hakre 2013-05-02 12:55:23

回答

0

要獲取第一個accommodation節點的id屬性,使用;

$xml->accommodation['id'] 

要得到information節點使用的items屬性;

$xml['items'] 

例如;

<?php 

$str = ' 
    <information items="192"> 
    <accommodation id="30"/> 
    <accommodation id="50"/> 
    <accommodation id="80"/> 
    </information> 
'; 

$xml = simplexml_load_string($str); 

$a = $xml->accommodation['id']; 
$b = $xml['items']; 

echo "$a, $b"; // Output: 30, 192 

?> 

->裝置的兒童和可串連例如; $xml->foo->bar['id']

希望有所幫助。

+0

非常感謝。 $ xml ['items']可能是我沒有嘗試的唯一的東西! – MarkHFLA 2013-04-30 20:39:23

+0

不客氣:-) – 2013-05-01 00:14:50