本文介绍了验证一个巨大的 XML 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到一种方法来针对 XSD 验证大型 XML 文件.我看到了问题 ...best way to validate an XML... 但答案都指向使用 Xerces 库进行验证.唯一的问题是,当我使用该库来验证 180 MB 文件时,我得到了 OutOfMemoryException.

I'm trying to find a way to validate a large XML file against an XSD. I saw the question ...best way to validate an XML... but the answers all pointed to using the Xerces library for validation. The only problem is, when I use that library to validate a 180 MB file then I get an OutOfMemoryException.

是否有任何其他工具、库、策略来验证比普通 XML 文件更大的文件?

Are there any other tools,libraries, strategies for validating a larger than normal XML file?

SAX 解决方案适用于 java 验证,但 libxml 工具的其他两个建议对于 java 之外的验证也非常有帮助.

The SAX solution worked for java validation, but the other two suggestions for the libxml tool were very helpful as well for validation outside of java.

推荐答案

不要使用 DOMParser,而是使用 SAXParser.这从输入流或读取器中读取,因此您可以将 XML 保存在磁盘上,而不是将其全部加载到内存中.

Instead of using a DOMParser, use a SAXParser. This reads from an input stream or reader so you can keep the XML on disk instead of loading it all into memory.

SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(true);
factory.setNamespaceAware(true);

SAXParser parser = factory.newSAXParser();

XMLReader reader = parser.getXMLReader();
reader.setErrorHandler(new SimpleErrorHandler());
reader.parse(new InputSource(new FileReader ("document.xml")));

这篇关于验证一个巨大的 XML 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 19:22