我用下面的XML响应结构工作:PHP - 迭代虽然Additonial XML节点
<CompressedVehicles>
<F>
<RS>
<R>
<VS>
<V />
<V />
</VS>
</R>
<R>
<VS>
<V />
<V />
</VS>
</R>
</RS>
</F>
</CompressedVehicles>
到目前为止,从一个老乡堆栈溢出成员指导下,我才得以构建基于工作JSON输出下面的PHP代码:
header('Content-Type: application/json');
$xml = simplexml_load_file('inventory.xml');
$CompressedVehicles = $xml->CompressedVehicles;
$attributes = array();
foreach($CompressedVehicles->F->attributes() as $key => $val)
{
$attributes[$key] = $val->__toString();
}
$data = array();
foreach($CompressedVehicles->F->RS->R->VS->V as $vehicle)
{
$line = array();
foreach($vehicle->attributes() as $key => $val)
{
$line[$attributes[$key]] = $val->__toString();
}
$data[] = $line;
}
$json = json_encode($data);
echo $json;
通过完成之前单个<R>
节点这仅迭代。我现在如何追加代码来迭代每个<R>
节点?
预先感谢您。
不直接指向它变成'V',削减到每个'R',所以鸟巢另一个循环,第一循环,直到' R',然后每一个'R'然后指向'VS-> V' – Ghost