Gathering Information About a Node
The following method and properties can be used to get the following information about a node.
- hasChildNodes method
If the node has any children.
- namespaceURI property
The namespace to which the node belongs.
- nodeName property
The name of the node.
- nodeType property
The type of the node.
- nodeTypeString property
The type of the node.
- parsed property
If the node has been parsed.
- specified property
If the node is explicitly specified in the XML file or implied within the document type definition (DTD) or XML Schema.
- xml property
The string representation of the node.
The following Microsoft JScript® example code shows how to test the nodeTypeString property to see if the node is of type "element". If the node is of type "element", the string representation of the node is loaded into a new document object through the loadXML method, creating a document fragment.
if (xmlNode.nodeTypeString == "element") {
xmlDoc.loadXML(xmlNode.xml);
if (xmlDoc.parseError.errorCode != 0) {
var myErr = xmlDoc.parseError;
alert("You have error " + myErr.reason);
}
}
You can use the following code if you prefer to work with the numeric representation.
if (xmlNode.nodeType == 1) {
xmlDoc.loadXML(xmlNode.xml);
if (xmlDoc.parseError.errorCode != 0) {
var myErr = xmlDoc.parseError;
alert("You have error " + myErr.reason);
}
}
You can get the name of a node by checking the nodeName property.
currentNodeName=xmlNode.nodeName
You can use the following code to find nodes without child nodes and retrieve their content as a string.
if (xmlNode.hasChildren == false) currentNodeContent=xmlNode.xml
You can use the following code to limit the nodes processed to nodes from specific namespaces.
if (xmlNode.namespaceURI == "http://www.examples.microsof.com/catalog") 'catalog-specific processing here
