简体   繁体   English

Java中高效的XML解析| 与Java中的C#XmlDocument等效

[英]Efficient XML parsing in Java | Equivalent of C# XmlDocument in Java

Below is my XML structure 下面是我的XML结构

<values>
<inputs>
 <input>one</input>
 <input>two</input>
</inputs>
<inputs>
 <input>one</input>
 <input>three</input>
</inputs>
</values>

GOAL : Want to put all input node values into a collection 目标:希望将所有输​​入节点值放入集合中

I can write SAX/DOM parser, read based on the node name and put each value into the collection. 我可以编写SAX / DOM解析器,根据节点名称进行读取,并将每个值放入集合中。

Is that the most efficient way? 那是最有效的方法吗?

Could something similar to XmlDocument in c# be used? 可以使用类似于C#中的XmlDocument的东西吗?

Thank you :) 谢谢 :)

By default these days. 这些天默认为。 I use Stax (Streaming API for XML) http://en.wikipedia.org/wiki/StAX 我使用Stax(用于XML的流API) http://en.wikipedia.org/wiki/StAX

Stax parsing is nice and efficient, but its not very pleasant to use. Stax解析是很好且高效的方法,但使用起来并不令人满意。

To iterate over an XML structure you can use techniques like the code below... 要遍历XML结构,可以使用下面的代码等技术。

XMLEventReader reader = factory.createXMLEventReader(in);

while(reader.hasNext()) {
    XMLEvent e = reader.nextEvent();
    ... 
}

but the real strength with Stax parsing comes when you can be certain of what the XML structure is like and you don't need to guess what the next event will be (ie when you know the XML conforms to an XSD). 但是Stax解析的真正优势在于可以确定XML结构是什么样,而不必猜测下一个事件是什么(即,当您知道XML符合XSD时)。

Try using JAXB. 尝试使用JAXB。 If you want really scalable stuff, use the listener functionality of JAXB (before/after unmarshall) and team this up with a SAX Parser as the content handler. 如果您真的想要可扩展的东西,请使用JAXB的侦听器功能(解组之前/之后),并将其与SAX解析器组合为内容处理程序。 This will allow your XML to be as big as you want without chewing up memory. 这将使您的XML可以随心所欲地变大而不会占用内存。 It just streams through a stream. 它只是通过一条流。

Something like this: 像这样:

JAXBContext jc = ...
Unmarshaller u = jc.createUnmarshaller();
u.setListener(new Unmarshaller.Listener() {
    @Override
    public void beforeUnmarshal(Object target, Object parent) {
        if (target instanceof MyObj) {
            ...
        }
    }

    public void afterUnmarshal(Object target, Object parent) {
        if (target instanceof MyObj) {
            ...
        }
    }
};
BufferedInputStream stream = new BufferedInputStream(inputStream);

SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
XMLReader reader = factory.newSAXParser().getXMLReader();
reader.setContentHandler(u.getUnmarshallerHandler());
reader.parse(new InputSource(stream));

//NOTE THIS CODE IS VERY ROUGH AND WONT COMPILE, BUT YOU SHOULD GET THE GIST

Yes. 是。

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

are the classes you need. 是您需要的课程。

Here is a quick tutorial . 这是一个快速教程

But let's get it straight. 但是,让我们弄清楚。 SAX based parser is more efficient :) XmlDocument type based parsing is more .... convinient . 基于SAX的解析器efficient :)基于XmlDocument类型的解析更加.... convinient :) :)

depending on xml size, you could use also Castor 根据xml大小,您也可以使用Castor
From XSD you can generate mapping classes and when you invoke Castor's Unmarshal 从XSD可以生成映射类,并且在调用Castor的Unmarshal时
it will generate a complex object based on these classes filled with xml content. 它将根据这些填充了xml内容的类生成一个复杂的对象。

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

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