“SAXParseException:不能以xml开头”警告在某些设备中发生

时间:2014-07-22 04:42:54

标签: android xml xml-parsing

我想从网络服务器获取一个xml文件,我在一些设备中尝试了我的代码,其中一些(三星galaxy i9000,inhon papilio g1)可以完全解析xml文件,但有些(Sony Xperia z,三星Galaxy Tab3)会收到警告而没有结果。以下是我的代码和日志,

以下代码:

HttpClient client = EasySSLSocketFactory.createMyHttpClient();
HttpGet methiod = new HttpGet(obj.toString());

HttpResponse response = client.execute(methiod);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity);

parsingXMLProcess(result);


private void parsingXMLProcess(String xmlfile) {
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        InputSource ins = new InputSource();
        ins.setCharacterStream(new StringReader(xmlfile));
        Document doc = builder.parse(ins);

        Log.i(getStringFromDocument(doc));

        NodeList nl1 = doc.getElementsByTagName("RequestId");
        NodeList nl2 = doc.getElementsByTagName("ResultCode");
        NodeList nl3 = doc.getElementsByTagName("ResultText");

        Log.d("check RequestId: \n" + getNodeFromNL(nl1) + "\n" +
              " ResultCode: \n" + getNodeFromNL(nl2) + "\n" + 
              " ResultText: \n" + getNodeFromNL(nl3));

    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

记录如下:

org.xml.sax.SAXParseException: processing instructions must not start with xml (position:unknown @10:7 in java.io.StringReader@44802648) 
at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:146)
at org.facetone.FacetoneFirstActivity.parsingXMLProcess(FacetoneFirstActivity.java:316)
at org.facetone.FacetoneFirstActivity.access$8(FacetoneFirstActivity.java:310)
at org.facetone.FacetoneFirstActivity$4.run(FacetoneFirstActivity.java:297)
at java.lang.Thread.run(Thread.java:841)

我也丢弃了<?xml version="1.0" encoding="utf-8"?>,警告将更改为“找不到xml解析协议”,真的没有任何线索来修复它,任何建议都将受到赞赏!!

XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<LicenseActivateRep>
    <RequestId>123456</RequestId>
    <ResultCode>1234</ResultCode>
    <ResultText>hello</ResultText>
</LicenseActivateRep>

2 个答案:

答案 0 :(得分:0)

请尝试这种方式,希望这有助于您解决问题。

private void parsingXMLProcess(String xmlfile) {
try {
     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
     DocumentBuilder builder = factory.newDocumentBuilder();
     InputSource ins = new InputSource();
     ins.setCharacterStream(new StringReader(xmlfile));
     Document doc = builder.parse(ins);

     Node nl1 = doc.getElementsByTagName("RequestId").item(0).getChildNodes().item(0);
     Node nl2 = doc.getElementsByTagName("ResultCode").item(0).getChildNodes().item(0);
     Node nl3 = doc.getElementsByTagName("ResultText").item(0).getChildNodes().item(0);

     System.out.print("RequestId  : "+nl1.getNodeValue());
     System.out.print("ResultCode : "+nl2.getNodeValue());
     System.out.print("ResultText : "+nl3.getNodeValue());

} catch (Throwable e) {
    e.printStackTrace();
}
}

答案 1 :(得分:-1)

构造是XML声明,只能出现在XML文件的开头。

构造,其中pi是任何不以&#34; xml&#34;开头的名称,是一条处理指令,除了文件的开头外,它可以显示。

因此,错误基本上意味着在文件的开头以外的地方找到了XML声明。可能在第10行第7栏。

为什么要在某些设备上而不是其他设备上出现错误?也许有些人没有仔细检查你的XML。