What is the simplest and minimalistic java xml api?

前端 未结 7 703
我在风中等你
我在风中等你 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 07:01

    Dom4J rocks. It's very easy and understandable

    Sample Code:

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

    Output:

    phleem
    gumbo
    phleem
    gumbo

提交回复
热议问题