First the xml code for greeting.xml
<?xml version="1.0"?> <!--a simple xml document--> <xdoc> <greeting>Hello HTML! <greeting>Hello XML! <applause type="sustained" /> </xdoc>
Then the XML Island code:
<XML ID="island" src="greeting.xml"></xml>
There seems to be a problem with the XML Island so you need to clear the cache if you have been to the page before and then reload the page.
If my server supported Active X controls I could have used the code below:
var myGreeting = new ActiveXObject("microsoft.XMLDOM");
myGreeting.load("greeting.xml");
if(myGreeting.parseError.reason !="") {
alert(myGreeting.parseError.reason)
}
The code below
myGreeting.documentElement.nodeName;is used to bring back the root element node name of .
The code below
myGreeting.doctype;is used to bring back the document type but if the DTD is external, or in this case if does not have a DTD, it only brings back that the value is .
The code below
myGreeting.implementation.hasFeature("XML", "1") will return a Boolean and takes two arguments, a feature (XML or HTML) and a version. In this case it returned because the exact version declaration is "1.0".
var rootEl=myGreeting.documentElementcreates a variable called rootEl which contains all the document nodes after the prolog. Then using the code
rootEl.parentNode.nodeNamebrings back which is the node name of the parent node.
myGreeting.childnodeswill create a nodeList of all the documents children of any given node. The length or number is
The myGreeting.xml document has a total of nine nodes
It has one Processing Instruction which is the version declaration.
The document has one Comment node.
There are four element nodes.
There are two text nodes.
The node types this document does not used are:
Relational methods return the requested relationship and I list the nodeType, nodeName, nodeValue:
rootEl.parentNode.nodename
rootEl.firstChild.nodename
rootEl.lastChild.nodename
rootEl.previousSibling.nodename
rootEl.nextSibling.nodename
Walking the tree using relationship methods of the DOM:
shows SIX nodes
(myGreeting.firstChild.nodeName)Brings back:
(myGreeting.firstChild.nextSibling.nodeName)Brings back:
(myGreeting.firstChild.nextSibling.nextSibling.nodeName)Brings back:
(myGreeting.firstChild.nextSibling.nextSibling.firstChild.nodeName)Brings back:
(myGreeting.firstChild.nextSibling.nextSibling.firstChild.nextSibling.nodeName);Brings back:
(myGreeting.firstChild.nextSibling.nextSibling.firstChild.nextSibling.nextSibling.nodeName);Brings back:
To write out the list of the greeting tags you can use the getElementsByTagName() method. You create the node list of the greeting elements using the code
var greetList = myGreeting.getElementsByTagName("greeting");
Then using a simple loop and the item() method to access the individual nodes for(var i=0; i < greetList.length; i++){
document.write(greetList.item(i).firstChild.nodeValue)
The attribute method can only be applied to and element type node. It will return a NamedNodeMap object containing the attributes of the element.
The first greeting element does not have any attributes so using the code
var anEl = rootEl.firstChild; fcAt.value = anEl.attributes(0).nodeValuebrings back
The lastChild has the attribute so the code
var anEl = rootEl.lastChild; lcAt.value = anEl.attributes(0).nodeValuebrings back the attribute value of
Another way to get the attribute of an element is to use the getAttribute method, code follows:
rootEl = myGreeting.documentElement;
anEl2 = rootEl.lastChild;
anEl2.getAttribute("type");
Relationships with regard to the first greeting element
Parent Node:
First Child:
Last Child:
Next Sibling:
Previous Sibling: (there is none)
Relationships with regard to the second greeting element
Parent Node:
First Child:
Last Child:
Next Sibling:
Previous Sibling:
Relationships with regard to the applause element
Parent Node:
First Child: (there is none)
Last Child: (there is none)
Next Sibling: (there is none)
Previous Sibling:
Relationships with regard to the xdoc element
Parent Node:
First Child:
Last Child:
Next Sibling: (there is none)
Previous Sibling:
Relationships with regard to the comment node
Parent Node:
First Child: (there is none)
Last Child: (there is none)
Next Sibling:
Previous Sibling:
Lists that are indexed:
child list of xdoc:
child list of first greeting:
child list of applause element:
list of the second greeting:
list of applause element:
list of applause attributes: