0

使用 dom 解析器解析我的 xml 时出现以下异常。url "http://www.xyz.com/ABC.aspx?accessCode=......&vin=GJHHFJHFJHFGF6788&reportType=3" 为每个 vin 参数返回一个 xml。

这是上面 url 返回的 xml

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<VINdecode Version="1.0.0" Report_Type="LITE" Date="11/1/2012">
    <VIN Number="GJHHFJHFJHFGF6788" Status="SUCCESS">
        <Vehicle VINdecode_Vehicle_ID="26870" Model_Year="2004" Make="Volkswagen" Model="Touareg" Trim_Level="V6">
            <Item Key="Model Year" Value="2004" Unit="" />
            <Item Key="Make" Value="Volkswagen" Unit="" />
            <Item Key="Model" Value="Touareg" Unit="" />
            <Item Key="Trim Level" Value="V6" Unit="" />
            <Item Key="Manufactured in" Value="GERMANY" Unit="" />
            <Item Key="Body Style" Value="SPORT UTILITY 2-DR" Unit="" />
            <Item Key="Engine Type" Value="3.2L V6 DOHC 24V" Unit="" />
        </Vehicle>
    </VIN>
</VINdecode>

这是我用来解析从带有 vin 的 url 返回的 xml 的代码。

 public VIN getVINExpansion(String vin) 
    {
        if(vin.length() != 17)
           return null;
        VIN vehicle = null;

        try 
        {

               String url="http://www.xyz.com/ABC.aspx?accessCode=........&vin="
                    + vin + "&reportType=3";
               DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
               DocumentBuilder db = dbf.newDocumentBuilder();
               Document doc = db.parse(url);  **// I get Exception in this line**
               NodeList vinlist = doc.getElementsByTagName("VIN");

               // rest goes here

         }
         catch(Exception e)
         {
           e.printStackTrace();
         }
        return vin;
    }

当我通过 rpc 调用从客户端将“vin”参数传递给上述函数时,我得到了正确的响应。但是在我传递相同的 vin 参数几个小时(比如 4-5)小时后,我得到了异常。之后,我继续收到此异常,直到我重新启动我的 tomcat 服务器。重新启动 tomcat 服务器后,我再次得到正确的响应 4-5 小时,直到它开始失败。

我得到的例外:

[Fatal Error] xml_ABC.aspx?accessCode=.......&vin=GJHHFJHFJHFGF6788&reportType=3:4:6: The processing instruction target matching "[xX][mM][lL]" is not allowed.
org.xml.sax.SAXParseException;


 systemId: http://www.xyz.com/ABC.aspx?accessCode=......&vin=GJHHFJHFJHFGF6788&reportType=3; lineNumber: 4; columnNumber: 6; The processing instruction target matching "[xX][mM][lL]" is not allowed.
        at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
        at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
        at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:177)
4

2 回答 2

1

我不知道它失败的确切原因,但我也遇到了同样的问题:当我尝试使用 DocumentBuilder.parse(url) 解析 url 响应 xml 时,经过几次试验后解析失败。

当我使用以下函数获取响应 xml 时:

public String getHttpGetResponseString(String url) throws Exception
    {
        HttpClient httpclient = new DefaultHttpClient();
        String responseBody ="";
        try {
            HttpGet httpget = new HttpGet(url);

            System.out.println("executing request " + httpget.getURI());

            // Create a response handler
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            responseBody = httpclient.execute(httpget, responseHandler);


        }
        finally {
            // When HttpClient instance is no longer needed,
            // shut down the connection manager to ensure
            // immediate deallocation of all system resources
            httpclient.getConnectionManager().shutdown();

        }
        return responseBody;
    }

然后将xml加载到dom中,我摆脱了异常。希望这可以解决您的问题。

于 2012-11-04T16:07:56.707 回答
-1

这是一个完全的hack,但以下将任何处理指令更改为标签....记住.... Hack!

public static String formatXml(String xml)
{
    String result = doFormatXml(xml);
    if (result.equals(xml))
    {
        result = doFormatXml(xml + "</xml>");
    }
    return result;
}

public static String doFormatXml(String xml)
{
    xml = xml.replaceAll("[?][>]", "/>");
    xml = xml.replaceAll("[<][?]", "<");
    try{
        Transformer serializer= SAXTransformerFactory.newInstance().newTransformer();
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        Source xmlSource=new SAXSource(new InputSource(new ByteArrayInputStream(xml.getBytes())));
        StreamResult res =  new StreamResult(new ByteArrayOutputStream());            
        serializer.transform(xmlSource, res);
        return new String(((ByteArrayOutputStream)res.getOutputStream()).toByteArray());
    }catch(Exception e){
        return xml;
    }
}

所以这样称呼它:

   String formattedXml = formatXml(unformattedXml);
于 2013-10-01T16:41:49.660 回答