【问题标题】:parsererror while converting xml to json将xml转换为json时出现解析器错误
【发布时间】:2016-12-18 01:25:43
【问题描述】:

在 php 中将 xml 响应转换为 json 时,我收到如下解析器错误

{
"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 标记中删除 时,它会给我正确的 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"
            }
        }
    }
}
}

请帮助我删除此类解析器错误....

【问题讨论】:

  • json响应仍然无效
  • 您是否使用在线提供的 Json lint 工具验证了您的响应 json
  • @MRX 是的,刚才我验证了来自jsonlint.com 的 json 响应,它显示了有效的 json
  • 问题只存在于具有 ... 类型值的标签中

标签: php android json xml xml-parsing


【解决方案1】:

实体&amp;#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);

【讨论】:

  • 问题出在 xml 中,但我从服务器获取 xml 响应,它的大小非常大...有没有办法在编码为 json 之前更改此类 xml 响应??
  • 非常感谢先生... $repaired = str_replace(" ", "", $original);工作.....
  • 小心,这只是一种解决方法。 XML 已损坏,应予以修复。这可能是其他实体会再次破坏它。
猜你喜欢
  • 2018-06-23
  • 1970-01-01
  • 2018-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-19
  • 2011-02-09
  • 1970-01-01
相关资源
最近更新 更多