打印所有节点SimpleXML

时间:2011-10-14 14:13:21

标签: php simplexml

这是我的第一个xml解析器脚本。 我的代码:

<?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个孩子。但是这段代码不会打印任何东西.. 问题在哪里?

...谢谢

1 个答案:

答案 0 :(得分:0)

note是一个SimpleXMLObject。所以你需要指针(箭头)表示法而不是数组(括号)表示法。

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