I\'m working in Java with XML and I\'m wondering; what\'s the difference between an element and a node?
An element is a type of node as are attributes, text etc.
An xml document is made of nested elements. An element begins at its opening tag and ends at its closing tag. You're probably seen <body>
and </body>
in html. Everything between the opening and closing tags is the element's content. If an element is defined by a self-closing tag (eg. <br/>
) then its content is empty.
Opening tags can also specify attributes, eg. <p class="rant">
. In this example the attribute name is 'class' and its value 'rant'.
The XML language has no such thing as a 'node'. Read the spec, the word doesn't occur.
Some people use the word 'node' informally to mean element, which is confusing because some parsers also give the word a technical meaning (identifying 'text nodes' and 'element nodes'). The exact meaning depends on the parser, so the word is ill-defined unless you state what parser you are using. If you mean element, say 'element'.
A node is the base class for both elements and attributes (and basically all other XML representations too).
node & element are same. Every element is a node , but it's not that every node must be an element.
Element is the only kind of node that can have child nodes and attributes.
Document also has child nodes, BUT
no attributes, no text, exactly one child element.
A node can be a number of different kinds of things: some text, a comment, an element, an entity, etc. An element is a particular kind of node.