Reputation:
I have a XML object that I need to convert to JSON for easier getting of values.
The XML object is as below:
<env:Envelope xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'>
<env:Header xmlns:work='http://bea.com/2004/06/soap/workarea/'>
<work:WorkContext xmlns:wsu='http://schemas.xmlsoap.org/ws/2002/07/utility' xmlns:work='http://bea.com/2004/06/soap/workarea/'>
<java class='java.beans.XMLDecoder'>
<string>weblogic.app.MerchantQueryWebService</string>
<int>214</int>
<string>weblogic.workarea.StringWorkContext</string>
<string>2.0</string>
<string/>
</java>
</work:WorkContext>
</env:Header>
<env:Body>
<m:RequestTransactionByTimeIntervalResponse xmlns:m='http://www.zain.com/'>
<m:RequestTransactionByTimeIntervalResult>
<java:Status xmlns:java='java:com.obopay.ws.merchantquery.zain'>0</java:Status>
<java:TotalTransactions xmlns:java='java:com.obopay.ws.merchantquery.zain'>1</java:TotalTransactions>
<java:TotalAmount xmlns:java='java:com.obopay.ws.merchantquery.zain'>50</java:TotalAmount>
<java:Transactions xmlns:java='java:com.obopay.ws.merchantquery.zain'>[46139805#254734977477#50#Test]</java:Transactions>
<java:Message xmlns:java='java:com.obopay.ws.merchantquery.zain'>Success</java:Message>
</m:RequestTransactionByTimeIntervalResult>
</m:RequestTransactionByTimeIntervalResponse>
</env:Body>
</env:Envelope>
I tried this;
$fileContents = str_replace(array("\n", "\r", "\t"), '', $response);
$fileContents = trim(str_replace('"', "'", $fileContents));
$simpleXml = simplexml_load_string($fileContents);
$json = json_encode($simpleXml);
echo $json;
However, the response is {}
Nothing on the JSON.
I would like to get the values on env:Body
, any advice on this?
Anyone.
Upvotes: 2
Views: 229
Reputation: 1098
Problem with PHP script has already been mentioned above so I would like to propose some alternatives.
There are a lot of options out there to achieve this. On top of my head there is x2js which is a client-side solution. I tried your XML and it worked pretty well with output
{
"Envelope": {
"Header": {
"WorkContext": {
"java": {
"string": [
"weblogic.app.MerchantQueryWebService",
"weblogic.workarea.StringWorkContext",
"2.0",
""
],
"int": "214",
"_class": "java.beans.XMLDecoder"
},
"_xmlns:wsu": "http://schemas.xmlsoap.org/ws/2002/07/utility",
"_xmlns:work": "http://bea.com/2004/06/soap/workarea/",
"__prefix": "work"
},
"_xmlns:work": "http://bea.com/2004/06/soap/workarea/",
"__prefix": "env"
},
"Body": {
"RequestTransactionByTimeIntervalResponse": {
"RequestTransactionByTimeIntervalResult": {
"Status": {
"_xmlns:java": "java:com.obopay.ws.merchantquery.zain",
"__prefix": "java",
"__text": "0"
},
"TotalTransactions": {
"_xmlns:java": "java:com.obopay.ws.merchantquery.zain",
"__prefix": "java",
"__text": "1"
},
"TotalAmount": {
"_xmlns:java": "java:com.obopay.ws.merchantquery.zain",
"__prefix": "java",
"__text": "50"
},
"Transactions": {
"_xmlns:java": "java:com.obopay.ws.merchantquery.zain",
"__prefix": "java",
"__text": "[46139805#254734977477#50#Test]"
},
"Message": {
"_xmlns:java": "java:com.obopay.ws.merchantquery.zain",
"__prefix": "java",
"__text": "Success"
},
"__prefix": "m"
},
"_xmlns:m": "http://www.zain.com/",
"__prefix": "m"
},
"__prefix": "env"
},
"_xmlns:env": "http://schemas.xmlsoap.org/soap/envelope/",
"__prefix": "env"
}
}
A server-side solution is YQL which provides API to do the same task. You may find this helpful.
Upvotes: 1
Reputation: 57121
The problem is that you have various namespaces which make it difficult to directly access the data. But rather than mess around converting it to JSON, it's worth trying to learn how to use SimpleXML and it becomes more flexible...
$simpleXml = simplexml_load_string($fileContents);
$simpleXml->registerXPathNamespace("m", "http://www.zain.com/");
$body = $simpleXml->xpath("//m:RequestTransactionByTimeIntervalResult");
$details = $body[0]->children("java", true);
var_dump($details);
echo "Status ".(string)$details->Status.PHP_EOL;
echo "TotalAmount ".(string)$details->TotalAmount.PHP_EOL;
Having to register namespaces allows you to use XPath and find the enclosing element for the actual data, so the line with ->xpath()
looks for an element <m:RequestTransactionByTimeIntervalResult>
. This returns an array of matching data, so in the next line you just use $body[0]
to get the first instance.
As this has data in the java
namespace, the line extracts all child nodes for just this namespace (->children("java", true)
).
The var_dump
just shows that the result is a SimpleXMLelement with the data in it. You can then access each of the values using normal SimpleXML element access (i.e. $details->TotalAmount
) casting it to a string to give you the raw data.
Upvotes: 0