Convert String XML fragment to Document Node in Java

java, string, xml

Solution

Element node =  DocumentBuilderFactory
    .newInstance()
    .newDocumentBuilder()
    .parse(new ByteArrayInputStream("<node>value</node>".getBytes()))
    .getDocumentElement();

Problem

In Java how can you convert a String that represents a fragment of XML for insertion into an XML document? e.g. ``` String newNode = "<node>value</node>"; // Convert this to XML ``` Then insert this node into an org.w3c.dom.Document as the child of a given node?

Original source

Related problems