You do not want to have the " @attributes " field encoded in JSON, but this is the standard way that PHP JSON serializes SimpleXMLElement.
As you say, you want to change this, you need to change the way that PHP JSON serializes the object. This is possible by implementing JsonSerializable using SimpleXMLElement yourself, and then providing JSON Serialization as you wish:
class JsonSerializer extends SimpleXmlElement implements JsonSerializable { function jsonSerialize() {
eg. using attributes as fields similar to all child elements.
Then you can just easily integrate it, for example. instead
$xml = simplexml_load_string($result);
you can use
$xml = simplexml_load_string($result, 'JsonSerializer');
or simply
$xml = new JsonSerializer($result);
and the rest of your function works the same, but only with your serialization of wishes.
Example:
$result = str_replace(array("\n", "\r", "\t"), '', $data); $xml = new JsonSerializer($result); $object = new stdclass(); $object->webservice[] = $xml; $result = json_encode($object, JSON_PRETTY_PRINT); header('content-Type: application/json'); echo $result;
Output:
{ "webservice": [ { "EBLCUSTOMER": { "ACCOUNTNO": "11111", "CUSTACCTNO": "121212", "ACCTSTATUS": "active", "CCYDESC": "BDT", "BALANCE": "9999", "AVAILABLEBALANCE": "99", "CUSTOMERNAME": "cus_name", "AMOUNTONHOLD": "1000", "ODLIMIT": "99" } } ] }
The serialization function for the above example:
function jsonSerialize() { // text node (or mixed node represented as text or self closing tag) if (!count($this)) { return $this[0] == $this ? trim($this) : null ; } // process all child elements and their attributes foreach ($this as $tag => $element) { // attributes first foreach ($element->attributes() as $name => $value) { $array[$tag][$name] = $value; } // child elements second foreach($element as $name => $value) { $array[$tag][$name] = $value; } } return $array; }
Some notes here:
- In serialization, you have to take care of what type of element belongs to you. Differentiation is performed from above for individual elements without children. If you need attribute handling, you need to add it.
trim($this) may already save you the problem you're trying to catch with $result = str_replace(array("\n", "\r", "\t"), '', $data); . SimpleXMLElement In any case, JSON will serialize the characters " \r " ( SimpleXMLElement uses " \n " for gaps). You might also be interested in XML normalization rules for whitespace .- If the attribute has the same name as the child, it will be overwritten by the child.
- If a child that follows another child with the same name, it will be overwritten.
The last two points are simply to keep a simple code example. A method that is consistent with the standard JSON serialization of the PHP SimpleXMLElement standard is provided in a number of my blog posts.
The basics of this particular procedure and the approximate implementation of JsonSerialize are available in the third post: SimpleXML and JSON Encode in PHP - Part III and the end .
Another related question:
- PHP converts XML to JSON group when there is one child