致命错误不允许匹配“[xX] [mM] [lL]”的处理指令目标

时间:2012-11-02 04:18:12

标签: java apache https httprequest domparser

使用dom解析器解析我的xml时遇到以下异常。 网址 “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>

这是我用来解析从vl返回的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)

2 个答案:

答案 0 :(得分:1)

我不知道它失败的确切原因,但同样的问题与我有关: 当我尝试使用DocumentBuilder.parse(url)解析url response 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中,我摆脱了异常。 希望这可以解决你的问题。

答案 1 :(得分:-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);