简体   繁体   English

哪个Java XML Parser可以进行简单处理?

[英]Which Java XML Parser for simple processing?

I am producing some XML in a java application. 我在java应用程序中生成一些XML。 I am looking at the variety of XML parsing options. 我正在研究各种XML解析选项。 I am not intending to do anything more than traverse the structure and extract values out of it. 我不打算做任何事情,而不是遍历结构并从中提取价值。 I need to use one of them that is built into the Java API (1.5+) without any additional plugins. 我需要使用内置于Java API(1.5+)中的其中一个而不需要任何额外的插件。 I don't need to create "events" or transform it into anything else. 我不需要创建“事件”或将其转换为其他任何内容。 I am not producing XML, merely reading and extracting data. 我不是在生成XML,只是读取和提取数据。 I am not enforcing a schema either. 我也没有强制执行架构。

Sun provide a list here, but it's not really obvious what I should use. Sun在这里提供了一个列表,但是我应该使用它并不是很明显。

http://java.sun.com/developer/technicalArticles/xml/JavaTechandXML/ http://java.sun.com/developer/technicalArticles/xml/JavaTechandXML/

What would be the most appropriate XML API to use in this case ? 在这种情况下,最合适的XML API是什么? JAXP ? JAXP? JDom ? JDom? XPath ? XPath?

I think using a DOM parser to parse the XML and load it into memory in a Document sounds sufficient for your needs. 我认为使用DOM解析器来解析XML并将其加载到Document中的内存中听起来足以满足您的需求。

You wouldn't use XPath in that case, just the Document API. 在这种情况下,您不会使用XPath,只需使用Document API。

JAXP is just a synonym for the XML parsing technology build into the JDK. JAXP只是构建到JDK中的XML解析技术的同义词。 The term JAXP (P is for Parsing) distinguishes it from JAXB (B is for binding). 术语JAXP(P用于解析)将其与JAXB(B用于绑定)区分开来。

Some 3rd party libraries built on top of DOM might make your life easier. 在DOM之上构建的一些第三方库可能会让您的生活更轻松。 Think about JDOM or DOM4J. 想想JDOM或DOM4J。

The most classical way of doing things in IMO would be combination of JAXP and XPath. 在IMO中最经典的做事方式是JAXP和XPath的组合。 Java 5.0 includes JAXP 1.3 and this is standard stuff. Java 5.0包含JAXP 1.3,这是标准的东西。 Please see this answer to a similar question for a minimalist coding sample. 对于极简主义编码样本,请参阅类似问题的 答案

Using the standard DOM Parser is good enough for your purpose. 使用标准DOM Parser足以满足您的需求。 Try out this example . 试试这个例子

I think that the most practical tool to use is XStream, from ThoughtWorks. 我认为最实用的工具是来自ThoughtWorks的XStream。 Some modern mvc frameworks like VRaptor use it to serve and consume xml. 像VRaptor这样的一些现代mvc框架使用它来服务和使用xml。 Take a look at: http://x-stream.github.io/ 看看: http//x-stream.github.io/

DOM parser is what you looking for i think. 我认为DOM解析器是你想要的。 easy to implement it and it has fast searching node capability 易于实现,并具有快速搜索节点功能

As the parsing strategy you can use either DOM strategy which has the advantage that the hole document is kept in memory and you can access it via xpath. 作为解析策略,您可以使用DOM策略,其优点是将孔文档保存在内存中,您可以通过xpath访问它。 i recommend this if you have small xml documents or if you really NEED all the data to be present and accessable all the time because this consumes a lot of heap space. 如果您有小型xml文档或者您确实需要所有数据一直存在且可访问,我建议这样做,因为这会消耗大量的堆空间。

if you have bigger documents or if you dont need to access the all the time you should either use the SAX method or the Stax method (xml pull parsing) if this is available in your java distribution. 如果您有更大的文档或者您不需要一直访问,那么您应该使用SAX方法或Stax方法(xml pull解析),如果这在您的Java发行版中可用。 These methods are event based. 这些方法是基于事件的。 so they traverse through the xml tree and make a kind of callback to a class defined by you. 所以它们遍历xml树并对你定义的类进行回调。 so you can react on events like "element xy starts" "element xy ends" 所以你可以对“元素xy开始”,“元素xy结束”等事件做出反应

XOM . XOM

Use xpath. 使用xpath。

如果它非常简单 - 在SAX解析器中执行它。

It seems that SAX is the API you want. 似乎SAX是您想要的API。

Google "SAX Parsing" and you will find many examples. 谷歌“SAX解析”,你会发现很多例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM