Core Java

How to Parse an XML Fragment into a Document Node in Java

Working with XML in Java can be essential in a variety of applications. In many cases, we may need to convert an XML string fragment into a Document node for easier parsing and manipulation of XML data. Java provides built-in tools to handle this task via the javax.xml.parsers package. There are also third-party libraries like xom that offer more flexibility for XML processing. This article explores two approaches for converting an XML string into a Document node: using Java’s built-in DocumentBuilderFactory and the third-party xom library.

1. Using DocumentBuilderFactory

Java has built-in XML processing packages that include important classes like DocumentBuilderFactory to create DocumentBuilder instances, DocumentBuilder to read and parse XML, and Document to represent the root of the XML structure.

Here’s an example of an XML string:

<employee>
    <name>Mr Fish</name>
    <role>Developer</role>
    <salary>75000</salary>
</employee>

We will convert this string XML fragment into a Document object.

public class XmlStringToDocument {

    public static void main(String[] args) {
        String xmlString = "<employee>"
                + "<name>Mr Fish</name>"
                + "<role>Developer</role>"
                + "<salary>75000</salary>"
                + "</employee>";
        
        try {
            // Convert the String XML fragment to a Document object
            Document document = convertStringToXMLDocument(xmlString);
            
            // Print the root element to verify
            System.out.println("Root Element: " + document.getDocumentElement().getNodeName());
        } catch (Exception e) {
        }
    }
    
    /**
     * Converts a string containing XML into a Document object.
     */
    private static Document convertStringToXMLDocument(String xmlString) throws Exception {
        // Create a new instance of DocumentBuilderFactory
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        
        // Create a DocumentBuilder instance
        DocumentBuilder builder = factory.newDocumentBuilder();
        
        // Parse the XML string using InputSource and StringReader
        InputSource inputSource = new InputSource(new StringReader(xmlString));
        
        // Return the parsed Document object
        return builder.parse(inputSource);
    }
}

The DocumentBuilderFactory class is used to create a DocumentBuilder, which provides the means to parse XML documents. We obtain an instance of the factory using the newInstance() method. The DocumentBuilder object is responsible for parsing the XML string and generating the Document object, which represents the structure of the XML.

To feed the XML string into the DocumentBuilder, we use InputSource and StringReader to wrap the string. The final output is the Document object, which in our case will represent the root of the XML structure, the <employee> node.

Output:

Root Element: employee

After converting the XML string into a Document object, we can extract specific elements and values using the getElementsByTagName and getTextContent methods.

    private static void extractAndPrintEmployeeDetails(Document document) {
        // Get the root element of the XML document
        Element rootElement = document.getDocumentElement();
        
        // Extract elements by tag name
        String name = rootElement.getElementsByTagName("name").item(0).getTextContent();
        String role = rootElement.getElementsByTagName("role").item(0).getTextContent();
        String salary = rootElement.getElementsByTagName("salary").item(0).getTextContent();
        
        // Print extracted values
        System.out.println("Employee Name: " + name);
        System.out.println("Employee Role: " + role);
        System.out.println("Employee Salary: " + salary);
    }

Output:

java xml fragment document node example output

2. Insert the Node into a Larger Document

Once we have the Element, we can insert it into a parent node in another Document. The example below shows how to insert a new Document node into an existing Document using Java’s DocumentBuilderFactory:

public class XmlInsertNode {

    public static void main(String[] args) {
        try {
            // Sample XML strings
            String existingXml = "<company><employees></employees></company>";
            String newEmployeeXml = "<employee><name>Mr Fish</name><role>Developer</role><salary>75000</salary></employee>";

            // Parse the existing XML document
            Document existingDocument = convertStringToXMLDocument(existingXml);
            
            // Parse the new employee XML string
            Document newEmployeeDocument = convertStringToXMLDocument(newEmployeeXml);
            
            // Insert the new employee node into the existing document
            insertNodeIntoExistingDocument(existingDocument, newEmployeeDocument);
            
            // Display the updated document structure
            String updatedXml = documentToString(existingDocument);
            System.out.println("Updated Document Structure:\n" + updatedXml);

        } catch (Exception e) {
        }
    }
    
    /**
     * Converts a string containing XML into a Document object.
     */
    private static Document convertStringToXMLDocument(String xmlString) throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        InputSource inputSource = new InputSource(new StringReader(xmlString));
        return builder.parse(inputSource);
    }

    /**
     * Inserts the root node of the new Document into an existing Document.
     */
    private static void insertNodeIntoExistingDocument(Document existingDocument, Document newDocument) {
        // Get the root element of the new document (employee node)
        Node newEmployeeNode = newDocument.getDocumentElement();
        
        // Import the new node into the existing document
        Node importedNode = existingDocument.importNode(newEmployeeNode, true);
        
        // Append the imported node to the appropriate location (inside <employees>)
        existingDocument.getElementsByTagName("employees").item(0).appendChild(importedNode);
    }

    /**
     * Converts the XML Document into a string for display.
     */
    private static String documentToString(Document document) throws Exception {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        DOMSource source = new DOMSource(document);
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        transformer.transform(source, result);
        return writer.toString();
    }
}
  • The convertStringToXMLDocument method is used to convert both the existing XML string (existingXml) and the new employee XML string (newEmployeeXml) into Document objects.
  • Inserting the New Node
    • In the insertNodeIntoExistingDocument method, the root element of the newDocument (representing the new employee) is imported into the existingDocument using the importNode() method.
    • The importNode() method ensures that the new node is compatible with the existing document. We use true to copy the entire node subtree.
    • The new employee node is then appended to the <employees> element in the existing document.

Output is:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<company>
  <employees>
    <employee>
      <name>Mr Fish</name>
      <role>Developer</role>
      <salary>75000</salary>
    </employee>
  </employees>
</company>

3. Using XOM Library

The XOM library is another option for working with XML in Java, providing a simple API for parsing, creating, and modifying XML documents. Using Maven, we need to add the XOM dependency to our pom.xml file.

<dependency>
  <groupId>xom</groupId>
  <artifactId>xom</artifactId>
  <version>1.3.9</version>
</dependency>
import nu.xom.*;

public class XOMInsertNode {

    public static void main(String[] args) {
        try {
            // Sample existing XML string
            String existingXml = "<company><employees></employees></company>";
            
            // Sample new employee XML string
            String newEmployeeXml = "<employee><name>Mr Fish</name><role>Developer</role><salary>75000</salary></employee>";
            
            // Parse the existing XML string into a XOM Document
            Builder parser = new Builder();
            Document existingDocument = parser.build(existingXml, null);
            
            // Parse the new employee XML string into a XOM Document
            Document newEmployeeDocument = parser.build(newEmployeeXml, null);
            
            // Insert the new employee node into the existing document
            insertNodeIntoExistingDocument(existingDocument, newEmployeeDocument);

            // Display the updated XML structure
            System.out.println(existingDocument.toXML());
        } catch (Exception e) {
        }
    }

    /**
     * Inserts the root node of the new Document into an existing Document.
     */
    private static void insertNodeIntoExistingDocument(Document existingDocument, Document newDocument) {
        // Get the root element of the new document (employee node)
        Element newEmployeeElement = newDocument.getRootElement();
        
        // Get the <employees> element from the existing document
        Element employeesElement = existingDocument.getRootElement().getFirstChildElement("employees");

        // Insert the new employee element into the <employees> element
        employeesElement.appendChild(newEmployeeElement.copy());
    }
}

In this code, the Builder class from the XOM library is used to parse XML strings into Document objects. The insertNodeIntoExistingDocument method retrieves the root element of the new employee document (newEmployeeElement) and appends it to the <employees> element in the existing document, using newEmployeeElement.copy() to ensure the new node is copied.

4. Conclusion

In this article, we examined two methods for handling XML in Java: DocumentBuilderFactory from the standard library and the XOM library. We showed how both approaches allow for parsing XML strings, manipulating documents, and inserting new nodes.

5. Download the Source Code

This article explains how to convert a Java XML fragment into a document node.

Download
You can download the full source code of this example here: java xml fragment document node

Omozegie Aziegbe

Omos Aziegbe is a technical writer and web/application developer with a BSc in Computer Science and Software Engineering from the University of Bedfordshire. Specializing in Java enterprise applications with the Jakarta EE framework, Omos also works with HTML5, CSS, and JavaScript for web development. As a freelance web developer, Omos combines technical expertise with research and writing on topics such as software engineering, programming, web application development, computer science, and technology.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button