How to parse a complex XML-RPC structure in PHP?
Here is my XML object: http://pastebin.com/0L8vf0ja Which was created from:
<?xml version="1.0" encoding="utf-8"?>
<methodResponse><params><param><value><struct><member><name>result</name><value><struct><member><name>application_instance_id</name><value><i4>89</i4></value></member></struct></value></member><member><name>status</name><value><i4>0</i4></value></member></struct></value></param></params></methodResponse>
I know upto this level:
$ApplicationInstanceID = (int)(string)$data->params->param->value->struct->member->
But from here, as you see in the pastebin, it becomes an array of objects - and I am not sure how to proceed.开发者_C百科
Any ideas, please?
By accessing it as an array:
$x = '<root><child><foo>23</foo><foo>34</foo></child></root>';
$xml = new SimpleXMLElement($x);
var_dump($xml->child->foo[0]);
var_dump($xml->child->foo[1]);
But, if you have it enabled, the xmlrpc_decode
function is perhaps easier to use.
var_dump(xmlrpc_decode($yourxml));
array(2) {
["result"]=>
array(1) {
["application_instance_id"]=>
int(89)
}
["status"]=>
int(0)
}
$ApplicationInstanceID = (int)(string)$data->params->param->value->struct->member[0]-> // first <member>
$ApplicationInstanceID = (int)(string)$data->params->param->value->struct->member[1]-> // second <member>
I'm a bit confused -- it looks like you've already parses the XML into a PHP object... So now just access submembers of the array as you would any elements of an array:
$data->params->param->value->struct->member[0]
And, for it's properties:
$data->params->param->value->struct->member[0]->name
you could try to use xpath to get the node value i think, SimpleXml implements it...
http://php.net/manual/en/simplexmlelement.xpath.php
精彩评论