使用simplexml_load_file方法解析XML时遇到问题。我有像这样的xml:
<root>
<show>
<list>
<entry name="this is title" code="1234">
<infos count="4">
<info type="random" percent="10" />
<info type="random" percent="12" />
<info type="random" percent="13" />
<info type="random" percent="15" />
</infos>
</entry>
<entry name="this is titles" code="1235">
<infos count="4">
<info type="random" percent="14" />
<info type="random" percent="16" />
<info type="random" percent="17" />
<info type="random" percent="18" />
</infos>
</entry>
</list>
</show>
</root>
我尝试使用simplexml_load_file进行解析,但我得到一个结果。我的意思是,不是两个条目。请看下面的代码。
<?php
$url = "text.xml";
$xml = simplexml_load_file($url);
foreach ($xml->show->list as $entry) {
$name = $entry->entry['name'];
$code = $entry->entry['code'];
echo "<p>The List Name: ".$name."</p>";
echo "<p>Code:".$code."</p>";
}
?>
好的,我尝试使用该代码,我只得到一个结果。我不知道怎么循环。所以,我试图在这个网站上提问。
感谢所有能够伸出援手的人:D
答案 0 :(得分:3)
foreach ($xml->list->enrty as $entry) {
$name = $entry->entry['name'];
$code = $entry->entry['code'];
echo "<p>The List Name: ".$name."</p>";
echo "<p>Code:".$code."</p>";
}
应该是
foreach ($xml->show->list->entry as $entry) {
$name = $entry->attributes()->name;
$code = $entry->attributes()->code;
//or like this
//$name = $entry["name"];
//$name = $entry["code"];
echo "<p>The List Name: ".$name."</p>";
echo "<p>Code:".$code."</p>";
}