2011-10-14 150 views
0

這是我的第一個xml解析器腳本。 我的代碼:打印所有節點SimpleXML

<?php 
$xmlstring = " 
<book> 
    <note> 
    <to>Tove</to> 
    <from>Jani</from> 
    <heading>Reminder</heading> 
    <body>Don't forget me this weekend!</body> 
    </note> 
    <note> 
    <to>Tove1</to> 
    <from>Jani</from> 
    <heading>Reminder</heading> 
    <body>Don't forget me this weekend!</body> 
    </note> 
</book> 
"; 

$xml = new SimpleXMLElement($xmlstring); 
foreach($xml->note as $note){ 
    echo $note["to"] . $note["from"] . $note["heading"] . $note["body"]; 
} 
?> 

我想打印note孩子。但此代碼不會打印任何內容.. 問題在哪裏?

謝謝...

回答

0

note是SimpleXMLObject。所以你需要指針(箭頭)符號而不是數組(括號)符號。

foreach($xml->note as $note){ 
    echo $note->to . $note->from . $note->heading . $note->body; 
}