0

我尝试使用 Apache Commons IO - BOMInputStream 方法来删​​除 BOM 并解析 XML 格式的 URL。但是,出现以下错误

[致命错误]:1:1:序言中不允许内容。org.xml.sax.SAXParseException;行号:1;列号:1;序言中不能有内容。

运行程序时仍然存在。我检查过 XML 格式的 URL 链接可能包含超过 1 个 BOM,但我认为 BOMInputStream 方法应该能够处理它们。我的代码中是否还有其他问题?感谢任何帮助。

ps 我可以使用字符串解析 URL,但希望保留 XML 格式,以便准确解析数据。

public void parseGoogleApi() throws IOException, SAXException,ParserConfigurationException {

    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();

        urlName = "http://www.google.com/finance/getprices?q=0005&x=HKG&i=3600&p=10d&f=d,c,h,l,o,v";
        URL u = new URL(urlName);
        HttpURLConnection urlCon = (HttpURLConnection) u.openConnection();                 
        urlCon.connect();
        InputStream iStream = urlCon.getInputStream();
        BOMInputStream bomIn = new BOMInputStream(iStream, ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_16BE,ByteOrderMark.UTF_32LE, ByteOrderMark.UTF_32BE);

        Document doc = builder.parse(bomIn); 
        iStream.close();
        System.out.println(doc);

    } catch (Exception e) {
        System.out.println(e);
    }
}
4

0 回答 0