This page contains the XML for <SCRIPT> DOMNode object documentation.
Return to W3C DOM Parser Documentation Main Page
-
CONTENTS
-
DOMNode - object description
-
-
-
Native Methods:
-
DOMNode - getNodeName method
-
DOMNode - getNodeValue method
-
DOMNode - setNodeValue method
-
DOMNode - getNodeType method
-
DOMNode - getParentNode method
-
DOMNode - getChildNodes method
-
DOMNode - getFirstChild method
-
DOMNode - getLastChild method
-
DOMNode - getPreviousSibling method
-
DOMNode - getNextSibling method
-
DOMNode - getAttributes method
-
DOMNode - getNamespaceURI method
-
DOMNode - getPrefix method
-
DOMNode - getLocalName method
-
DOMNode - getOwnerDocument method
-
DOMNode - insertBefore method
-
DOMNode - replaceChild method
-
DOMNode - removeChild method
-
DOMNode - appendChild method
-
DOMNode - hasChildNodes method
-
DOMNode - cloneNode method
-
DOMNode - getElementsByTagName method
-
DOMNode - getElementsByTagNameNS method
-
DOMNode - setPrefix method
-
DOMNode - normalize method
-
DOMNode - isSupported method
-
DOMNode - getXML method
-
DOMNode - hasAttributes method
-
-
-
Inherited Methods:
-
The DOMNode object has no inherited W3C methods
DOMNode - object description
The DOMNode 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
DOMNode interface expose methods for dealing with children, not all objects
implementing the DOMNode interface may have children. For example, DOMText nodes may not
have children.
The methods getNodeName, getNodeValue and getAttributes are included as a mechanism to get at
node information easily. In cases where there is no obvious mapping for the return values of
these methods for a specific nodeType, (e.g., getNodeValue for a DOMElement or getAttributes
for a DOMComment ), these methods return null.
DOMNode - getNodeName method
W3C DOM Level 1 and Level 2 (namespaces)
node.getNodeName();
- or -
node.nodeName
accepts:
N/A
returns:
String - name of the node
throws:
N/A
The getNodeName method returns the name of the DOMNode or data about the DOMNode type
based on the following table:
nodeName from an: |
returns: |
Attribute (Attr) |
name of attribute |
CDATASection |
"#cdata-section" |
Comment |
"#comment" |
Document |
"#document" |
DocumentFragment |
"#document-fragment" |
Element |
tag name |
ProcessingInstruction |
target |
Text |
"#text" |
NOTE: nodeName for the following DOMNode types are not supported in XML for <SCRIPT>
-
DTD
-
Entity
-
EntityReference
-
DOMNotation
NOTE: It is also possible to access the name of the
DOMNode by accessing the nodeName property. This property should be treated as
read-only and should never be set in code.
NOTE: when used with namespaces,
the node name returned will be a QName (Qualified-Name) if the node
has a namespace prefix attached. Nodes with default namespaces are not
QNames and will return only the tag name. See example 2.
Example 1:
function xmljsDOMExample() {
var xml;
xml = ""
+ "<?xml version=\"1.0\"?>"
+ "<ROOTNODE attribute=\"attributeValue\">"
+ "<TAG1 foo=\"goo\">"
+ "Hello World"
+ "<!--comment-->"
+ "<![CDATA[this child is of <<<>nodeType CDATA]]>"
+ "</TAG1>"
+ "</ROOTNODE>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//get the root node
var docRoot = domDoc.getDocumentElement();
//the following should be "ROOTNODE"
alert(docRoot.getNodeName());
//the following should be "attribute"
alert(docRoot.getAttributeNode("attribute").getNodeName());
//get the "TAG1" element
var tag1 = docRoot.getElementsByTagName("TAG1").item(0);
//the following should be "#cdata-section"
alert(tag1.childNodes.item(2).getNodeName());
//the following should be "#comment"
alert(tag1.childNodes.item(1).getNodeName());
//the following should be "#document"
alert(domDoc.nodeName);
//the following should be "#document-fragment"
alert(domDoc.createDocumentFragment().getNodeName());
//the following should be "TAG1"
alert(tag1.getNodeName());
//the following should be "xml"
alert(domDoc.firstChild.getNodeName());
//the following should be "#text"
alert(tag1.firstChild.getNodeName());
} // end function xmljsDOMExample
Example 2:
function xmljsDOMExample() {
var xml =""
+ "<?xml version=\"1.0\"?>"
+ "<ROOTNODE "
+ "xmlns=\"http://xmljs.sf.net/ns1\" "
+ "xmlns:ns2=\"http://xmljs.sf.net/ns2\" >"
+ "<TAG1>"
+ "ns1 Hello world"
+ "</TAG1>"
+ "<ns2:TAG1>"
+ "ns2 Hello world"
+ "</ns2:TAG1>"
+ "</ROOTNODE>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//namespaceAware is true by default
//this line is here only for demonstration purposes
parser.namespaceAware = true;
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//get the root node
var docRoot = domDoc.getDocumentElement();
//get the DOMNodeList for TAG1 with the ns1 (default) namespace
var ns1 = "http://xmljs.sf.net/ns1";
var tag1 = docRoot.getElementsByTagNameNS(ns1, "TAG1").item(0);
//the following should be "TAG1"
//because that DOMElement's name is not a QName
//(because it is in the default namespace)
alert(tag1.getNodeName());
//get the DOMNodeList for TAG1 with the ns2 namespace
var ns2 = "http://xmljs.sf.net/ns2";
var tag1 = docRoot.getElementsByTagNameNS(ns2, "TAG1").item(0);
//the following should be "ns2:TAG1"
//because that DOMElement is not in the default namespace
alert(tag1.getNodeName());
} // end function xmljsDOMExample
DOMNode - getNodeValue method
W3C DOM Level 1
node.getNodeValue()
- or -
node.nodeValue;
accepts:
N/A
returns:
String - value of the node
throws:
N/A
The getNodeValue method returns the value of the DOMNode based on the following table:
getNodeValue from an: |
returns: |
Attribute (DOMAttr) |
value of attribute |
DOMCDATASection |
content of the CDATA Section |
DOMComment |
content of the comment |
DOMProcessingInstruction |
entire content excluding the target |
DOMText |
content of the text node |
Calling getNodeValue on all other types of DOMNode objects will return null.
NOTE: It is also possible to access the value of the
DOMNode by accessing the nodeValue property. This property should be treated as
read-only and should never be set in code.
Example:
function xmljsDOMExample() {
var xml;
xml = ""
+ "<?xml version=\"1.0\"?>"
+ "<ROOTNODE attribute=\"attributeValue\">"
+ "<TAG1 foo=\"goo\">"
+ "Hello World"
+ "<!--comment-->"
+ "<![CDATA[this child is of <<<>nodeType CDATA]]>"
+ "</TAG1>"
+ "</ROOTNODE>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//get the root node
var docRoot = domDoc.getDocumentElement();
//the following should be "attributeValue"
alert(docRoot.getAttributeNode("attribute").getNodeValue());
//get the "TAG1" element
var tag1 = docRoot.getElementsByTagName("TAG1").item(0);
//the following should be "this child is of <<<>nodeType CDATA"
alert(tag1.getChildNodes().item(2).getNodeValue());
//the following should be "comment"
alert(tag1.getChildNodes().item(1).getNodeValue());
//the following should be 'version="1.0"'
alert(domDoc.getFirstChild().getNodeValue());
//the following should be "Hello World"
alert(tag1.getFirstChild().getNodeValue());
} // end function xmljsDOMExample
DOMNode - setNodeValue method
W3C DOM Level 1
node.setNodeValue(<new value>)
accepts:
String - the new value for the DOMNode
returns:
String - the new value of the DOMNode
throws:
The setNodeValue method is an XML for <SCRIPT> extension for setting the values
of all DOMNodes and DOMNode descendants that support the getNodeValue method.
This method should be used in all cases where the value of the node must be set in
code. Calling setNodeValue property updates all aspects of the node (such as the
return value of the getData method of DOMCharacterData objects) properly.
The following nodes support the setNodeValue method:
-
DOMAttr
-
DOMCDATASection
-
DOMComment
-
DOMProcessingInstruction
-
DOMText
Another option for setting the value of DOMAttribute Nodes is to call the
.setAttribute method of the attribute nodes's container node. This option is also
demonstrated in the example below.
Example:
function xmljsDOMExample() {
var xml;
xml = ""
+ "<?xml version=\"1.0\"?>"
+ "<ROOTNODE attribute=\"attributeValue\">"
+ "<TAG1 foo=\"goo\">"
+ "Hello World"
+ "<!--comment-->"
+ "<![CDATA[this child is of <<<>nodeType CDATA]]>"
+ "</TAG1>"
+ "</ROOTNODE>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//get the root node
var docRoot = domDoc.getDocumentElement();
//the following should be "attributeValue"
alert(docRoot.getAttributes().getNamedItem("attribute").getNodeValue());
//set the attribute's value to "First New Value"
docRoot.getAttributes().getNamedItem("attribute").setNodeValue("First New Value");
//the following should be "First New Value"
alert(docRoot.getAttributes().getNamedItem("attribute").getNodeValue());
//set the attribute's value to "Second New Value"
//via the container node's setAttribute method
docRoot.setAttribute("attribute", "Second New Value");
//the following should be "Second New Value"
alert(docRoot.getAttributes().getNamedItem("attribute").getNodeValue());
//now get the CharacterData under the TAG1 Element
//get the "TAG1" element
var tag1 = docRoot.getElementsByTagName("TAG1").item(0);
//the following should be "Hello World"
alert(tag1.getFirstChild().getNodeValue());
//set the node value and data properties using the setNodeValue helper function
//DO NOT set nodeValue or data directly
tag1.getFirstChild().setNodeValue("New Hello World");
//the following should be "New Hello World"
alert(tag1.getFirstChild().getNodeValue());
alert(tag1.getFirstChild().getData());
}// end function xmljsDOMExample
DOMNode - getNodeType method
W3C DOM Level 1
node.getNodeType();
- or -
node.nodeType;
accepts:
N/A
returns:
int - nodeType code of the node
throws:
N/A
The getNodeType method returns values according to the DOMNode types
in the following table:
Node Type
|
Return Value
|
DOMNode.ELEMENT_NODE
|
1
|
DOMNode.ATTRIBUTE_NODE
|
2
|
DOMNode.TEXT_NODE
|
3
|
DOMNode.CDATA_SECTION_NODE
|
4
|
DOMNode.ENTITY_REFERENCE_NODE
|
NOT IMPLEMENTED*
|
DOMNode.ENTITY_NODE
|
NOT IMPLEMENTED*
|
DOMNode.PROCESSING_INSTRUCTION_NODE
|
7
|
DOMNode.COMMENT_NODE
|
8
|
DOMNode.DOCUMENT_NODE
|
9
|
DOMNode.DOCUMENT_TYPE_NODE
|
NOT IMPLEMENTED*
|
DOMNode.DOCUMENT_FRAGMENT_NODE
|
11
|
DOMNode.NOTATION_NODE
|
NOT IMPLEMENTED*
|
*NOT IMPLMENTENTED
: XML for <SCRIPT> does not support the creation of these node types.
NOTE: It is also possible to access the type of the
DOMNode by accessing the nodeType property. This property should be treated as
read-only and should never be set in code.
If developer's wish to compare node types, XML for <SCRIPT> provides the following constants
which correspond to the return values above:
-
DOMNode.ELEMENT_NODE
-
DOMNode.ATTRIBUTE_NODE
-
DOMNode.TEXT_NODE
-
DOMNode.CDATA_SECTION_NODE
-
DOMNode.ENTITY_REFERENCE_NODE
-
DOMNode.ENTITY_NODE
-
DOMNode.PROCESSING_INSTRUCTION_NODE
-
DOMNode.COMMENT_NODE
-
DOMNode.DOCUMENT_NODE
-
DOMNode.DOCUMENT_TYPE_NODE
-
DOMNode.DOCUMENT_FRAGMENT_NODE
-
DOMNode.NOTATION_NODE
-
DOMNode.NAMESPACE_NODE
Example:
function xmljsDOMExample() {
var xml;
xml = ""
+ "<?xml version=\"1.0\"?>"
+ "<ROOTNODE attribute=\"attributeValue\">"
+ "<TAG1 foo=\"goo\">"
+ "Hello World"
+ "<!--comment-->"
+ "<![CDATA[this child is of <<<>nodeType CDATA]]>"
+ "<? hello ?>"
+ "</TAG1>"
+ "</ROOTNODE>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//get the root node
var docRoot = domDoc.getDocumentElement();
//get the "TAG1" element
var tag1 = docRoot.getElementsByTagName("TAG1").item(0);
//the following should be 1
alert(tag1.getNodeType());
//the following should be 2
alert(tag1.attributes.getNamedItem("foo").getNodeType());
//the following should be 3
alert(tag1.childNodes.item(0).getNodeType());
//the following should be 4
alert(tag1.childNodes.item(2).getNodeType());
//the following should be 7
alert(tag1.childNodes.item(3).getNodeType());
//the following should be 8
alert(tag1.childNodes.item(1).getNodeType());
//the following should be 9
alert(domDoc.getNodeType());
//the following should be 11
alert(domDoc.createDocumentFragment().getNodeType());
}// end function xmljsDOMExample
DOMNode - getParentNode method
W3C DOM Level 1
node.getParentNode();
- or -
node.parentNode;
accepts:
N/A
returns:
DOMNode - the parent node of the queried DOMNode object
throws:
N/A
NOTE: It is also possible to access the parent node of the
DOMNode by accessing the parentNode property. This property should be treated as
read-only and should never be set in code.
All DOMNodes, except Document, DocumentFragment, and Attr may have a parent.
However, if a DOMNode has just been created and not yet added to the tree,
or if it has been removed from the tree, the return value of parentNode will be
null.
Example:
function xmljsDOMExample() {
var xml;
xml = ""
+ "<?xml version=\"1.0\"?>"
+ "<ROOTNODE attribute=\"attributeValue\">"
+ "<TAG1 foo=\"goo\">"
+ "Hello World"
+ "<!--comment-->"
+ "<![CDATA[this child is of <<<>nodeType CDATA]]>"
+ "<? hello ?>"
+ "</TAG1>"
+ "</ROOTNODE>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//get the root node
var docRoot = domDoc.getDocumentElement();
//get the "TAG1" element
var tag1 = docRoot.getElementsByTagName("TAG1").item(0);
var retNode = tag1.getParentNode();
//the following should be "ROOTNODE"
alert(retNode.nodeName);
retNode = tag1.childNodes.item(1).getParentNode();
//the following should be "TAG1"
alert(retNode.getNodeName());
//the following should be null (attributes cannot have parents)
alert(tag1.attributes.getNamedItem("foo").getParentNode());
//attributes can, however, have owner Elements
//the following should be "TAG1"
alert(tag1.attributes.getNamedItem("foo").getOwnerElement().getNodeName());
}// end function xmljsDOMExample
DOMNode - getChildNodes method
W3C DOM Level 1
node.getChildNodes();
- or -
node.childNodes;
accepts:
N/A
returns:
DOMNodeList - the child nodes of the queried DOMNode object
throws:
N/A
Querying the getChildNodes method of a DOMNode object returns a DOMNodeList containing all
of the children of the queried node. If there are no children, this is a DOMNodeList containing no nodes.
The content of the returned DOMNodeList is "live" in the sense that, for instance, changes to the
children of the node object that it was created from are immediately reflected in the nodes
returned by the DOMNodeList accessors. In other words, it is not a static snapshot of the
content of the node. This is true for every DOMNodeList, including the ones returned by
the getElementsByTagName method.
NOTE: It is also possible to access the child nodes of the
DOMNode by accessing the childNodes property. This property should be treated as
read-only and should never be set in code.
Example:
function xmljsDOMExample() {
var xml;
xml = ""
+ "<?xml version=\"1.0\"?>"
+ "<ROOTNODE attribute=\"attributeValue\">"
+ "<TAG1 foo=\"goo\">"
+ "Hello World"
+ "<!--comment-->"
+ "<![CDATA[this child is of <<<>nodeType CDATA]]>"
+ "<?hello world ?>"
+ "</TAG1>"
+ "</ROOTNODE>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//get the root node
var docRoot = domDoc.getDocumentElement();
//get a DOMNodeList consisting of "TAG1" elements
domNodeListTag1s = docRoot.getElementsByTagName("TAG1");
//get the number of nodes in the
//domNodeListTag1s DOMNodeList
//the following should be 1
alert(domNodeListTag1s.getLength());
//get the TAG1 element from the domNodeListsTag1s DOMNodeList
//using the DOMNodeList item method. The item method
//is zero-based, so the node we're interested in would be
//node 0 since the length of the DOMNodeList object was 1
var tag1 = domNodeListTag1s.item(0);
//call getChildNodes to get another DOMNodeList object
//NOTE: getChildNodes does *not* return an array.
//It returns a DOMNodeList object
var domNodeList = tag1.getChildNodes();
//call the getLength method of the DOMNodeList object
//the following should be 4
alert(domNodeList.getLength());
}// end function xmljsDOMExample
DOMNode - getFirstChild method
W3C DOM Level 1
node.getFirstChild();
- or -
node.firstChild;
accepts:
N/A
throws:
N/A
returns:
DOMNode - the first child of the queried DOMNode object
getFirstChild is a convience method to access the first of the child nodes
in a DOMNode. If there is no such DOMNode, null is returned.
getFirstChild is equivalent to DOMNode.getChildNodes().item(0)
NOTE: It is also possible to access the first child of the
DOMNode by accessing the firstChild property. This property should be treated as
read-only and should never be set in code.
Example:
function xmljsDOMExample() {
var xml;
xml = ""
+ "<?xml version=\"1.0\"?>"
+ "<ROOTNODE attribute=\"attributeValue\">"
+ "<TAG1 foo=\"goo\">"
+ "Hello World"
+ "<!--comment-->"
+ "<![CDATA[this child is of <<<>nodeType CDATA]]>"
+ "<? hello ?>"
+ "</TAG1>"
+ "</ROOTNODE>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//get the root node
var docRoot = domDoc.getDocumentElement();
//get the "TAG1" element
var tag1 = docRoot.getElementsByTagName("TAG1").item(0);
//the following should be "Hello World"
alert(tag1.getFirstChild().getNodeValue());
//the following should also be "Hello World"
alert(tag1.getChildNodes().item(0).getNodeValue());
}// end function xmljsDOMExample
DOMNode - getLastChild method
W3C DOM Level 1
node.getLastChild();
- or -
node.lastChild;
accepts:
N/A
returns:
DOMNode - the last child of the queried DOMNode object
throws:
N/A
getLastChild is a convience method to access the last of the child nodes
in a DOMNode. If there is no such DOMNode, null is returned.
getLastChild is equivalent to DOMNode.getChildNodes().item(DOMNode.getChildNodes().getLength() - 1)
NOTE: It is also possible to access the last child of the
DOMNode by accessing the lastChild property. This property should be treated as
read-only and should never be set in code.
Example:
function xmljsDOMExample() {
var xml;
xml = ""
+ "<?xml version=\"1.0\"?>"
+ "<ROOTNODE attribute=\"attributeValue\">"
+ "<TAG1 foo=\"goo\">"
+ "Hello World"
+ "<!--comment-->"
+ "<![CDATA[this child is of <<<>nodeType CDATA]]>"
+ "<? hello ?>"
+ "Hi!"
+ "</TAG1>"
+ "</ROOTNODE>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//get the root node
var docRoot = domDoc.getDocumentElement();
//get the "TAG1" element
var tag1 = docRoot.getElementsByTagName("TAG1").item(0);
//the following should be "Hi!"
alert(tag1.getLastChild().getNodeValue());
//the following should also be "Hi!"
var lastChildIndex = tag1.getChildNodes().getLength() -1;
alert(tag1.getChildNodes().item(lastChildIndex).getNodeValue());
}// end function xmljsDOMExample
DOMNode - getPreviousSibling method
W3C DOM Level 1
node.getPreviousSibling();
- or -
node.previousSibling;
accepts:
N/A
returns:
DOMNode - the previous sibling of the queried DOMNode object
throws:
N/A
getPreviousSibling is a convience method for accessing the DOMNode
immediately preceding the queried DOMNode. If there is no such node, null is returned.
NOTE: It is also possible to access the previous sibling of the
DOMNode by accessing the previousSibling property. This property should be treated as
read-only and should never be set in code.
Example:
function xmljsDOMExample() {
var xml;
xml = ""
+ "<?xml version=\"1.0\"?>"
+ "<ROOTNODE attribute=\"attributeValue\">"
+ "<TAG1 foo=\"goo\">"
+ "Hello World"
+ "<!--comment-->"
+ "<![CDATA[this child is of <<<>nodeType CDATA]]>"
+ "<? hello ?>"
+ "</TAG1>"
+ "</ROOTNODE>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//get the root node
var docRoot = domDoc.getDocumentElement();
//get the "TAG1" element
var tag1 = docRoot.getElementsByTagName("TAG1").item(0);
//item(1) is the comment. The previousSibling of the
//comment is "Hello World" so
//the following should be "Hello World"
alert(tag1.getChildNodes().item(1).getPreviousSibling().getNodeValue());
}// end function xmljsDOMExample
DOMNode - getNextSibling method
W3C DOM Level 1
node.getNextSibling();
- or -
node.nextSibling;
accepts:
N/A
returns:
DOMNode - the next sibling of the queried DOMNode object
throws:
N/A
getNextSibling is a convience method for accessing the DOMNode
immediately following the queried DOMNode. If there is no such node, null is returned.
NOTE: It is also possible to access the next sibling of the
DOMNode by accessing the nextSibling property. This property should be treated as
read-only and should never be set in code.
Example:
function xmljsDOMExample() {
var xml;
xml = ""
+ "<?xml version=\"1.0\"?>"
+ "<ROOTNODE attribute=\"attributeValue\">"
+ "<TAG1 foo=\"goo\">"
+ "Hello World"
+ "<!--comment-->"
+ "<![CDATA[this child is of <<<>nodeType CDATA]]>"
+ "<? hello ?>"
+ "</TAG1>"
+ "</ROOTNODE>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//get the root node
var docRoot = domDoc.getDocumentElement();
//get the "TAG1" element
var tag1 = docRoot.getElementsByTagName("TAG1").item(0);
//item(0) is the "Hello World". The next sibling of the
//"Hello World" is the comment so
//the following should be "comment"
alert(tag1.getChildNodes().item(0).getNextSibling().getNodeValue());
}// end function xmljsDOMExample
DOMNode - getAttributes method
W3C DOM Level 1
node.getAttributes();
- or -
node.attributes;
accepts:
N/A
returns:
DOMNamedNodeMap - A DOMNamedNodeMap containing the queried DOMNodes' attributes
throws:
N/A
The getAttributes method of a DOMNode will return all of the attributes of a DOMNode in a DOMNamedNodeMap.
The attributes in the DOMNamedNodeMap are of type DOMNode and can be queried via the methods
of the DOMNamedNodeMap object.
The getAttributes method only exists for DOMNodes of type DOMNode.ELEMENT_NODE. If the queried DOMNode
is not of this type, null is returned.
If there are no attributes in the DOMNode, an empty DOMNamedNodeMap is returned.
NOTE: It is also possible to access the attributes of the
DOMNode by accessing the attributes property. This property should be treated as
read-only and should never be set in code.
Example:
function xmljsDOMExample() {
var xml;
xml = ""
+ "<?xml version=\"1.0\"?>"
+ "<ROOTNODE attribute=\"attributeValue\">"
+ "<TAG1 foo=\"goo\">"
+ "Hello World"
+ "<!--comment-->"
+ "<![CDATA[this child is of <<<>nodeType CDATA]]>"
+ "<? hello ?>"
+ "</TAG1>"
+ "</ROOTNODE>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//get the root node
var docRoot = domDoc.getDocumentElement();
//get the "TAG1" element
var tag1 = docRoot.getElementsByTagName("TAG1").item(0);
//the following should be "goo"
alert(tag1.getAttributes().getNamedItem("foo").getNodeValue());
}// end function xmljsDOMExample
DOMNode - getNamespaceURI method
W3C DOM Level 2
node.getNamespaceURI;
- or -
node.namespaceURI;
accepts:
N/A
returns:
String - the namespace associated with the queried DOMNode
throws:
N/A
The getNamespaceURI method returns the namespace URI associated with the queried DOMNode.
NOTE: It is also possible to access the namespace URI of the
DOMNode by accessing the namespaceURI property. This property should be treated as
read-only and should never be set in code.
Example:
function xmljsDOMExample() {
var xml =""
+ "<?xml version=\"1.0\"?>"
+ "<ns1:ROOTNODE "
+ "xmlns:ns1=\"http://xmljs.sf.net/ns1\" "
+ "xmlns:ns2=\"http://xmljs.sf.net/ns2\">"
+ "<ns1:TAG1>"
+ "ns1 Hello world"
+ "</ns1:TAG1>"
+ "<ns2:TAG1>"
+ "ns2 Hello world"
+ "</ns2:TAG1>"
+ "</ns1:ROOTNODE>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//namespaceAware is true by default
//this line is here only for demonstration purposes
parser.namespaceAware = true;
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//get the root node
var docRoot = domDoc.getDocumentElement();
//get the DOMNodeList for TAG1 with the ns1 namespace
var ns1 = "http://xmljs.sf.net/ns1";
var tag1 = docRoot.getElementsByTagNameNS(ns1, "TAG1").item(0);
//the following should be "http://xmljs.sf.net/ns1"
alert(tag1.getNamespaceURI());
} // end function xmljsDOMExample
DOMNode - getPrefix method
W3C DOM Level 2
node.getPrefix();
- or -
node.prefix;
accepts:
N/A
returns:
String - the namespace prefix of the queried DOMNode
throws:
N/A
The getPrefix method returns the namespace prefix of queried DOMNode.
NOTE: It is also possible to access the prefix of the
DOMNode by accessing the prefix property. This property should be treated as
read-only and should never be set in code.
Example:
function xmljsDOMExample() {
var xml =""
+ "<?xml version=\"1.0\"?>"
+ "<ns1:ROOTNODE "
+ "xmlns:ns1=\"http://xmljs.sf.net/ns1\" "
+ "xmlns:ns2=\"http://xmljs.sf.net/ns2\">"
+ "<ns1:TAG1>"
+ "ns1 Hello world"
+ "</ns1:TAG1>"
+ "<ns2:TAG1>"
+ "ns2 Hello world"
+ "</ns2:TAG1>"
+ "</ns1:ROOTNODE>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//namespaceAware is true by default
//this line is here only for demonstration purposes
parser.namespaceAware = true;
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//get the root node
var docRoot = domDoc.getDocumentElement();
//get the DOMNodeList for TAG1 with the ns1 namespace
var ns1 = "http://xmljs.sf.net/ns1";
var tag1 = docRoot.getElementsByTagNameNS(ns1, "TAG1").item(0);
//the following should be "ns1"
alert(tag1.getPrefix());
} // end function xmljsDOMExample
DOMNode - getLocalName method
W3C DOM Level 2
node.getLocalName();
- or -
node.localName;
accepts:
N/A
returns:
String - the localName of the DOMNode
throws:
N/A
The getLocalName method returns the name of the DOMNode excluding any namespace information.
NOTE: It is also possible to access the local name of the
DOMNode by accessing the localName property. This property should be treated as
read-only and should never be set in code.
Example:
function xmljsDOMExample() {
var xml =""
+ "<?xml version=\"1.0\"?>"
+ "<ns1:ROOTNODE "
+ "xmlns:ns1=\"http://xmljs.sf.net/ns1\" "
+ "xmlns:ns2=\"http://xmljs.sf.net/ns2\">"
+ "<ns1:TAG1>"
+ "ns1 Hello world"
+ "</ns1:TAG1>"
+ "<ns2:TAG1>"
+ "ns2 Hello world"
+ "</ns2:TAG1>"
+ "</ns1:ROOTNODE>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//namespaceAware is true by default
//this line is here only for demonstration purposes
parser.namespaceAware = true;
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//get the root node
var docRoot = domDoc.getDocumentElement();
//get the DOMNodeList for TAG1 with the ns1 namespace
var ns1 = "http://xmljs.sf.net/ns1";
var tag1 = docRoot.getElementsByTagNameNS(ns1, "TAG1").item(0);
//the following should be "ns1:TAG1"
alert(tag1.getNodeName());
//the following should be "TAG1"
alert(tag1.getLocalName());
} // end function xmljsDOMExample
DOMNode - getOwnerDocument method
W3C DOM Level 1
node.getOwnerDocument;
- or -
node.ownerDocument;
accepts:
N/A
returns:
DOMDocument - The DOMDocument object associated with this node
throws:
N/A
getOwnerDocument is a convience method allowing access to the root DOMDocument object
associated with the queried DOMNode.
NOTE: It is also possible to access the owner document of the
DOMNode by accessing the ownerDocument property. This property should be treated as
read-only and should never be set in code.
Example:
function xmljsDOMExample() {
var xml;
xml = ""
+ "<?xml version=\"1.0\"?>"
+ "<ROOTNODE attribute=\"attributeValue\">"
+ "<TAG1 foo=\"goo\">"
+ "Hello World"
+ "<!--comment-->"
+ "<![CDATA[this child is of <<<>nodeType CDATA]]>"
+ "<? hello ?>"
+ "</TAG1>"
+ "</ROOTNODE>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//get the root node
var docRoot = domDoc.getDocumentElement();
//get the "TAG1" element
var tag1 = docRoot.getElementsByTagName("TAG1").item(0);
//the alert displaying "true" should fire
if (tag1.getOwnerDocument() == domDoc) {
alert("true");
}
else {
alert("false");
}
}// end function xmljsDOMExample
DOMNode - insertBefore method
W3C DOM Level 1
node.insertBefore(<newNode>, <referenceNode>);
accepts:
DOMNode newNode - the DOMNode to insert
DOMNode referenceNode - the DOMNode will be inserted in front of this object
returns:
DOMNode - The successfully inserted child node
throws:
insertBefore is a DOM Manipulation function. Calling insertBefore on a DOMNode will insert
the new DOMNode into the DOMDocument directly in front of the reference object passed in. The
update will be visible throughout the DOMDocument tree.
Example:
function xmljsDOMExample() {
var xml;
xml = ""
+ "<?xml version=\"1.0\"?>"
+ "<ROOT>"
+ "<TAG1>"
+ "Hello"
+ "</TAG1>"
+ "</ROOT>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//get the root node
var docRoot = domDoc.getDocumentElement();
//get the "TAG1" element
var tag1 = docRoot.getElementsByTagName("TAG1").item(0);
//create a new (TAG2) element
var tag2 = domDoc.createElement("TAG2");
//create a new DOMText node
var text = domDoc.createTextNode("New");
//append the TextNode to the new TAG2 element
tag2.appendChild(text);
//add TAG2 to the DOMDocument
docRoot.insertBefore(tag2, tag1);
//the following should be
//"<?xml version=\"1.0\" ?>
//<ROOT>
//<TAG2>
//New
//</TAG2>
//<TAG1>
//Hello
//</TAG1>
//</ROOT>"
alert(domDoc.getXML());
}// end function xmljsDOMExample
DOMNode - replaceChild method
W3C DOM Level 1
node.replaceChild(<newChild>, <referenceChild>);
accepts:
DOMNode newChild - the DOMNode that will replace the referenceChild
DOMNode referenceChild - the DOMNode that will be replaced
returns:
throws:
replaceChild is a DOM Manipulation function. Calling replaceChild on a DOMNode will replace the
referenceChild with the new DOMNode passed in. The update will be visible throughout the DOMDocument tree.
NOTE: If the newChild is already in the tree, it is removed
before the replace is performed.
Example:
function xmljsDOMExample() {
var xml;
xml = ""
+ "<?xml version=\"1.0\"?>"
+ "<ROOT>"
+ "<TAG1>"
+ "Hello"
+ "</TAG1>"
+ "</ROOT>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//get the root node
var docRoot = domDoc.getDocumentElement();
//get the "TAG1" element
var tag1 = docRoot.getElementsByTagName("TAG1").item(0);
//get TAG1's "Hello" child
var tag1HelloChild = tag1.getFirstChild();
//create a new TextNode
var newTextNode = domDoc.createTextNode("New Text Node");
//replace TAG1's "Hello" child with the new child
tag1.replaceChild(newTextNode, tag1HelloChild);
//the following should be
//"<?xml version=\"1.0\" ?>
//<ROOT>
//<TAG1>
//New Text Node
//</TAG1>
//</ROOT>"
alert(domDoc.getXML());
}// end function xmljsDOMExample
DOMNode - removeChild method
W3C DOM Level 1
node.removeChild(<nodeToRemove>);
accepts:
DOMNode nodeToRemove - the DOMNode that will be remove
returns:
throws:
replaceChild is a DOM Manipulation function. Calling removeChild on a DOMNode will remove the
DOMNode passed in. The update will be visible throughout the DOMDocument tree.
Example:
function xmljsDOMExample() {
var xml;
xml = ""
+ "<?xml version=\"1.0\"?>"
+ "<ROOT>"
+ "<TAG1>"
+ "Hello"
+ "<!--remove me -->"
+ "</TAG1>"
+ "</ROOT>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//get the root node
var docRoot = domDoc.getDocumentElement();
//get the "TAG1" element
var tag1 = docRoot.getElementsByTagName("TAG1").item(0);
//get TAG1's comment child
var tag1CommentChild = tag1.getChildNodes().item(1);
//remove TAG1's comment child
tag1.removeChild(tag1CommentChild);
//the following should be
//"<?xml version=\"1.0\" ?>
//<ROOT>
//<TAG1>
//Hello
//</TAG1>
//</ROOT>"
alert(domDoc.getXML());
}// end function xmljsDOMExample
DOMNode - appendChild method
W3C DOM Level 1
node.appendChild(<newChild>);
accepts:
DOMNode newChild - the DOMNode that will be appended
returns:
throws:
Adds the node newChild to the end of the list of children of the DOMNode.
NOTE: If the newChild is already in the tree, it is first removed.
Example:
function xmljsDOMExample() {
var xml;
xml = ""
+ "<?xml version=\"1.0\"?>"
+ "<ROOT>"
+ "<TAG1>"
+ "Hello"
+ "</TAG1>"
+ "</ROOT>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//get the root node
var docRoot = domDoc.getDocumentElement();
//get the "TAG1" element
var tag1 = docRoot.getElementsByTagName("TAG1").item(0);
//create a new comment Node
var newDOMComment = domDoc.createComment("new child comment");
//append the new child to TAG1
tag1.appendChild(newDOMComment);
//the following should be
//"<?xml version=\"1.0\" ?>
//<ROOT>
//<TAG1>
//Hello
//<!--new child comment-->
//</TAG1>
//</ROOT>"
alert(domDoc.getXML());
}// end function xmljsDOMExample
DOMNode - hasChildNodes method
W3C DOM Level 1
node.hasChildNodes();
accepts:
N/A
returns:
boolean - an indication of whether or not the queried DOMNode has child nodes
throws:
N/A
hasChildNodes is a convenience method to allow easy determination of whether a node has any children.
The function returns true if the queried DOMNode has children or false otherwise.
Example:
function xmljsDOMExample() {
var xml;
xml = ""
+ "<?xml version=\"1.0\"?>"
+ "<ROOT>"
+ "<TAG1>"
+ "Hello"
+ "</TAG1>"
+ "</ROOT>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//get the root node
var docRoot = domDoc.getDocumentElement();
//get the "TAG1" element
var tag1 = docRoot.getElementsByTagName("TAG1").item(0);
//the following should be "true"
alert(tag1.hasChildNodes());
//get the first child of tag1
var firstChild = tag1.getFirstChild();
//the following should be "false"
alert(firstChild.hasChildNodes());
}// end function xmljsDOMExample
DOMNode - cloneNode method
W3C DOM Level 1
node.cloneNode(<cloneChildren>);
accepts:
boolean cloneChildren - recursively clone children
returns:
throws:
N/A
The cloneNode method returns a duplicate of the DOMNode that had cloneNode invoked.
A common use for cloneNode is to serve as a generic copy constructor for nodes.
NOTE: The duplicate node has no parent and is not part of the
DOMDocument.
NOTE: The cloned node's namespace attributes
(prefix, localName, and namespaceURI) are also cloned from the source node.
See example 2 for a demonstration.
Example 1:
function xmljsDOMExample() {
var xml;
xml = ""
+ "<?xml version=\"1.0\"?>"
+ "<ROOT>"
+ "<TAG1 name=\"value\">"
+ "Hello"
+ "</TAG1>"
+ "</ROOT>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//get the root node
var docRoot = domDoc.getDocumentElement();
//get the "TAG1" element
var tag1 = docRoot.getElementsByTagName("TAG1").item(0);
var totalClone = tag1.cloneNode(true);
var partialClone = tag1.cloneNode(false);
//the following should be
//"<TAG1 name="value">
//Hello
//</TAG1>"
alert(totalClone.getXML());
//the following should be
//"<TAG1 name="value">
//</TAG1>"
alert(partialClone.getXML());
}// end function xmljsDOMExample
Example 2:
function xmljsDOMExample() {
var xml;
xml = ""
+ "<?xml version=\"1.0\"?>"
+ "<ns1:ROOT xmlns:ns1=\"http://xmljs.sf.net/ns1\" >"
+ "<ns1:TAG1>"
+ "Hello"
+ "</ns1:TAG1>"
+ "</ns1:ROOT>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//get the root node
var docRoot = domDoc.getDocumentElement();
//get the "TAG1" element
var ns1 = "http://xmljs.sf.net/ns1";
var tag1 = docRoot.getElementsByTagNameNS(ns1, "TAG1").item(0);
var clone = tag1.cloneNode(true);
//the following should be "ns1"
alert(clone.getPrefix());
//the following should be "http://xmljs.sf.net/ns1"
alert(clone.getNamespaceURI());
}// end function xmljsDOMExample
DOMNode - getElementsByTagName method
W3C DOM Level 1*
*NOTE: The W3C specification includes
getElementsByTagName only for DOMElements and DOMDocuments. XML for <SCRIPT> implements
getElementsByTagName for all DOMNodes.
node.getElementsByTagName(<tag>);
accepts:
String - the name of the tag to match on
returns:
throws:
N/A
The getElementsByTagName method returns a DOMNodeList of all the Elements with a given tag name
in the order in which they would be encountered in a preorder traversal of the Document tree.
NOTE: The wildcard character "*" can be used to match all nodes.
When "*" is used, the queried node is included in the DOMNodeList as well.
Example:
function xmljsDOMExample() {
var xml;
xml = ""
+ "<?xml version=\"1.0\"?>"
+ "<ROOT>"
+ "<TAG1>"
+ "Hello"
+ "</TAG1>"
+ "<TAG2>"
+ "There"
+ "</TAG2>"
+ "</ROOT>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//get the root node
var docRoot = domDoc.getDocumentElement();
//get the DOMNodeList for any element matching "TAG1"
var tag1NodeList = docRoot.getElementsByTagName("TAG1");
//get the DOMNodeList for any element matching "TAG2"
var tag2NodeList = docRoot.getElementsByTagName("TAG2");
//get the DOMNodeList for any element
//NOTE: docRoot matches "*" as well and will be returned
var allTagsNodeList = docRoot.getElementsByTagName("*");
//the following should be 1
alert(tag1NodeList.getLength());
//the following should be 1
alert(tag2NodeList.getLength());
//the following should be 3
alert(allTagsNodeList.getLength());
}// end function xmljsDOMExample
DOMNode - getElementsByTagNameNS method
W3C DOM Level 2*
*NOTE: The W3C specification includes
getElementsByTagName only for DOMElements and DOMDocuments. XML for <SCRIPT>
implements getElementsByTagName for all DOMNodes.
node.getElementsByTagNameNS(<nameSpaceURI>,<localName>);
accepts:
String namespaceURI - the namespace to search
String localName - the localName to search
returns:
throws:
N/A
The DOM Level 2 getElementsByTagNameNS method is similar to the DOM Level 1 method
getElementsByTagName. The Level 2 functionality adds the capability to search by namespaces.
Example:
function xmljsDOMExample() {
var xml =""
+ "<?xml version=\"1.0\"?>"
+ "<ns1:ROOTNODE "
+ "xmlns:ns1=\"http://xmljs.sf.net/ns1\" "
+ "xmlns:ns2=\"http://xmljs.sf.net/ns2\">"
+ "<ns1:TAG1>"
+ "ns1 Hello world"
+ "</ns1:TAG1>"
+ "<ns2:TAG1>"
+ "ns2 Hello world"
+ "</ns2:TAG1>"
+ "</ns1:ROOTNODE>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//namespaceAware is true by default
//this line is here only for demonstration purposes
parser.namespaceAware = true;
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//get the root node
var docRoot = domDoc.getDocumentElement();
//get the DOMNodeList for TAG1 with the ns1 namespace
var ns1 = "http://xmljs.sf.net/ns1";
var tag1 = docRoot.getElementsByTagNameNS(ns1, "TAG1").item(0);
//the following should be "ns1 Hello world"
alert(tag1.getFirstChild().getNodeValue());
} // end function xmljsDOMExample
DOMNode - setPrefix method
W3C DOM Level 2
DOMNode.setPrefix(<prefix>)
accepts:
String prefix - the new prefix for the node
returns:
N/A
throws:
The setPrefix method sets the prefix of a node to the prefix value passed in.
Example:
function xmljsDOMExample() {
var xml;
xml = ""
+ "<?xml version=\"1.0\"?>"
+ "<ns1:ROOTNODE xmlns:ns1=\"http://xmljs.sf.net/ns1\">"
+ "<ns1:TAG1>"
+ "Hello"
+ "</ns1:TAG1>"
+ "</ns1:ROOTNODE>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//get the root node
var docRoot = domDoc.getDocumentElement();
//give the docRoot a new prefix - note that the namespaceURI
//remains the same
docRoot.setPrefix("foo");
//the following should be
//<foo:ROOTNODE xmlns:ns1="http://xmljs.sf.net/ns1" >
//<ns1:TAG1>
//Hello
//</ns1:TAG1>
//</foo:ROOTNODE>
alert(docRoot.getXML());
}// end function xmljsDOMExample
DOMNode - normalize method
W3C DOM Level 1
node.normalize();
accepts:
N/A
returns:
N/A
throws:
N/A
The normalize method traverses all of the childNodes of the DOMNode and combines all adjacent
DOMNodes of type DOMNode.NODE_TEXT into a single DOMNode.NODE_TEXT. This method is particularly useful if
the DOM has been manipulated in code with multiple DOMNode.NODE_TEXT objects added and
the programmer would like to simplify the object representation of the data.
Example:
function xmljsDOMExample() {
var xml;
xml = ""
+ "<?xml version=\"1.0\"?>"
+ "<ROOT>"
+ "<TAG1>"
+ "Hello"
+ "</TAG1>"
+ "</ROOT>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//get the root node
var docRoot = domDoc.getDocumentElement();
//get the "TAG1" element
var tag1 = docRoot.getElementsByTagName("TAG1").item(0);
var newTextNode = domDoc.createTextNode("There");
tag1.appendChild(newTextNode);
//the following should be 2
alert(tag1.getChildNodes().getLength());
tag1.normalize();
//the following should be 1
alert(tag1.getChildNodes().getLength());
}// end function xmljsDOMExample
DOMNode - isSupported method
W3C DOM Level 2
node.isSupported(<String feature>, <String version>);
accepts:
String feature: A String of the feature to test for
String version: A String of the version of the feature to test for
returns:
boolean - true if the feature/version is supported: false otherwise
throws:
N/A
The isSupported method is a way of testing to determine
if the DOM implementation implements a specific feature.
XML for <SCRIPT>'s W3C DOM Parser supports the DOM level 1 and DOM Level 2 specifications and
will return true if the following information is passed to the method:
-
feature is "xml" and version is "1.0" or "2.0"
-
feature is "core" and version is "2.0"
NOTE: The isSupported method uses the hasFeature method of the
DOMNode's DOMImplementation to return its value. In other words:
node.isSupported("xml", "1.0");
and
node.ownerDocument.implementation.hasFeature("xml", "1.0");
are equivalent;
The feature and version strings are case insensitive
Example:
function xmljsDOMExample() {
var xml;
xml = ""
+ "<?xml version=\"1.0\"?>"
+ "<ROOT>"
+ "<TAG1>"
+ "Hello"
+ "</TAG1>"
+ "</ROOT>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//get the root node
var docRoot = domDoc.getDocumentElement();
//get the "TAG1" element
var tag1 = docRoot.getElementsByTagName("TAG1").item(0);
//the following should be "true"
alert(tag1.isSupported("core", "2.0"));
}// end function xmljsDOMExample
DOMNode - getXML method
XML for <SCRIPT> DOM Extension
node.getXML();
accepts:
N/A
returns:
String - the XML text represented by the DOMNode
throws:
N/A
The getXML method is a convenience wrapper around a DOMNode's toString method. XML for <SCRIPT>'s
W3C DOM Parser overrides the toString methods on many objects to return the underlying XML
of the object. The XML returned contains all the XML of all of the queried DOMNode's child nodes.
Example:
function xmljsDOMExample() {
var xml;
xml = ""
+ "<?xml version=\"1.0\"?>"
+ "<ROOT>"
+ "<TAG1>"
+ "Hello"
+ "</TAG1>"
+ "</ROOT>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//get the root node
var docRoot = domDoc.getDocumentElement();
//get the "TAG1" element
var tag1 = docRoot.getElementsByTagName("TAG1").item(0);
//the following should be
//"<TAG1>
//Hello
//</TAG1>"
alert(tag1.getXML());
}// end function xmljsDOMExample
DOMNode - hasAttributes method
W3C DOM Level 1
DOMNode.hasAttributes();
accepts:
N/A
returns:
boolean - true if the DOMNode has attributes, false otherwise
throws:
N/A
The hasAttributes method is a convience method for determining if a DOMNode has child
attributes or not.
Example:
function xmljsDOMExample() {
var xml;
xml = ""
+ "<?xml version=\"1.0\"?>"
+ "<ROOTNODE>"
+ "<TAG1>"
+ "Hello World"
+ "</TAG1>"
+ "<TAG1 foo=\"goo\" >"
+ "Hello World"
+ "</TAG1>"
+ "</ROOTNODE>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//get the root node
var docRoot = domDoc.getDocumentElement();
//find the tag1 elements
var tag1s = domDoc.getElementsByTagName("TAG1");
//get the first tag1
var firstTag1 = tag1s.item(0);
//the following should be "false"
alert(firstTag1.hasAttributes());
//get the second tag1
var secondTag1 = tag1s.item(1);
//the following should be "true"
alert(secondTag1.hasAttributes());
}// end function xmljsDOMExample