简体   繁体   English

在JAVA中解析XML元素

[英]Parsing XML Elements in JAVA

  1. I want to parse XML in java. 我想解析Java中的XML。 It will be DOM or SAX. 它将是DOM或SAX。 Read in a book JAXP is good one. 读一本书JAXP是一本好书。 Also when i google out found XERCES/XALAN. 另外,当我用谷歌搜索出XERCES / XALAN时。 Which parser is commonly used? 常用的解析器是?

  2. In SAX Parser if i register for a single element event will the SAX parse stops processing the XML message after encountering the element. 在SAX解析器中,如果我注册一个元素事件,则SAX解析在遇到元素后将停止处理XML消息。 Read in a book DOM reads the entire XML and loads into memory even if i want to know single element value. 读一本书即使我想知道单个元素的值,DOM也会读取整个XML并将其加载到内存中。

I want to parse XML in java. 我想解析Java中的XML。 It will be DOM or SAX. 它将是DOM或SAX。 Read in a book JAXP is good one. 读一本书JAXP是一本好书。 Also when i google out found XERCES/XALAN. 另外,当我用谷歌搜索出XERCES / XALAN时。 Which parser is commonly used? 常用的解析器是?

Xerces is an implementation of both DOM and SAX, and it is built into the JDK. Xerces是DOM和SAX的实现,并且内置在JDK中。 See javax.xml.parsers . 请参阅javax.xml.parsers

In SAX Parser if i register for a single element event will the SAX parse stops processing the XML message after encountering the element. 在SAX解析器中,如果我注册一个元素事件,则SAX解析在遇到元素后将停止处理XML消息。

No. 没有。

Read in a book DOM reads the entire XML and loads into memory even if i want to know single element value. 读一本书即使我想知道单个元素的值,DOM也会读取整个XML并将其加载到内存中。

Yes. 是。

  1. All of the above parsers you mention are excellent. 您提到的所有上述解析器都很出色。 My personal preference would be XERCES if the application did a lot of XML processing, otherwise, the "built in" parsers are more than good enough. 如果应用程序进行了大量XML处理,我个人比较喜欢XERCES,否则,“内置”解析器已经足够了。

  2. You will need to process every event from the SAX parser and ignore the ones you are not interested in. You can stop parsing at any point by "destroying" the parser object. 您将需要处理SAX解析器中的每个事件,并忽略您不感兴趣的事件。您可以随时通过“销毁”解析器对象来停止解析。 If you are only interested in one or two elements of a large message then SAX is the way to go. 如果您只对大型邮件的一个或两个元素感兴趣,那么SAX是您的最佳选择。 If you are interested in all or most of the elements then use the DOM parser, you take a slight performance hit, but, the "give me what I want" API makes for much clearer code than the "take what I give you" SAX API. 如果您对所有或大部分元素都感兴趣,则使用DOM解析器,会对性能造成轻微影响,但是“给我想要的东西” API比“给我想要的东西” SAX使代码更清晰API。

I'm not sure I grasp all the details of what you want to accomplish but if you have xsd files describing the XML format I'd say JAXB is the way to go. 我不确定我是否掌握了要完成的所有细节,但是如果您有描述XML格式的xsd文件,我会说JAXB是必经之路。 It'll take you xsd and generate classes for you all auto-magically. 它会带您xsd并为您自动神奇地生成类。 Then you can easily go back and forth - marshal and unmarshal data. 然后,您可以轻松地往返-元数据和非元数据。 I was very impressed by the level of automation in Netbeans recently when I tackled a similar task 最近,当我处理类似任务时,Netbeans的自动化水平给我留下了深刻的印象

Hope my suggestion to look into JAXB is helpful. 希望我对研究JAXB的建议有所帮助。

For simple XML to POJO, I've found XStream to be the least annoying of all parsers in Java: 对于到POJO的简单XML,我发现XStream是Java中所有解析器中最讨厌的:

http://x-stream.github.io/ http://x-stream.github.io/

DOM and SAX are interfaces, Xerces is the most popular implementation of these interfaces. DOM和SAX是接口,Xerces是这些接口的最流行的实现。 There are in fact two versions of Xerces: the one from Apache, and the one built into the Sun/Oracle JDK. 实际上,有两种版本的Xerces:一种来自Apache,另一种内置于Sun / Oracle JDK中。 The one in the JDK is buggy; JDK中的一个是越野车; use the one from Apache in preference. 优先使用Apache中的一个。

Since you are just starting out, it is probably worth looking more widely than DOM and SAX. 由于您才刚刚起步,因此可能值得比DOM和SAX更广泛地看。 SAX is a very low-level interface, offering good performance but little functionality: you can expect to write rather a lot of complex code in your application if this is the way you go. SAX是一个非常底层的接口,提供了良好的性能,但功能却很少:如果这样做,您可以期望在应用程序中编写大量复杂的代码。 DOM presents you with a tree model of XML, but there are much better and simpler interfaces that do the same thing: JDOM offers everything you need and is much easier to use than DOM. DOM为您提供了XML的树模型,但是有更好,更简单的接口可以完成相同的工作:JDOM提供了您所需的一切,并且比DOM易于使用。

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

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