您好我可以解析数组,但是我无法解析所有子项(子项),它只显示子项的第一项。
$xml_data =
'<response>
<item>
<name>Life Cover1</name>
<feature>Indexation1</feature>
<benefits>
<bitem>foo1</bitem>
<bitem>foo2</bitem>
</benefits>
</item>
<item>
<name>Life Cover2</name>
<feature>Indexation2</feature>
<benefits>
<bitem>fd1</bitem>
<bitem>foo3</bitem>
<bitem>foo4</bitem>
</benefits>
</item>
<item>
<name>Life Cover3</name>
<feature>Indexation3</feature>
<benefits>
<bitem>foo5</bitem>
<bitem>foo6</bitem>
</benefits>
</item>
</response>
';
//Display Data output format View
$xml = new SimpleXMLElement($xml_data);
$items = $xml->xpath('//item');
$itemNames = $xml->xpath('//name');
$features = $xml->xpath('//feature');
$benefits = $xml->xpath('//benefits');
$DataSize = sizeof($items);
echo '<table border="1" cellpadding="0" cellspacing="0"><tbody>';
foreach($items as $item){
echo '<tr>';
echo '<td>' . $item->name . '</td>';
echo '<td>' . $item->feature . '</td>';
echo '<td>' . $item->benefits->children() . '</td>';
echo '</tr>';
}
echo '</tbody></table>';
$ item-&gt; benefits-&gt; children();只显示第一个bitem,如何在这种解析输出中显示所有bitem?
由于
答案 0 :(得分:1)
你可以试试这个:
$xml_data =
'<response>
<item>
<name>Life Cover1</name>
<feature>Indexation1</feature>
<benefits>
<bitem>foo1</bitem>
<bitem>foo2</bitem>
</benefits>
</item>
<item>
<name>Life Cover2</name>
<feature>Indexation2</feature>
<benefits>
<bitem>fd1</bitem>
<bitem>foo3</bitem>
<bitem>foo4</bitem>
</benefits>
</item>
<item>
<name>Life Cover3</name>
<feature>Indexation3</feature>
<benefits>
<bitem>foo5</bitem>
<bitem>foo6</bitem>
</benefits>
</item>
</response>
';
//Display Data output format View
$xml = new SimpleXMLElement($xml_data);
$items = $xml->xpath('//item');
$itemNames = $xml->xpath('//name');
$features = $xml->xpath('//feature');
$benefits = $xml->xpath('//benefits');
$DataSize = sizeof($items);
echo '<table border="1" cellpadding="0" cellspacing="0"><tbody>';
foreach($items as $item){
echo '<tr>';
echo '<td>' . $item->name . '</td>';
echo '<td>' . $item->feature . '</td>';
echo '<td>';
$child_item = '';
foreach($item->benefits->children() as $child)
{
$child_item .= $child.' ,';
}
echo rtrim($child_item,' ,');
echo '</td>';
echo '</tr>';
}
echo '</tbody></table>';
这将显示所有包含逗号分隔字符串的子项。希望这会对你有所帮助。