What is the simplest and minimalistic java xml api?

前端 未结 7 649
我在风中等你
我在风中等你 2021-01-07 06:12

There are many pretty good json libs lika GSon. But for XML I know only Xerces/JDOM and both have tedious API. I don\'t like to use unnecessary objects like DocumentFactory,

相关标签:
7条回答
  • 2021-01-07 06:38

    The W3C DOM model is unpleasant and cumbersome, I agree. JDOM is already pretty simple. The only other DOM API that I'm aware of that is simpler is XOM.

    0 讨论(0)
  • 2021-01-07 06:38

    What about StAX? With Java 6 you don't even need additional libs.

    0 讨论(0)
  • 2021-01-07 06:43

    Deppends on how complex your java objects are: are they self-containing etc (like graph nodes). If your objects are simple, you can use Google gson - it is the simpliest API(IMO). In Xstream things start get messy when you need to debug.Also you need to be carefull when you choose an aprpriate Driver for XStream.

    0 讨论(0)
  • 2021-01-07 06:44

    try VTD-XML. Its almost 3 to 4 times faster than DOM parsers with outstanding memory footprint.

    0 讨论(0)
  • 2021-01-07 06:52

    NanoXML is very small, below 50kb. I've found this today and I'm really impressed.

    0 讨论(0)
  • 2021-01-07 07:01

    Dom4J rocks. It's very easy and understandable

    Sample Code:

    public static void main(String[] args) throws Exception {
        final String xml = "<root><foo><bar><baz name=\"phleem\" />"
                         + "<baz name=\"gumbo\" /></bar></foo></root>";
    
        Document document = DocumentHelper.parseText(xml);
    
        // simple collection views
        for (Element element : (List<Element>) document
                .getRootElement()
                .element("foo")
                .element("bar")
                .elements("baz")) {
            System.out.println(element.attributeValue("name"));
        }
    
        // and easy xpath support
        List<Element> elements2 = (List<Element>)
            document.createXPath("//baz").evaluate(document);
        for (final Element element : elements2) {
            System.out.println(element.attributeValue("name"));
        }
    }
    

    Output:

    phleem
    gumbo
    phleem
    gumbo

    0 讨论(0)
提交回复
热议问题