Reputation: 14645
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, XpathExpressionFactory, NodeList and so on. So in the light of native xml support in languages such as groovy/scala I have a question. Is there are minimalistic java XML IO framework?
PS XStream/JAxB good for serialization/deserialization, but in this case I'm looking for streaming some data in XML with XPath for example.
Upvotes: 2
Views: 287
Reputation: 22847
NanoXML is very small, below 50kb. I've found this today and I'm really impressed.
Upvotes: 0
Reputation: 758
try VTD-XML. Its almost 3 to 4 times faster than DOM parsers with outstanding memory footprint.
Upvotes: 1
Reputation: 163262
JDOM and XOM are probably the simplest. DOM4J is more powerful but more complex. DOM is just horrible. Processing XML in Java will always be more complex than processing JSON, because JSON was designed for structured data while XML was designed for documents, and documents are more complex than structured data. Why not use a language that was designed for XML instead, specifically XSLT or XQuery?
Upvotes: 0
Reputation: 298818
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
Upvotes: 1
Reputation: 403441
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.
Upvotes: 3
Reputation: 8278
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.
Upvotes: 0
Reputation: 2131
What about StAX? With Java 6 you don't even need additional libs.
Upvotes: 2