哪个是java中最好的XML解析库 [英] Which is the best library for XML parsing in java

查看:24
本文介绍了哪个是java中最好的XML解析库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在搜索用于解析 XML(复杂配置和数据文件)的 Java 库,我用谷歌搜索了一下,但除了 dom4j(似乎他们正在开发 V2)之外找不到其他的.我已经查看了 commons配置但不喜欢它,XML 上的其他 apache 项目似乎处于休眠状态.我自己还没有评估过 dom4j,只是想知道 - java 是否有其他(好的)开源 xml 解析库?您对 dom4j 的体验如何?

I'm searching the java library for parsing XML (complex configuration and data files), I googled a bit but couldn't found other than dom4j (Seems like they are working on V2).. I have taken look at commons configuration but didn't like it, Other apache projects on XML seems under hibernation. I haven't evaluated dom4j by myself but just wanted to know - Does java has other (Good) open source xml parsing libraries? and how's your experience with dom4j?

在@Voo 的回答之后,让我再问一个 - 我应该使用 java 的内置类还是任何第三方库,如 dom4j.. 有什么优点?

After the @Voo's answer let me ask another one - Should I use java's built-in classes or any third-party library like dom4j.. What are the advantages?

推荐答案

实际上 Java 支持 4 种开箱即用的 XML 解析方法:

Actually Java supports 4 methods to parse XML out of the box:

DOM Parser/Builder:整个 XML 结构被加载到内存中,您可以使用众所周知的 DOM 方法来处理它.DOM 还允许您使用 Xslt 转换写入文档.示例:

DOM Parser/Builder: The whole XML structure is loaded into memory and you can use the well known DOM methods to work with it. DOM also allows you to write to the document with Xslt transformations. Example:

public static void parse() throws ParserConfigurationException, IOException, SAXException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);
    factory.setIgnoringElementContentWhitespace(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    File file = new File("test.xml");
    Document doc = builder.parse(file);
    // Do something with the document here.
}

SAX 解析器:仅用于读取 XML 文档.Sax 解析器遍历文档并调用用户的回调方法.有用于文档、元素等开始/结束的方法.它们在 org.xml.sax.ContentHandler 中定义,并且有一个空的帮助类 DefaultHandler.

SAX Parser: Solely to read a XML document. The Sax parser runs through the document and calls callback methods of the user. There are methods for start/end of a document, element and so on. They're defined in org.xml.sax.ContentHandler and there's an empty helper class DefaultHandler.

public static void parse() throws ParserConfigurationException, SAXException {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(true);
    SAXParser saxParser = factory.newSAXParser();
    File file = new File("test.xml");
    saxParser.parse(file, new ElementHandler());    // specify handler
}

StAx Reader/Writer:这适用于面向数据流的接口.程序在准备好时要求下一个元素,就像游标/迭代器一样.您还可以使用它创建文档.阅读文档:

StAx Reader/Writer: This works with a datastream oriented interface. The program asks for the next element when it's ready just like a cursor/iterator. You can also create documents with it. Read document:

public static void parse() throws XMLStreamException, IOException {
    try (FileInputStream fis = new FileInputStream("test.xml")) {
        XMLInputFactory xmlInFact = XMLInputFactory.newInstance();
        XMLStreamReader reader = xmlInFact.createXMLStreamReader(fis);
        while(reader.hasNext()) {
            reader.next(); // do something here
        }
    }
}

写文档:

public static void parse() throws XMLStreamException, IOException {
    try (FileOutputStream fos = new FileOutputStream("test.xml")){
        XMLOutputFactory xmlOutFact = XMLOutputFactory.newInstance();
        XMLStreamWriter writer = xmlOutFact.createXMLStreamWriter(fos);
        writer.writeStartDocument();
        writer.writeStartElement("test");
        // write stuff
        writer.writeEndElement();
    }
}

JAXB:读取 XML 文档的最新实现:是 Java 6 v2 的一部分.这允许我们从文档中序列化 java 对象.您使用一个实现 javax.xml.bind.Unmarshaller 接口的类来阅读文档(您可以从 JAXBContext.newInstance 获得一个用于此的类).上下文必须使用使用的类进行初始化,但您只需要指定根类,而不必担心静态引用的类.您使用注释来指定哪些类应该是元素(@XmlRootElement),哪些字段应该是元素(@XmlElement)或属性(@XmlAttribute,真是个惊喜!)

JAXB: The newest implementation to read XML documents: Is part of Java 6 in v2. This allows us to serialize java objects from a document. You read the document with a class that implements a interface to javax.xml.bind.Unmarshaller (you get a class for this from JAXBContext.newInstance). The context has to be initialized with the used classes, but you just have to specify the root classes and don't have to worry about static referenced classes. You use annotations to specify which classes should be elements (@XmlRootElement) and which fields are elements(@XmlElement) or attributes (@XmlAttribute, what a surprise!)

public static void parse() throws JAXBException, IOException {
    try (FileInputStream adrFile = new FileInputStream("test")) {
        JAXBContext ctx = JAXBContext.newInstance(RootElementClass.class);
        Unmarshaller um = ctx.createUnmarshaller();
        RootElementClass rootElement = (RootElementClass) um.unmarshal(adrFile);
    }
}

写文档:

public static void parse(RootElementClass out) throws IOException, JAXBException {
    try (FileOutputStream adrFile = new FileOutputStream("test.xml")) {
        JAXBContext ctx = JAXBContext.newInstance(RootElementClass.class);
        Marshaller ma = ctx.createMarshaller();
        ma.marshal(out, adrFile);
    }
}

从一些旧的演讲幻灯片中无耻地复制的例子;-)

Examples shamelessly copied from some old lecture slides ;-)

关于我应该使用哪个 API?".嗯,这取决于 - 并非所有 API 都具有与您看到的相同的功能,但是如果您可以控制用于映射 XML 文档的类 JAXB 是我个人最喜欢的,非常优雅和简单的解决方案(尽管我还没有将它用于非常大的文档,它可能会变得有点复杂).SAX 也非常易于使用,如果您没有充分的理由使用它,请远离 DOM - 在我看来,旧的、笨拙的 API.我认为没有任何现代 3rd 方库具有 STL 所缺少的任何特别有用的功能,而标准库通常具有经过充分测试、记录和稳定的优点.

About "which API should I use?". Well it depends - not all APIs have the same capabilities as you see, but if you have control over the classes you use to map the XML document JAXB is my personal favorite, really elegant and simple solution (though I haven't used it for really large documents, it could get a bit complex). SAX is pretty easy to use too and just stay away from DOM if you don't have a really good reason to use it - old, clunky API in my opinion. I don't think there are any modern 3rd party libraries that feature anything especially useful that's missing from the STL and the standard libraries have the usual advantages of being extremely well tested, documented and stable.

这篇关于哪个是java中最好的XML解析库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆