распечатать все узлы SimpleXML - PullRequest
0 голосов
/ 14 октября 2011

Это мой первый скрипт парсера 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 голосов
/ 14 октября 2011

note - это объект SimpleXMLObject.Таким образом, вам потребуется указатель (стрелка), а не массив (скобка).

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