1

I am writing one code, that performs some operation on DOM. Till now what I think is elements and nodes, both are same.

But while writing code, I understand that both have some difference. I read so many blogs, forum answers. But I didn't get true feel of their meaning. So please explain these terms deeply and give some examples. So that it become very clear to me.

Thanks in advance. Looking for your kind response.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
devsda
  • 4,112
  • 9
  • 50
  • 87
  • Check it out [here](http://stackoverflow.com/questions/132564/whats-the-difference-between-an-element-and-a-node-in-xml). – Jayamohan Apr 08 '13 at 11:27
  • I read the answers, but I didn't understand what is element , and what is not element. So help me understanding that. – devsda Apr 08 '13 at 11:37
  • 1
    Check NomNomBot's updated answer. – Felix Kling Apr 08 '13 at 11:38
  • @devsda — I've rolled your last change back. If you are having problems parsing a DOM, then solving it would require a significantly different question to the one you have asked here. Ask a new question instead of editing this one. (When do you, show code as well as error messages, and post error messages as text, not screenshots.) – Quentin Apr 08 '13 at 12:24
  • @Quentin Sorry. I will post another question for that, and will give link to you. – devsda Apr 08 '13 at 12:26
  • @Quentin http://stackoverflow.com/questions/15879307/why-jsoup-code-throws-exception – devsda Apr 08 '13 at 12:41

4 Answers4

2

Have you gone through this - What's the difference between an element and a node in XML?

A node is the basic datatype in a DOM - a node can include element, document etc.

According to the DOM, everything in an XML document is a node.

The DOM says:

The entire document is a document node. Every XML element is an element node. The text in the XML elements are text nodes. Every attribute is an attribute node. Comments are comment nodes.

From w3.org:

Node:

The Node interface is the primary datatype for the entire Document Object Model. It represents a single node in the document tree. While all objects implementing the Node interface expose methods for dealing with children, not all objects implementing the Node interface may have children. For example, Text nodes may not have children, and adding children to such nodes results in a DOMException being raised.

Element:

The Element interface represents an element in an HTML or XML document. Elements may have attributes associated with them; since the Element interface inherits from Node, the generic Node interface attribute attributes may be used to retrieve the set of all attributes for an element. There are methods on the Element interface to retrieve either an Attr object by name or an attribute value by name. In XML, where an attribute value may contain entity references, an Attr object should be retrieved to examine the possibly fairly complex sub-tree representing the attribute value. On the other hand, in HTML, where all attributes have simple string values, methods to directly access an attribute value can safely be used as a convenience.

Read this as well - w3schools node types.

Community
  • 1
  • 1
1

An element is a type of node, others include comments and text.

The HTML:

<p> Example <!-- foo --> Example <span></span> </p>

…will create an HTML P Element Node containing a text node, a comment node, another text not and an HTML SPAN Element Node.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • I didn't realize that a comment was a node, kinda just figured it was ignored. – Gary Apr 08 '13 at 11:27
  • Some parsers do discard comments, but DOM specifies a node type for them. – Quentin Apr 08 '13 at 11:28
  • @Quentin means according to your definition, element is tag (ul , table, span , dive , etc.), and comment , text are not element. Am I right ? – devsda Apr 08 '13 at 11:35
  • A start tag and (usually) an end tag are how a markup document marks up the start and end of an element. – Quentin Apr 08 '13 at 11:36
  • @Quentin Means my thinking is correct ? Because your statement is quite difficult to understand for me. – devsda Apr 08 '13 at 11:49
  • Aside from tags being things which tell the parser to create elements not actually being elements themselves, yes. – Quentin Apr 08 '13 at 12:06
  • @Quentin I asked this question, as I stuck while writing my code, I am using Jsoup for DOM operation. It shows exception at line `elem.remove()`. Here,elem is element, and remove() is a function, that removes node and their descendents from DOM tree. Please tell me why this line shows excpetion, if element is also a node. – devsda Apr 08 '13 at 12:21
0

in Dom an Element extends the functionalitys of Nodes. Everything in Dom is a Node.

interface Element : Node

Node:

http://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-1950641247

Element:

http://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-745549614

Diogo Bento
  • 197
  • 2
  • 13
0

Element is a special case of Node.

radkovo
  • 868
  • 6
  • 10