How do I get mixed content of an XML element in JDOM?

The following example demonstrates how to read mixed content of an xml element. A mixed content can have more than one type of content such as text (Text), comment (Comment), CDATA (CDATA) or some child elements (Element).

You also see that we can remove the mixed content from the element by calling the remove method of the List just like what we do with a collection of data.

package org.kodejava.jdom;

import org.jdom2.*;
import org.jdom2.input.SAXBuilder;

import java.io.IOException;
import java.io.StringReader;
import java.util.List;

public class JDOMMixedContent {
    public static void main(String[] args) {
        String xml = """
                <root> \
                    <data> \
                        <!-- This element contains application data --> \
                        User Information \
                        <![CDATA[<table><tr><td>-data-</td></tr></table>]]> \
                        <field name="username">alice</field> \
                    </data> \
                </root>""";

        SAXBuilder builder = new SAXBuilder();
        try {
            Document document = builder.build(new StringReader(xml));
            Element root = document.getRootElement();
            Element data = root.getChild("data");

            // Reading the mixed content of an xml element and iterate
            // the result list. This list object can contain any of the
            // following objects: Comment, Element, CDATA, DocType,
            // ProcessingInstruction, EntityRef and Text.
            List<Content> contents = data.getContent();

            for (Content content : contents) {
                if (content instanceof Comment comment) {
                    System.out.println("Comment   = " + comment);
                } else if (content instanceof Element element) {
                    System.out.println("Element   = " + element);
                } else if (content instanceof CDATA cdata) {
                    System.out.println("CDATA     = " + cdata);
                } else if (content instanceof DocType docType) {
                    System.out.println("DocType   = " + docType);
                } else if (content instanceof ProcessingInstruction pi) {
                    System.out.println("PI        = " + pi);
                } else if (content instanceof EntityRef entityRef) {
                    System.out.println("EntityRef = " + entityRef);
                } else if (content instanceof Text text) {
                    System.out.println("Text      = " + text);
                }
            }

            // Remove the second mixed contents which is the CDATA contents.
            contents.remove(2);
        } catch (JDOMException | IOException e) {
            e.printStackTrace();
        }
    }
}

Here are the result of our program:

Text      = [Text:          ]
Comment   = [Comment: <!-- This element contains application data -->]
Text      = [Text:          User Information         ]
CDATA     = [CDATA: <table><tr><td>-data-</td></tr></table>]
Text      = [Text:          ]
Element   = [Element: <field/>]
Text      = [Text:      ]

Maven Dependencies

<dependency>
    <groupId>org.jdom</groupId>
    <artifactId>jdom2</artifactId>
    <version>2.0.6.1</version>
</dependency>

Maven Central

Wayan

Leave a Reply

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