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

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

问题描述

我正在尝试迁移我的一个类,它使用 DOM 解析大量 XPath 表达式到 SAX 解析。 DOM 解析对我有好处,但我尝试解析的一些文件太大,导致服务器超时。我想重新使用 XPath SAX 解析,但我不知道是否可能,如果不可能你可以帮助我,因为我不知道如何使用以下代码,当我只使用 SAX

 文档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元素时,它只会触发事件。你必须保持内存中的上下文(通常是堆栈的父元素)知道你在树中的位置。



既然你不会有树在内存中,您将无法使用XPath。您只能测试当前的上下文(您的manuallay托管堆栈)来查询您的文档。请注意,SAX解析器只会在您的文件上运行一次,因此文件中的顺序很重要。



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


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");

解决方案

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.

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.

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天全站免登陆