簡體   English   中英

xml轉換為json時發生parsererror

[英]parsererror while converting xml to json

當在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標簽中刪除&#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"
            }
        }
    }
}
}

請幫助我該刪除此類parsererror的方法。

實體&#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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM