简体   繁体   English

我应该使用什么xml解析器?

[英]What xml parser should I use?

I have a somewhat large file (~500KiB) with a lot of small elements (~3000). 我有一个较大的文件(〜500KiB),其中包含许多小元素(〜3000)。 I want to pick one element out of this and parse it to a java class. 我想从中选择一个元素并将其解析为java类。

Attributes Simplified 简化 属性

<xml>     
<attributes>
  <attribute>
     <id>4</id>
     <name>Test</id>
  </attribute>

  <attribute>
     <id>5</id>
     <name>Test2</name>
  </attribute>

<!--3000 more go here-->
</attributes>

class Simplified 简化

public class Attribute{
  private int id;
  private String name;

  //Mutators and accessors

}

I kinda like XPath, but people suggested Stax and even VDT-XML. 我有点喜欢XPath,但是人们建议使用Stax甚至VDT-XML。 What should I do. 我该怎么办。

500 kb is not that large. 500 kb并不大。 If you like XPath, go for it. 如果您喜欢XPath,那就去做吧。

I kinda like XPath, but people suggested Stax and even VDT-XML. 我有点喜欢XPath,但是人们建议使用Stax甚至VDT-XML。 What should I do. 我该怎么办。

DOM, SAX and VTD-XML are all three different ways to parse a XML document. DOM,SAX和VTD-XML都是解析XML文档的三种不同方式。 Roughly in this order of memory efficiency. 大致按此顺序的内存效率。 DOM needs over 5 times of memory as XML file big is. DOM需要的内存是XML文件的5倍以上。 SAX is only a bit more efficient, VTD-XML uses only a little more memory than the XML file big is, about 1.2 times. SAX的效率更高,VTD-XML仅使用比XML文件大一点的内存,大约是它的1.2倍。

XPath is just a way to select elements and/or data from a (parsed) XML document. XPath只是从(已解​​析的)XML文档中选择元素和/或数据的一种方式。

With other words, you can just use XPath in combination with any of the XML parsers. 换句话说,您可以将XPath与任何XML解析器结合使用。 So this is after all a non-concern. 因此,这毕竟是无关紧要的。 If you just want to go for best memory efficiency and performance, go for VTD-XML. 如果只想获得最佳的内存效率和性能,请使用VTD-XML。

Avoid anything that is a DOM parser - no need for that, especially with a large-ish file and relatively simple XML syntax. 避免使用任何一种DOM解析器-无需这样做,尤其是对于大型文件和相对简单的XML语法而言。

Which specific one to use, sorry, I haven't used them, so I can't give you any more guidance than to look at your licensing, performance, and support (for questions). 抱歉,我没有使用过哪个特定的软件,因此除了查看您的许可,性能和支持(如有疑问)之外,我无法给您更多指导。

我最喜欢的XML库是Dom4j

I have commented above as well, because there are few options to consider - but by the sound of it your initial description I think you could get away with a simple SAX processor here: which will probably run faster (although it might not look as pretty when it comes to mapping the Java class) than other mechanisms: 我在上面也进行了评论,因为没有太多要考虑的选项-但是从您的初步描述来看,我认为您可以在这里使用一个简单的SAX处理器:它可能会运行得更快(尽管它看起来可能不那么漂亮)映射Java类时),而不是其他机制:

There is an example here, which matches quite closely with your example: 这里有一个示例,与您的示例非常匹配:

http://www.informit.com/articles/article.aspx?p=26351&seqNum=6 http://www.informit.com/articles/article.aspx?p=26351&seqNum=6

Whenever I have to deal with XML I just use XMLBeans . 每当我必须处理XML时,我就只使用XMLBeans It may be overkill for what you are after, but it makes life easy (once you know how to use it). 它可能对您所追求的东西造成过大杀伤力,但它使生活变得轻松(一旦您知道如何使用它)。

如果您根本不关心性能,那么Apache Digester可能对您有用,因为它在定义规则后已经为您初始化了Java对象。

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

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