om.DomNode.appendChild

Adds an existing node at the end of the list of children in the current node.

Syntax

appendChild(
   node om.DomNode )
  1. node is a reference to a node.

Usage

The appendChild() method takes an existing om.DomNode element node and adds it at the end of the children of the object node calling the method.

The child node passed to the appendChild() method must have been created from the same DOM document object, for example with the om.DomDocument.createElement() method.

If the node passed to the appendChild() method is already attached to another parent node, it will be detached from that parent node before being attached to the new parent node.

Example

MAIN
    DEFINE doc  om.DomDocument,
           r om.DomNode,
           p1, p2 om.DomNode,
           c1, c2 om.DomNode

    LET doc = om.DomDocument.create("Items")

    LET r = doc.createElement("Zoo")

    LET p1 = doc.createElement("DodoList")

    -- appends p1 under r
    CALL r.appendChild(p1)

    LET c1 = doc.createElement("Dodo")
    CALL c1.setAttribute("name", "momo")
    CALL c1.setAttribute("gender", "male")
    CALL p1.appendChild(c1)

    LET p2 = doc.createElement("DodoList")
    CALL r.appendChild(p2)
    LET c2 = doc.createElement("Dodo")
    CALL c2.setAttribute("name", "kiki")
    CALL c2.setAttribute("gender", "female")
    CALL p2.appendChild(c2)

    CALL r.writeXml("file1.xml")

    -- moves c1 under p2
    CALL p2.appendChild(c1)

    CALL r.writeXml("file2.xml")
END MAIN

The above program will produce the following XML files:

file.xml
<?xml version='1.0' encoding='ASCII'?>
<Zoo>
  <DodoList>
    <Dodo name="momo" gender="male"/>
  </DodoList>
  <DodoList>
    <Dodo name="kiki" gender="female"/>
  </DodoList>
</Zoo>
file2.xml
<?xml version='1.0' encoding='ASCII'?>
<Zoo>
  <DodoList/>
  <DodoList>
    <Dodo name="kiki" gender="female"/>
    <Dodo name="momo" gender="male"/>
  </DodoList>
</Zoo>