tags:

views:

176

answers:

4

Possible Duplicate:
What’s the difference between an element and a node in 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? 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?

+1  A: 

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.

Kragen
+3  A: 

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

Frank
+1  A: 

Please have a look at http://stackoverflow.com/questions/132564/whats-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.

Modery
+1  A: 

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
Andy E