Java – Difference between Element node and Text Node

domjavaxmlxml-parsing

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE people SYSTEM "validator.dtd">

<people>
    <student>
        <name>John</name>
        <course>Computer Technology</course>
        <semester>6</semester>
        <scheme>E</scheme>
    </student>

    <student>
        <name>Foo</name>
        <course>Industrial Electronics</course>
        <semester>6</semester>
        <scheme>E</scheme>
    </student>
</people>  

In simple XML language <open-tag> data </open-tag> is an element.
As per my XML above, <student> ... </student> is an element and so are the other tags.

In DOM parsing, there is an Element node and a Text node. Referring to the book I am using, <student> is an Element node and <name>, <course> and the other nested tags are Text nodes.

So, if I am understanding DOM properly, all the outer tags are Elements and the tags that contain the actual data are Text nodes ?

Best Answer

This xml <people><student><name></name></student></people> has no text nodes.

This xml <people><student><name>John</name></student></people> has one text node John

This xml

<people>
    <student>
        <name>John</name>
    </student>
</people>  

has 5 text nodes, text node 1 is spaces and CRLF between <people> and <student> and so on