Node creation methods usage examples

Node creation methods usage examples for the xml.DomDocument class.

Creating a node for the DomDocument is done in two steps:

  • Create the node.
  • Add the node to the DomDocument.

Each time you create a node, you need to append it at the right place in the DomDocument. To add a node to the document use the DomDocument management methods or the DomNode manipulation methods.

Creates a structure of nodes.
createElementNS("cny", "Company", "")
Produces <cny:Company xmlns:cny=""/> or <cny:Company />. See Cautions for more details.
Creates a Country attribute node.
  • To set a value to the attribute, use the method setNodeValue of the xml.DomNode class.
  • To add the attribute to an element node, use the method setAttributeNode of the xml.DomNode class.
Produces xmlns:tw="" tw:Town=""
  • To set a value to the attribute use the method setNodeValue of the xml.DomNode class.
  • To add the attribute to an element node use the method setAttributeNodeNS of the xml.DomNode class.
  • For optimization reasons, the namespace is not written beside the attribute until the saving of the DomDocument.
  • When accessing the element node, the namespace is not listed in the list of children. In the example above, tw:Town="" is in the list of children, not xmlns:tw="".
  • To access the namespace during the DomDocument building, use the method normalize first. Normalize writes the namespace declaration at the appropriate place. If there is no previous declaration, it will be accessible as an attribute of this element, otherwise it will be an attribute of one of the ancestors of the element.
createTextNode("My Company")
Creates a text node.
createComment("End of the card")
Produces <!--End of the card-->
createCDATASection("<website><a href=\"\">My 
Produces <![CDATA[<website><a href="">My Company</a></website>]]>
Creates the entity reference &title.
createProcessingInstruction("xml-stylesheet", "type=\"text/xsl\"
Produces <?xml-stylesheet type="text/xsl"href="card.xsl"?>
createDocumentType("Card", NULL, NULL,"<!ELEMENT 
Card (lastname, firstname, company, location)>")
Produces <!DOCTYPE Card [ <!ELEMENT Card (lastname , firstname , company , location)>]>
  • Only inline DTD are supported. The DTD has to been inserted in the DomDocument at an appropriate place.
Is a method that creates a lightweight DomDocument. It represents a subtree of nodes that do not need to conform to well-formed XML rules. This makes DocumentFragment easier to manipulate than a DomDocument.
for i=1 to 5
  let node = doc.createelement("Card")
  call root.appendchild(node) end for
This produces a subtree with 5 Card nodes that do not have any root node. Once the subtree is completed, it can be added to the DomDocument object like any other node.