Why are Nodes and Elements different in XML? What was the rationale?

11,068

Solution 1

Node is a base class of Element - pretty much everything in an Xml document is a Node, for example:

<!ENTITY...>
<xml a="myAttribute">
    SomeText
    <!-- A comment -->
</xml>

In the above example:

  • <!ENTITY...> is an entity
  • <xml ... is an element
  • a="myAttribute" is an attribute
  • SomeText is a text node
  • <!-- A comment --> is a comment

All of the above inherit from Node, in fact in the above example myAttribute is also a text node.

Solution 2

Node is more generic than element. Check out this page for all the different 'things' a Node can stand for. An Element is just one of those possibilities, which corresponds to the tags. It is important to stress that nodes do not generally represent XML tags.

For example, <a>blah</a> contains two nodes. The first is an 'element' representing the <a> tag, the second is a text node containing "blah".

Solution 3

They don't both represent tags. An element represents a node that begins with a start tag and ends with an end tag. An element is a node but a node is not necessarily an element. For example nodes can be:

  • Elements
  • Text data
  • Comments

Solution 4

Please have a look at What's the difference between an element and a node in XML? A similar question had been asked before.

Summary: An element is a particular kind of node. A node can also be an attribute node, text node, comment node, etc.

Share:
11,068
Uganu Mamana
Author by

Uganu Mamana

Updated on July 22, 2022

Comments

  • Uganu Mamana
    Uganu Mamana almost 2 years

    Having been writing code that operates on XML for a while, I have always wondered what is the reason for having both Nodes and Elements? (We know what the differences are). Both of them represent tags (more or less) and having different methods, just makes the code complicated.

    Are there any special semantics or practical reasons, or is it just the fact that the DOM spec was committee generated?