How to get node content of XML with Dom4j in java

Swordsfrog picture Swordsfrog · Nov 29, 2011 · Viewed 12.7k times · Source

I have a XML file like:

<description>
  <text>blahblah</text>
  <code>code</code>
  <text>blah</text>
</description>

I've navigated to the node description, and I want to read the full content including the <text> and so on.

I've used the getText(), but it returned empty string.
I've used the getStringValue(), but it filtered all <text>.
I've used the asXML(), the result is close, but the result contains <description> which I don't want.

Is there a method to get the XML content of a element?

Answer

Qwerky picture Qwerky · Nov 29, 2011

Something like this:

public static void main(String[] args) throws DocumentException {
  String xml = "<description><text>blahblah</text><code>code</code><text>blah</text></description>";
  SAXReader reader = new SAXReader();
  Document doc = reader.read(new StringReader(xml));
  Element description = doc.getRootElement();
  String content = getContent(description);
  System.out.println(content);
}

private static String getContent(Element element) {
  StringBuilder builder = new StringBuilder();
  for (Iterator<Element> i = element.elementIterator(); i.hasNext();) {
    Element e = i.next();
    builder.append(e.asXML());
  }
  return builder.toString();
}

Note that if the element has text content itself, this won't return the text content, only the child nodes.