SAX 解析器可以在 Java 中使用 XPath 吗? [英] Can SAX Parsers use XPath in Java?

查看:33
本文介绍了SAX 解析器可以在 Java 中使用 XPath 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将使用 DOM 解析和大量 XPath 表达式的类之一迁移到 SAX 解析.DOM 解析对我来说很好,但是我尝试解析的一些文件太大了,它们会导致服务器超时.我想重用 XPathSAX 解析,但我不确定这是否可能,如果不可能,请帮助我,因为我不知道如何以下代码将在我仅使用 SAX 时:

I'm trying to migrate one of my classes which uses DOM parsing with lots of XPath expressions to SAX parsing. DOM Parsing was good for me but some of the files i try to parse are too big and they cause server timeouts. I want to reuse the XPath with the SAX parsing but i'm not sure if it is possible and if not possible could you please help me because i have no idea how the following code will be when i use only SAX:

Document doc = bpsXml.getDocument();
String supplierName = BPSXMLUtils.getXpathString(doc, "/Invoice/InvoiceHeader/Party[@stdValue='SU']/Name/Name1");
String language = BPSXMLUtils.getXpathString(doc, "/Invoice/InvoiceHeader/InvoiceLanguage/@stdValue");

推荐答案

仅使用 SAX 解析器不会在内存中构建 XML 树的表示(这就是 SAX 内存效率更高的原因).它只会在遇到新的 XML 元素时触发事件".您必须将上下文(通常是一堆父元素)保留在内存中以知道"您在树中的位置.

Simply using a SAX parser will not build a representation of your XML tree in memory (this is why SAX is more memory-efficient). It will only trigger "events" whenever a new XML element is encountered. You will have to keep the context (often a stack of parent elements) in memory to "know" where you are in the tree.

由于您的内存中没有树,因此您将无法使用 XPath.您只能测试当前的上下文"(您的手动托管堆栈)来查询您的文档.请记住,SAX 解析器只会对您的文件执行一次,因此文件中的顺序很重要.

Since you will not have a tree in memory, you will not be able to use XPath. You can only test for the current "context" (your manuallay managed stack) to query your document.Remember that the SAX parser will only do one run on your file, so order in the file is important.

幸运的是,还有其他方法,例如 VTD-XML,这是一个构建 XML 树的库在内存中,但只有结构部分,它不会从文件中提取实际内容,根据需要提取内容.它比 DOM 解析器的内存效率高得多,同时仍然允许 XPath.我个人在工作中使用这个库来解析大约 700MB 的 XML 文件和 XPath(是的,这很疯狂,但它有效,而且速度非常快.)

Fortunately, there are other approach like VTD-XML which is a library that build the XML tree in memory, but only the structure part, it does not extract the actual content from the file, the content is extracted as-needed. It is much more memory efficient than a DOM parser while still allowing XPath. I personnaly use this library at work to parse ~700MB XML files with XPath (yes that's insane but it works and it is very fast.)

这篇关于SAX 解析器可以在 Java 中使用 XPath 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆