Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

dom

xml

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?

like image 687
Uganu Mamana Avatar asked Feb 03 '10 09:02

Uganu Mamana


2 Answers

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.

like image 132
Justin Avatar answered Sep 28 '22 13:09

Justin


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".

like image 31
Frank Avatar answered Sep 28 '22 14:09

Frank