I'm working in Java with XML and I'm wondering; what's the difference between an element and a node?
13 Answers
Different W3C specifications define different sets of "Node" types.
Thus, the DOM spec defines the following types of nodes:
Document
--Element
(maximum of one),ProcessingInstruction
,Comment
,DocumentType
DocumentFragment
--Element
,ProcessingInstruction
,Comment
,Text
,CDATASection
,EntityReference
DocumentType
-- no childrenEntityReference
--Element
,ProcessingInstruction
,Comment
,Text
,CDATASection
,EntityReference
Element
--Element
,Text
,Comment
,ProcessingInstruction
,CDATASection
,EntityReference
Attr
--Text
,EntityReference
ProcessingInstruction
-- no childrenComment
-- no childrenText
-- no childrenCDATASection
-- no childrenEntity
--Element
,ProcessingInstruction
,Comment
,Text
,CDATASection
,EntityReference
Notation
-- no children
The XML Infoset (used by XPath) has a smaller set of nodes:
XPath has the following Node types:
- root nodes
- element nodes
- text nodes
- attribute nodes
- namespace nodes
- processing instruction nodes
- comment nodes
The answer to your question "What is the difference between an element and a node" is:
An element is a type of node. Many other types of nodes exist and serve different purposes.
As described in the various XML specifications, an element
is that which consists of a start tag, and end tag, and the content in between, or alternately an empty element tag (which has no content or end tag). In other words, these are all elements:
<foo> stuff </foo>
<foo bar="baz"></foo>
<foo baz="qux" />
Though you hear "node" used with roughly the same meaning, it has no precise definition per XML specs. It's usually used to refer to nodes of things like DOMs, which may be closely related to XML or use XML for their representation.
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 defined as:
the smallest unit of a valid, complete structure in a document.
or as:
An object in the tree view that serves as a container to hold related objects.
Now their are many different kinds of nodes as an elements node, an attribute node etc.
Now i know ,the element is one of node
All node types in here"http://www.w3schools.com/dom/dom_nodetype.asp"
Element is between the start tag and end in the end tag
So text node is a node , but not a element.