JavaScript method: XML.getName()

This method returns a string representing the name of the current element or attribute. You must use the other XML get methods to navigate through an XML document. This method is a synonym for getNodeName();.

Syntax
XML.getName();

Arguments

There are no arguments for this method.

Return values

A string or null.

The method returns a string representing the name of the current element or attribute. It returns null if the current node has no name.

Example

This example displays the names of the elements and attributes in an XML document.

This example requires the following sample data:

  • A local XML file (for example, C:\test.xml which contains <document><parent1><child1 attribute1="a" /><child2 /></parent1><parent2><child3 /><child4 /></parent2></document>)
  • A valid XML string (for example, <document><parent1><child1>0123</child1></parent1></document>)
/* Create variables to store XML objects and strings.
*  Use XMLObject to store a valid XML object such as an XML file.
*  Use sourceXMLString to store a valid XML string.
*  The script sets the content of XMLString to sourceXMLString.
*  The script assigns values to the empty variables later on */
var XMLFile = new XML();
XMLFile = XMLFile.setContent( "C:\\test.xml", true );
var sourceXMLString = "<document><parent1><child1>0123</child1></parent1></document>";
var XMLString = new XML();
XMLString = XMLString.setContent( sourceXMLString );
var XMLSource;

function walkXML( elem )
{
 print( "Element " + elem.getName() );
 var node = elem.getFirstAttribute();
 while( node != null )
 {
   var attrName  = node.getName();
   var attrValue = node.getNodeValue();
   print( "Attribute " + attrName + " = " + attrValue );
   node = elem.getNextAttribute();
 }
 var nodeValue = elem.getNodeValue();
 if ( typeof nodeValue == "string" && nodeValue.length > 0 )
 {
  print( "Element value of " + elem.getName() + " is: " + nodeValue );
 }

 var child = elem.getFirstChildElement();
 while( child != null )
 {
   var nodeName  = child.getName();
   var nodeValue = child.getNodeValue();
   walkXML( child );
   child = child.getNextSiblingElement();
 }
}

print( "Now printing the structure of an XML file...\n" );
XMLSource = XMLFile
walkXML( XMLSource );

print( "Now printing the structure of an XML string...\n" );
XMLSource = XMLString
walkXML( XMLSource );