user6344335Tejpal user6344335Tejpal - 3 months ago 11
JSON Question

parsererror while converting xml to json

I am getting parsererror as follow while converting xml response to json in php

{
"ENVELOPE":{
"parsererror":{
"h3":[
"This page contains the following errors:",
"Below is a rendering of the page up to the first error."
],
"div":{
"_style":"font-family:monospace;
font-size:12px",
"__text":"error on line 16 at column 32: xmlParseCharRef: invalid xmlChar value 4\n"
},
"_style":"display: block;
white-space: pre;
border: 2px solid #c77;
padding: 0 1em 0 1em;
margin: 1em;
background-color: #fdd;
color: black"
},
"HEADER":{
"VERSION":"1",
"STATUS":"1"
},
"BODY":{
"DESC":{
"CMPINFO":{
"COMPANY":"0"
}
},
"DATA":{
"COLLECTION":{
"GROUP":{
"PARENT":{
"_TYPE":"String"
},
"_NAME":"Capital Account",
"_RESERVEDNAME":"Capital Account"
},
"_ISMSTDEPTYPE":"Yes","_MSTDEPTYPE":"4"
}
}
}
}
}


i am using

$data = curl_exec($ch);
curl_close($ch);

$array_data = json_decode(json_encode(simplexml_load_string($data)), false);

$response["error"] = FALSE;
$response["name"] = $array_data;
echo json_encode($response);


to convert xml response to json
bellow is my xml response

<ENVELOPE>
<HEADER>
<VERSION>1</VERSION>
<STATUS>1</STATUS>
</HEADER>
<BODY>
<DESC>
<CMPINFO>
<COMPANY>0</COMPANY>
</CMPINFO>
</DESC>
<DATA>
<COLLECTION ISMSTDEPTYPE="Yes" MSTDEPTYPE="4">
<GROUP NAME="Capital Account" RESERVEDNAME="Capital Account">
<PARENT TYPE="String">&#4; Primary</PARENT>
<COMPANYNAME TYPE="String">Vision Solutions App</COMPANYNAME>
<MASTERID TYPE="Number"> 1</MASTERID>
<BSDRCLOSING TYPE="Amount"></BSDRCLOSING>
<BSCRCLOSING TYPE="Amount">12345.00</BSCRCLOSING>
<ISGROUP TYPE="Logical">Yes</ISGROUP>
<VSPLNAME TYPE="String">Capital Account</VSPLNAME>
</GROUP>
<GROUP NAME="Current Assets" RESERVEDNAME="Current Assets">
<PARENT TYPE="String">&#4; Primary</PARENT>
<COMPANYNAME TYPE="String">Vision Solutions App</COMPANYNAME>
<MASTERID TYPE="Number"> 6</MASTERID>
<BSDRCLOSING TYPE="Amount">-11247421.05</BSDRCLOSING>
<BSCRCLOSING TYPE="Amount">22260433.00</BSCRCLOSING>
<ISGROUP TYPE="Logical">Yes</ISGROUP>
<VSPLNAME TYPE="String">Current Assets</VSPLNAME>
</GROUP>
</COLLECTION>
</DATA>
</BODY>
</ENVELOPE>


when i remove &#4 from PARENT tag it gives me proper json output as follow

{
"ENVELOPE":{
"HEADER":{
"VERSION":"1",
"STATUS":"1"
},
"BODY":{
"DESC":{
"CMPINFO":{
"COMPANY":"0",
"VOUCHER":"0"
}
},
"DATA":{
"COLLECTION":{
"GROUP":[
{
"PARENT":{
"_TYPE":"String",
"__text":"Primary"
},
"COMPANYNAME":{
"_TYPE":"String",
"__text":"Vision Solutions App"
},
"MASTERID":{
"_TYPE":"Number",
"__text":" 1"
},
"BSDRCLOSING":{
"_TYPE":"Amount"
},
"BSCRCLOSING":{
"_TYPE":"Amount",
"__text":"12345.00"
},
"ISGROUP":{
"_TYPE":"Logical",
"__text":"Yes"
},
"VSPLNAME":{
"_TYPE":"String",
"__text":"Capital Account"
},
"_NAME":"Capital Account",
"_RESERVEDNAME":"Capital Account"
},{
....
],
"_ISMSTDEPTYPE":"Yes",
"_MSTDEPTYPE":"4"
}
}
}
}
}


please help me what to do for removing such parsererror....

ThW ThW
Answer

The entity &#4; is not valid. So your XML is broken:

$element = new SimpleXmlElement('<PARENT TYPE="String">&#4; Primary</PARENT>');

Output:

Warning: SimpleXMLElement::__construct(): Entity: line 1: parser error : xmlParseCharRef: invalid xmlChar value 4

That would be a control character (EOT, End Of Transmission). So it does not make sense in an XML text node.

The XML should be fixed. But if you are not able to do that, you could use a string replace to remove the entities.

$repaired = str_replace('&#4;', '', $original);
Comments