正在检索xml文件


Retrieving xml file

我正在尝试检索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&amp;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&amp;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&amp;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>

我搞不清楚这个错误。里面有什么错误?

更改

foreach ($xml->children() as $child){

foreach ($xml as $child){

此外,您没有访问用于回显的正确元素。例如,你应该做这样的事情:-

echo "<td>".$child->name->h3->a."</td>";

因为每个html元素都是一个SimpleXml元素;

http://php.net/simplexml

您还没有关闭php标记。尝试

echo "</tr>";
}
?>