我的XML文件如下:agents.xml
<?xml version="1.0" encoding="UTF-8"?>
<agents>
<agent id="1">
<aname>pi1</aname>
<alive>0</alive>
<scenarios>1,2,3</scenarios>
</agent>
<agent id="2">
<aname>pi2</aname>
<alive>1</alive>
<scenarios>4,5,6</scenarios>
</agent>
</agents>
我想检索由属性值“id”选择的代理的所有子元素。我尝试了以下操作,将变量“id”传递给脚本:
$agents_xml = simplexml_load_file("/<path_to>/agents.xml");
$json = json_encode($agents_xml);
$array = json_decode($json,TRUE);
//decrement id for correct index
$id=$id-1;
//I want to return in JSON format
$testarr=json_encode($array['agent'][$id]);
echo $testarr;
当“id”的值为1时,我得到了这个:
{"@attributes":{"id":"1"},"aname":"pi1","alive":"0","scenarios":"1,2,3"}
但我知道,如果XML像以下一样混乱:
<?xml version="1.0" encoding="UTF-8"?>
<agents>
<agent id="2">
<aname>pi2</aname>
<alive>1</alive>
<scenarios>4,5,6</scenarios>
</agent>
<agent id="1">
<aname>pi1</aname>
<alive>0</alive>
<scenarios>1,2,3</scenarios>
</agent>
</agents>
这不太可靠。
感谢您的任何想法!
答案 0 :(得分:0)
尝试
/customer-menu/1/accounts/1/update