Search code examples
javadom4j

Adding xml to DOMElement from String


I have a string with xml content, for example:

String str = "<history><message from="Alice" to="Bob" /></history>";

and I want to add this xml content to some element (org.dom4j.DOMElement). How to do it?


I've tried this:

myElement.addText(str);

But it gives me just escaped text content.


Solution

  • You need to use Dom4j to parse the XML fragment into a separate Document object, and then append the root element of that Document object to the appropriate place of your target Document.