tags:

views:

200

answers:

7

There are a lot of questions that ask the best XML parser, I am more interested in what is the XML parser that is the most like Groovy for Java?

I want:

SomeApiDefinedObject o = parseXml( xml );
for( SomeApiDefinedObject it : o.getChildren() ) {
   System.out.println( it.getAttributes() );
}

The most important things are that I don't want to create a class for every type of XML node, I'd rather just deal with them all as strings, and that building the XML doesn't require any converters or anything, just a simple object that is already defined

If you have used the Groovy XML parser, you will know what I'm talking about

Alternatively, would it be better for me to just use Groovy from Java?

A: 

I highly recommend JAXB. Great for XML <--> Java objects framework.

duduamar
A: 

There used to be a very small and simple XML parser called NanoXML. It seems not to be developed anymore, but it's still available at http://devkix.com/nanoxml.php

fish
A: 
Carl Smotricz
So it's exactly what he does *not* want...
Michael Borgwardt
A: 

I work with Dozer and Castor for getting OTOM (Object to Object Mapping).

Kartik
+3  A: 

Here is something quick you can do with Sun Java Streaming XML Parser

    FileInputStream xmlStream = new FileInputStream(new File("myxml.xml"));
    XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(xmlStream);
    while(reader.hasNext()){
        reader.next();
        for(int i=0; i < reader.getAttributeCount(); i++) {
           System.out.println(reader.getAttributeName(i) + "=" + reader.getAttributeValue(i));
        }
    }
ring bearer
Also, you can not get terse syntax like Groovy in Java. So either use a simpler API or use Groovy iself.
This is useful, but not exactly what I want, the problem is the XML I will be getting back is complicated, but I only need a small portion of it... so I don't want to have to mirror the XML heiarchy in java code, I want it to do that magically, perhaps using maps with names
walnutmon
@jboyd: actually, this is one of the few answers here where you do in fact *not* have to mirror the XML hierarchy in Java code. Mine (and Brian's unexplainably deleted one) are the others. These work pretty exactly like your "I want" code.
Michael Borgwardt
Yes, this will do, I'm not quite sure how well I will be able to traverse complicated XML though, reader.next() seems way too vague because XML is not linear
walnutmon
+1  A: 

Looks like all you want is a simple DOM API, such as provided by dom4j. There actually already a DOM API in the Standard Library (the org.w3c.dom packages), but it's only the API, so you need a separate implementation - might as well use something a little more advanced like dom4j.

Michael Borgwardt
+1  A: 

Use Groovy.

It seems that your primary goal is to be able to access the DOM in a "natural" way via object accessors, and Java won't let you do this without defining classes. Groovy, because it is "duck typed," will allow you to do this.

The only reason not to use Groovy is if (1) XML processing is a very small part of your application, and/or (2) you have to work with other people who may want to program strictly in Java.

Whatever you do, do not decide to "just deal with them all as strings." XML is not a simple format, and unless you know the spec inside and out, you're not likely to get it right. Which means that your XML will be rejected by spec-conformant parsers.

Anon