DOM Parser无法解析大型xml文件

时间:2016-02-16 15:09:53

标签: java xml xpath domparser

我正在尝试使用DOM Parser和Xpath解析一个大的xml文件,但似乎我的代码断了,因为它是一个大的xml文件(60000行)。当我尝试打印xml时,它从xml的中间开始打印。我有什么想法可以避免这种情况吗?

此致

FileInputStream file = new FileInputStream(new File(filePath));
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder =  builderFactory.newDocumentBuilder();
        Document xmlDocument = builder.parse(file);
        XPath xPath = XPathFactory.newInstance().newXPath();

        disclaimer = xPath.compile(disclaimerPath + File.separator + "title").evaluate(xmlDocument);

            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer transformer = tf.newTransformer();
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            StringWriter writer = new StringWriter();
            transformer.transform(new DOMSource(xmlDocument), new StreamResult(writer));

            System.out.println(writer.getBuffer().toString().replaceAll("\n|\r", ""));

0 个答案:

没有答案