Element class

An Element object represents an XML element node. An element may have other Element, Comment and Text nodes as children. Furthermore an element may have attributes (these are not considered to be children).

Element inherits from the Node class, so all functions defined for Node can be used with Element.

Name

getQualifiedName( ) : String

Returns the qualified element name, i.e. including the namespace prefix if there is one.

e.g.: "prfx:node"

getBaseName( ) : String

Returns the element name without the namespace prefix.

e.g.: "node"

getPrefix( ) : String

Returns the namespace prefix or the empty string if there is none.

e.g.: "prfx"

getNamespaceURI( ) : String

Returns the namespace URI corresponding to namespace prefix or the default namespace URI for the document if there is no prefix or the empty string if there is no prefix and no default namespace.

e.g.: "prefix.txt"

Child nodes

hasChildNodes( ) : Boolean

Returns true if this element has any child nodes, and false if not.

getChildNodes( ) : NodeList

Returns the list of child nodes of this element, in document order or an empty list if the element has no child nodes.

getFirstChild( ) : Node

Returns the first child node of this element or null if there is none.

getLastChild( ) : Node

Returns the last child node of this element or null if there is none.

appendChild( new-child : Node )

Appends a new child node to the list of children for this element.

insertBefore( new-child : Node, ref-child : Node )

Inserts a new child node before the specified reference node, which must be in the list of children of this element.

removeChild( oldChild : Node ) : Node

Removes the specified node from the list of children of this element, and returns the removed node.

replaceChild( newChild : Node, oldChild : Node ) : Node

Replaces the specified child of this element with another node, and returns the removed node.

//example

//create a new empty document
var doc = new Document();
var root = doc.createElement("root");
doc.setDocumentElement(root);

//create a comment  
var comment = doc.createComment("This is a comment");
root.appendChild(comment);

//create another element
var content = doc.createElement("content",null);
root.appendChild(content);

//create text
var text = doc.createText("this is text");
content.appendChild(text);
job.log(1, content.hasChildNodes()?"true":"false"); //"true"

//create element
var title = doc.createElement("title",null);
root.insertBefore(title,comment);

//create another textnode
var text2 = doc.createText("this is the content");

//replace the old text in the content node
content.replaceChild(text2, text);

//remove the first child from root
var first = root.getFirstChild();
root.removeChild(first);

//add attribute (which is also a node)
root.addAttribute("id", null, "123");
//get nodelist
var list = root.getChildNodes();
job.log(1, list.length); //2
doc.save(docPath); //docPath = path to save document to

Result (at docPath):

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <!--This is a comment-->
  <content>this is the content</content>
</root>

Attributes

getAttributes( ) : AttrList

Returns the list of attributes of this element, in arbitrary order. If there are no attributes, returns an empty list.

getAttributeValue( qualified-name : String, prefix-map : Map ) : String

Returns the value for the element's attribute with the specified qualified name or null if there is no such attribute. If the qualified name includes a prefix it is resolved using the map in the second argument, otherwise the second argument may be null.

addAttribute( qualified-name : String, prefix-map : Map, value : String )

Adds an attribute to the element with the specified qualified name and value or replaces an existing attribute with the same name. If the qualified name includes a prefix it is resolved using the map in the second argument, otherwise the second argument may be null.

//example

//create an empty document
var doc = new Document(); 

//create a new element and set this as the document element 
var root = doc.createElement("root");
doc.setDocumentElement(root); 

//create a new prefix-map
var map = doc.createEmptyMap();
map.put('prfx','prefix');

//add attributes
root.addAttribute("prfx:id",map,"1234");
root.addAttribute("id",null,"597");

//get attribute value
var attrvalue = root.getAttributeValue("prfx:id",map);
job.log(1,attrvalue); //"1234" 

//get attributes
var list = root.getAttributes();
job.log(1, list.length); //2