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>

これは、URLから返されたxmlをvinで解析するために使用するコードです。

 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

これは完全なハックですが、以下は処理ディレクティブをタグに変更します...覚えておいてください....ハック!

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 に答える