1. Introduction

XML processing is a common requirement in Java, especially when dealing with data interchange, configuration files, or web services. Besides, converting a string that contains an XML fragment into a Document node allows us to manipulate the XML structure using DOM (Document Object Model) APIs.

This tutorial explores different methods to convert a string containing an XML fragment into a Document node using Java.

2. Converting String XML Fragment to Document Node

To manipulate an XML string in Java, we must first parse it into a Document object. Besides, the DocumentBuilder class from the javax.xml.parsers package allows us to do this efficiently.

Consider the following XML string fragment:

String xmlString = "<root><child>Example</child></root>";

To convert this string into a Document, we utilize the DocumentBuilder to parse the string:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new InputSource(new StringReader(xmlString)));

In this code snippet, we create an instance of DocumentBuilderFactory and initialize a DocumentBuilder. The XML string is wrapped inside an InputSource using StringReader. This enables the builder to parse the string and produce a Document object.

The parsed document now contains the XML structure from the string, and we can manipulate it as needed. To verify the content of the parsed document, we can access its root element and its child node:

assertNotNull(document);
Element rootElement = document.getDocumentElement();
assertEquals("root", rootElement.getNodeName());

First, we ensure that the parsed Document isn’t null and assert that the root element’s name is root. Next, we verify that the child element is read correctly:

var childElements = rootElement.getElementsByTagName("child");

assertNotNull(childElements);
assertEquals(1, childElements.getLength());
assertEquals("Example", childElements.item(0).getTextContent());

This ensures that the document’s root node equals child and its text content equals Example.

3. Inserting the Document Node into an Existing Document

Once we’ve parsed the new XML fragment into a Document, we can add it to an existing XML structure. To achieve this, we first import the node into the existing document before appending it to the desired location.

Let’s begin with an existing XML document:

Document existingDocument = builder.newDocument();
Element rootElement = existingDocument.createElement("existingRoot");
existingDocument.appendChild(rootElement);

This creates a new XML document with a root element called existingRoot. Now, we parse the XML string fragment that we want to add to this document:

String xmlString = "<child>Example</child>";
Document newDocument = builder.parse(new InputSource(new StringReader(xmlString)));

In this case, the XML string Example is converted into a DOM structure that contains a child node. To add this node to the existing document, we must import the node first:

Element newNode = (Element) existingDocument.importNode(newDocument.getDocumentElement(), true);

The importNode() method copies the node from the parsed newDocument into the existingDocument. This step is necessary because we can’t directly append a node from one document to another without importing it first. Moreover, the true flag indicates that the entire subtree (all child elements) will be imported.

Finally, we append the imported node to the root element of the existing document:

existingDocument.getDocumentElement().appendChild(newNode);

This operation adds the child node to the root existingRoot node. To ensure the node has been successfully appended, we can validate the structure by checking the number of child nodes in the root element:

assertEquals(1, existingDocument.getDocumentElement().getChildNodes().getLength());
assertEquals("child", existingDocument.getDocumentElement().getChildNodes().item(0).getNodeName());

Here, we verify that the root element contains exactly one child node and that the name of this child node is a child.

4. Handling Invalid XML Strings

When working with XML, we may encounter situations where the input string is not well-formed or invalid. In such cases, handling exceptions during the parsing process is important. Consider the following invalid XML string:

String invalidXmlString = "<child>Example</child";

Here, the XML string is missing the closing bracket for the root element, making it invalid. To handle this, we attempt to parse the invalid XML string using DocumentBuilder and ensure that the appropriate exception is thrown. Moreover, we start by initializing a DocumentBuilderFactory and DocumentBuilder:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();

Next, we attempt to parse the invalid XML string and use assertThrows to verify that a SAXParseException is thrown:

assertThrows(SAXParseException.class, () -> {
    builder.parse(new InputSource(new StringReader(invalidXmlString)));
});

In this code, the assertThrows method checks if a SAXParseException is thrown during the parsing attempt. Furthermore, this ensures that our code properly detects and handles the invalid XML string.

By validating input in this way, we can ensure that only well-formed XML strings are processed, improving the reliability of our XML parsing logic.

5. Conclusion

In conclusion, converting string XML fragments to Document nodes is an important part of working with XML in Java. By leveraging Java’s DOM API, we can dynamically parse, manipulate, and integrate XML content.

As usual, we can find the full source code and examples over on GitHub.