04-15-2004, 03:52 AM
Hello, I would like to know what a node is. At first, I thought it was a tag, but then I saw it wasn't.
Could anyone explain what exactly a node is to me?
btw, if this thread should go elswhere, feel free to move it
04-15-2004, 08:37 PM
Basically any part of the HT/X/?ML document is a node. There are at least 12 kinds of nodes, basically only 3 of which you need to worry about regularly (green), and 2 others which are handy to know(blue). The rest are important when you're doing really intensive XML processing for data sharing, etc.
ELEMENT_NODE - any element in the document, whether <h1> or <style>
ATTRIBUTE_NODE - any attribute of any element, created in the ML of the document itself (script properties don't count as attribute nodes)
TEXT_NODE - any text in the document that lives outside attribute nodes. <input type="submit" value="Go!"> : not a text node. <button>Go!</button> : text node.
DOCUMENT_NODE - any whole document, including those contained in each frame.
DOCUMENT_FRAGMENT_NODE - any collection of the rest of the types of nodes except for the DOCUMENT_NODE type
CDATA_SECTION_NODE - a node explicitly created to be ignored by any XML processing, wrapped in a CDATA comment: <![CDATA[ comments here ]]>
ENTITY_REFERENCE_NODE - more popular in DTDs and in future levels of the DOM, this is essentially a variable that refers to something else. Read the HTML specification to see entity references in action.
ENTITY_NODE - what's refered to by entity reference nodes. I'm not even sure how this can exist in XML just yet.
PROCESSING_INSTRUCTION_NODE - this is how to tell an XML parser to do something (obey constraints, perform a calculation) - though I'm not sure how it's defined.
COMMENT_NODE - any comment not using CDATA wrappers.
DOCUMENT_TYPE_NODE - the part of the document specifying the DTD or schema location
NOTATION_NODE - umm... I'm kind of at a loss here.
Oh, and everybody please feel free to correct anything that's wrong in that list.
04-15-2004, 10:54 PM
<body bgcolor="red" (node)>(node)
html, body, bgcolor="red", h1, text
04-15-2004, 11:42 PM
Ok so there a few terms you need to know:
All of these things beneath Node are Nodes. Think of Node as a parent, and the rest Node's children -- so they inherit characteristics from Node, and then add new characteristics to become different.
Basically all HTML tags are Elements. So all the Elements in that above code snippet is: html, body, and h1.
h1 is a ChildNode of body, and text is a ChildNode of h1. text is a TextNode.
bgcolor is an Attribute of the body Element, and "red" is the value of the bgcolor Attribute.
I think that's correct. I don't know how familiar you are with APIs or if you even know how to read one, but this is a good one that I use and it might help you figure out these things better:
Hope that helps,
04-15-2004, 11:55 PM
How could I do something like this?
04-16-2004, 12:25 AM
It's not quite as easy as that. What we're talking about here is DOM (Document Object Model). This model (the M) basically says that this file is a document (the D). This document has objects, arranged in a tree format (the O).
So we've got a tree. This code (or document):
Produces the following tree (or object model):
So document is at the top.
If you do document.childNodes, you'll get a NodeList consisting of one Node: html.
If you do document.firstChild.childNodes, you'll get a NodeList consisting of one Node: body
If you do document.firstChild.firstChild.childNodes (or document.body.childNodes), you'll get a NodeList consisting of one Node: h1.
I think you see my point. So it's not as easy as saying document.nodes and expect to get ALL the nodes in the DOM -- you have to "walk" the tree.
You should really look at that API I posted. It tells you all the methods you're allowed to use. If you want to use a method, but it's not in the API, then you have to make that method up on your own (or find it from somewhere..).
Hope that helps,
04-16-2004, 01:59 AM
ok, still one problem though
document.body.childNodes.length gives me 2 when I have this:
it takes the p and the text, so the tree would be like this:
P & #text
what's going on here?
04-16-2004, 02:15 AM
That #text is not your text. Chances are that it's a space or something. You gotta be careful when walking the tree!
Try alerting nodeValue on it and see if you get nothing..
04-16-2004, 02:18 AM
nope, it IS the text...
04-16-2004, 02:30 AM
Try it yourself:
04-16-2004, 09:15 AM
What browser are you using?
I used IE6 and Netscap 7.1 and both alerted 1, not 2 like you said...
04-16-2004, 09:56 AM
Interesting, I tested in Moz 1.6 and IE 6, both alert 1. But Opera alerts 2 :confused:
04-16-2004, 11:35 PM
04-16-2004, 11:37 PM
Just tested with IE 5.5
It alerts 1!!!
Since when does IE do stuff better than firebird?
04-16-2004, 11:42 PM
I have no idea.. do you have the latest version all that? I've never heard of firebird -- you sure you don't mean firefox?
Maybe you can go to their website and post it on their bugs list, if they've got one.
04-17-2004, 04:49 AM
firdebird is an earlier verion of firefox (it was called firebird before)