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?
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));
}
}
I would like to shamelessly plug the small open-source library I have written to make parsing XML in Java a breeze.
Check out Jinq2XML.
http://code.google.com/p/jinq2xml/
Some sample code would look like:
Jocument joc = Jocument.load(urlOrStreamOrFileName);
joc.single("root").children().each(new Action() {
public void act(Jode j){
System.out.println(j.name() + ": " + j.value());
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With