Wednesday, March 28, 2007

Retrieving partially XML parts from a Document using JDOM

It is just a small tip. I love JDOM, I find it easy and simple so whenever I have to parse an XML document in a DOM manner - JDOM is my API of choice. There was this time that while I was browsing through programmatically an XML document I want to acquire partially a part of the XML and return it as String. No Elements , no Attributes neither values. I want a part of the XML as it is. I had a bunch of elements - a complex tree mapped as an XML I wanted as a String an Element and all its children inside it.The easiest solutions is given with the XMLOutputter.Here is a sample.
Element root = d.getRootElement();
Element kid = d.getChild("Kid");
XMLOutputter outputter = new XMLOutputter();
String xmlpart=outputter.outputString(kid);

The String xmlpart is outputed as it is in the actual XML document- as XML!

Nice and simple!


1 comment:

  1. have you looked at vtd-xml, which is the latest and more advanced/powerful XML Processing API

    vtd-xml

    ReplyDelete