我正在尝试检索xml文件的内容,但没有检索到任何内容。 我的xml文件是:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet href="mobiles.xsl" type="text/xsl"?>
<db>
<mobiles>
<name><h3><a href="http://www.digitaltrends.com/cell-phone-reviews/
motorola-moto-g-review/">Motorola Moto G</a></h3></name>
<screen_size><span class="value">4.5" - 5"</span></screen_size>
<carrier><span class="value">AT&T</span></carrier>
<Operating_System><span class="value">Android</span></Operating_System>
</mobiles>
<mobiles>
<name><h3><a href="http://www.digitaltrends.com/cell-phone-reviews
/apple-iphone-5s-review/">Apple iPhone 5S</a></h3></name>
<screen_size><span class="value">4" - 4.5"</span></screen_size>
<carrier><span class="value">AT&T</span></carrier>
<Operating_System><span class="value">iOS</span></Operating_System>
</mobiles>
<mobiles>
<name><h3><a href="http://www.digitaltrends.com/cell-phone-reviews/lg-g2-review/">
LG G2</a></h3></name>
<screen_size><span class="value">5" - 5.5"</span></screen_size>
<carrier><span class="value">AT&T</span></carrier>
<Operating_System><span class="value">Android</span></Operating_System>
</mobiles>
</db>
我用来检索内容的代码是:
<!DOCTYPE html>
<html>
<body>
<?php
$xml=simplexml_load_file("mobiles.xml");
echo "<h2>Top 3 smartphones of the month</h2>";
foreach ($xml->children() as $child){
echo "<tr>";
echo "<td>".$child->name."</td>";
echo "<td>".$child->screen_size."</td>";
echo "<td>".$child->carrier."</td>";
echo "<td>".$child->Operating_System."</td>";
echo "</tr>";
}
?>
</body>
</html>
我无法弄清楚错误。错误是什么?
答案 0 :(得分:1)
你还没有关闭php标签。尝试;
echo "</tr>";
}
?>
答案 1 :(得分:1)
更改
foreach ($xml->children() as $child){
要
foreach ($xml as $child){
此外,您没有访问正确的回显元素。例如,你应该做的事情如下: -
echo "<td>".$child->name->h3->a."</td>";
因为每个html元素都是SimpleXml元素;