$使用dom4j可解析 返回&#x等字样的 html转义字符【转】

如果以GET或POST请求某个系统返回,带有 $#x 那很有可能是axis服务器返回的.

<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><validateInvoiceResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><validateInvoiceReturn xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">{&quot;returncode&quot;:&quot;10006&quot;,&quot;returnmsg&quot;:&quot;&#x672A;&#x83B7;&#x53D6;&#x5230;&#x4EA4;&#x6613;&#x673A;&#x6784;&#x4FE1;&#x606F;&quot;,&quot;fpdm&quot;:&quot;null&quot;,&quot;fphm&quot;:&quot;null&quot;,&quot;kprq&quot;:&quot;null&quot;}
</validateInvoiceReturn></validateInvoiceResponse></soapenv:Body></soapenv:Envelope>

实际内容:

{&quot;returncode&quot;:&quot;10006&quot;,&quot;returnmsg&quot;:&quot;&#x672A;&#x83B7;&#x53D6;&#x5230;&#x4EA4;&#x6613;&#x673A;&#x6784;&#x4FE1;&#x606F;&quot;,&quot;fpdm&quot;:&quot;null&quot;,&quot;fphm&quot;:&quot;null&quot;,&quot;kprq&quot;:&quot;null&quot;}

参考 http://www.iteye.com/problems/73011

$$$$$ 使用dom4j可解析它

    public static void main(String[] args) throws Exception {
        String retXml = readStringFromFile("d://a.txt","GBK");//你刚得到的返回报文
        
        Document document = DocumentHelper.parseText(retXml);

        OutputFormat format = OutputFormat.createPrettyPrint();   
        StringWriter sw = new StringWriter();   
        XMLWriter xw = new XMLWriter(sw, format);   
        xw.setEscapeText(false);  
        xw.write(document);  
        xw.flush();   
        
        String finalRetXml = sw.toString();  
        System.out.println("最终返回报文:
"+finalRetXml);
    }

最终得到:

文件 d://a.txt存在与否?: true
读到的文件内容如下:
<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><validateInvoiceResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><validateInvoiceReturn xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">{&quot;returncode&quot;:&quot;10006&quot;,&quot;returnmsg&quot;:&quot;&#x672A;&#x83B7;&#x53D6;&#x5230;&#x4EA4;&#x6613;&#x673A;&#x6784;&#x4FE1;&#x606F;&quot;,&quot;fpdm&quot;:&quot;null&quot;,&quot;fphm&quot;:&quot;null&quot;,&quot;kprq&quot;:&quot;null&quot;}</validateInvoiceReturn></validateInvoiceResponse></soapenv:Body></soapenv:Envelope>
最终返回报文:
<?xml version="1.0" encoding="UTF-8"?>

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soapenv:Body>
    <validateInvoiceResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
      <validateInvoiceReturn xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="soapenc:string">{"returncode":"10006","returnmsg":"未获取到交易机构信息","fpdm":"null","fphm":"null","kprq":"null"}</validateInvoiceReturn>
    </validateInvoiceResponse>
  </soapenv:Body>
</soapenv:Envelope>
原文地址:https://www.cnblogs.com/whatlonelytear/p/5180068.html