将xml转换为json时的parsererror

时间:2016-08-11 10:42:03

标签: php android json xml xml-parsing

我在php中将xml响应转换为json时得到parsererror如下所示

{
"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"
            }
        }
    }
}
}

我正在使用

$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);

将xml响应转换为json  下面是我的xml回复

<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>

当我从PARENT标签中删除&amp;#4时,它给出了正确的json输出,如下所示

{
"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"
            }
        }
    }
}
}

请帮我解决删除此类错误的问题....

1 个答案:

答案 0 :(得分:2)

实体&#4;无效。所以你的XML坏了:

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

输出:

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

这将是一个控制角色(EOT,传输结束)。所以它在XML文本节点中没有意义。

应该修复XML。但是如果你不能这样做,你可以使用字符串替换来删除实体。

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