How do I get mixed content of an xml element?

The following examples demonstrate 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.example.jdom;

import org.jdom.*;
import org.jdom.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 contains any of the
            // following objects: Comment, Element, CDATA, DocType,
            // ProcessingInstruction, EntityRef and Text.
            List content = data.getContent();

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

            // Remove the second mixed content which is the CDATA content.
            content.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:     ]