java - 如何使用 xquery 在 java 中处理非常大的 xml 文件,如 150mb

标签 java xml xquery

我正在使用 xquery 查询大型 xml 文档。使用xquery doc函数会不会导致内存堆出站? 如何在java中使用xquery查询一个大的xml文件。 示例说明将被应用。

最佳答案

首先,考虑到当今机器的强大功能,150 MB 并不算大。如果它增长到 GB,请考虑改为使用 Stax 或 SAX。

XPath/Xquery 资源使用将取决于实现,例如,在 Dom4J 的情况下,与 DOM 相比,XPath/Xquery 的资源占用通常要少得多,但这通常取决于各种其他因素,例如文档的长度(即您有多少“childNode”元素)以及您感兴趣的数据在文档中的位置。

从这里引用https://stackoverflow.com/a/725007/6785908

XPath memory usage and completion time tends to increase the further down the document you go. For example, let's say you have an XML document with 20,000 childNode elements, each childNode has a unique identifier that you know in advance, and you want to extract a known childNode from the document. Extracting the 18,345th childNode would use much, much, much more memory than extracting the 3rd.

So if you are using XPath to extract all childNode elements, you may find it less efficient than parsing into a DOM. XPath is generally an easy way of extracting a portion of an XML doucment. I'd not recommend using it for processing all of an XML document.

Spring Xquery 示例

https://github.com/spring-projects/spring-integration-extensions/tree/master/samples/xquery

使用 Java 的 Xquery 示例

这是我从第一个谷歌搜索结果中得到的 https://docs.oracle.com/database/121/ADXDK/adx_j_xqj.htm#ADXDK115

import javax.xml.xquery.XQConnection;
import javax.xml.xquery.XQException;
import javax.xml.xquery.XQPreparedExpression;
import javax.xml.xquery.XQSequence;

import oracle.xml.xquery.OXQDataSource;

public class HelloWorld {

    public static void main(String[] args) throws XQException {
        OXQDataSource ds = new OXQDataSource();
        XQConnection con = ds.getConnection();
        String query = "<hello-world>{1 + 1}</hello-world>";
        XQPreparedExpression expr = con.prepareExpression(query);
        XQSequence result = expr.executeQuery();

        // prints "<hello-world>2</hello-world>"
        System.out.println(result.getSequenceAsString(null));

        result.close();
        expr.close();
        con.close();
    }

} 

我想重申,对于 150 MB 大小的 xml 处理,您不必太担心内存占用。

关于java - 如何使用 xquery 在 java 中处理非常大的 xml 文件,如 150mb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44638384/

相关文章:

error-handling - 是否可以检索引发错误的命名空间?

sql-server - 用Transact-SQL替换所有记录中的多个XML属性值?

java - 我有一个具有多种方法的类文件,我可以从测试用例中调用该类吗

java - Java 的 WS-Discovery 实现

java - 检测何时连接/断开 Wi-Fi

java - 地理位置权限不足

java - 在java中编写自定义indexOf()方法

c# - 如何在 C# 中解析嵌套的 XML 节点

java - 使用 XSD 架构解析 XML 文件时出现 SAXException

google-chrome - 有没有办法在Chrome中运行XQuery?