Vous êtes sur la page 1sur 2

Element is a subinterface of Node.

An Element represents an element in an XML document, so


something that begins with a start tag and ends with an end tag. An Element is a Node, but there
are other things besides Element which are also Nodes. For example, attributes, comments etc.
are also Nodes, but not Elements.

If you are going to use the DOM in the standard Java library you should get really familiar with
the table in the Javadocs for the Node interface in the org.w3c.dom package. There are 12
different subinterfaces of Node - each with its own peculiarities summarized in that table.

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.

Node is like super type of an element. Element is a


specific type of node.

exmaple

Element:
<name>Hari</name>

Node:
<name id="1212" dep="6">Hari</name>
What is the difference between XML Attributes and XML
Elements?
Both elements and attributes have been designed to have a Name and a Value.

Elements can be parents of other elements and/or attributes and can be repeated within the
same level of an XML document. They also usually have start and end tags.
An element would look like: <Customer>John Doe</Customer>

Attributes consist of a named pair attached to an element start-tag. An example of how an


attribute is: <Customer ID="1234">John Doe</Customer>. Attribute values must be enclosed
in single or double quotes. Attribute names must be unique within a single element occurrence.

When to use Elements versus Attributes is a very polemic question and more related to
architectural considerations than technical details.

Nevertheless there are things only Elements can do...

Elements can occur more than once (repeating) within the same level, while attributes can only
appear once within the same level, example:

It is okay to have:
<Root>
<Customer ID="1234">John Doe</Customer>
<Customer ID="2345">Jane Doe</Customer>
</Root>

But it would be invalid to have:


<Root>
<Customer ID="1234" ID="2345">
</Root>

Elements can be defined to be in a certain order, while attributes can appear in any order.

If you would like to get some extra details on this matter, please see visit one of the following
web-pages:

http://www.itworld.com/nl/xml_prac/12132001/
http://xml.coverpages.org/elementsAndAttrs.html

Vous aimerez peut-être aussi