How do I get mixed content of an xml element?

Category: org.jdom, viewed: 1316 time(s).

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.input.SAXBuilder;
import org.jdom.*;

import java.io.StringReader;
import java.io.IOException;
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 e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Here are the result of our program:

Comment   = [Comment: <!-- This element contains application data -->]
Text      = [Text: User Information]
CDATA     = [CDATA: <table><tr><td>-data-</td></tr></table>]
Element   = [Element: <field/>]
Click here to lend your support to: Kode Java Org and make a donation at www.pledgie.com !

 

Can't find what you are looking for? Join our FORUMS and ask some questions!
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats