2012-11-02 63 views
0

我在使用dom解析器解析我的xml时遇到以下异常。 url“ ”http://www.xyz.com/ABC.aspx?accessCode = ...... & vin = GJHHFJHFJHFGF6788 & reportType = 3“ 为每个vin参数返回一个xml。致命错误不允许处理指令匹配“[xX] [mM] [lL]”

下面是由上述URL

<?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> 

返回的XML这里是我使用解析XML从与VIN的URL返回的代码。

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; 
    } 

当我通过一个“VIN”参数上面的函数从通过RPC调用我的客户端,我得到正确的响应。但几个小时后(比如说4-5小时),当我传递相同的vin参数时,我会得到例外。之后,我继续得到这个异常,直到我重新启动我的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) 
+0

我想以某种方式“形成不良的”输出XML或东西,第二次检查,被它试图解析XML是什么? – rogerdpack

回答

1

我不知道为什么会失败,但同样的问题是与我的确切原因: 当我试图解析使用DocumentBuilder.parse URL响应XML( url),几次试用后解析失败。

当我使用下面的函数获取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到,我摆脱了异常。 希望这可以解决你的问题。

+0

非常感谢你!!现在我没有得到任何例外。我从过去8小时监控我的服务器。一切似乎都好。 –

-1

这是一个彻头彻尾的黑客,但下面的任何处理指令变为标签....记住....哈哈!

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); 
相关问题