第一要素+属性和一个XML文件的副元素+属性其实我想我的网页上阅读PHP5这个XML文件。阅读PHP5
我有这样的例子作为我samples.xml文件:
<sample amount="5" name="Pasta" dest="pasta/sample_pasta.lua">
<product name="pasta1"/>
<product name="pasta2"/>
</sample>
...
<sample amount="18" name="Meat" dest="pasta/sample_meat.lua">
<product name="meat1"/>
<product name="meat2"/>
</sample>
而且有我的PHP代码:
<?php
echo '<table><tr><td>Name</td><td>Amount</td><td>Product</td></tr>';
$reader = new XMLReader();
if (!$reader->open("samples.xml")) {
die("Failed to open 'samples.xml'");
}
while($reader->read()) {
if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == 'sample') {
$amount = $reader->getAttribute('amount');
$name = $reader->getAttribute('name');
echo '<tr><td>'.$name.'</td><td>'.$amount.'</td><td>---?[array result here]?---</td></tr>';
}
echo '</table>';
?>
而这就是网页上我的脚本打印:
名称|金额|商品
意大利面| 5 | ?--- [阵列这里结果] ---
肉?| 18 | ??--- [阵列这里结果] ---
但我需要这个页面作为数组就是这样的阅读产品名称:
名称|金额|产品
面条| 5 | pasta1,pasta2
肉类| 18 | meat1,meat2
请,任何信息将是有益的!
这应该使用更容易'SimpleXMLElement' – Ghost 2014-09-12 13:51:09
我怎样才能使用的SimpleXMLElement读取与它们的属性,第一和第二要素是什么?并感谢您的回答! – user3050478 2014-09-12 13:55:16