Share via


Source: DynamDOM.js

 

This sample application creates a simple, but complete, XML DOM object, with <root> as the document element. This element contains three child elements: <node1>, <node2>, and <node3>. The first child element contains character data. The second child element contains a CDATA section. The last child element contains three empty child elements: <subnode1>, <subnode2>, and <subnode3>.

JScript Source File (DynamDOM.js)

main();

function main()
{
  var dom = MakeDOM(null);

  try {  
    // Create a processing instruction targeted for xml.
    var node = dom.createProcessingInstruction("xml",
                                      "version='1.0'");
    dom.appendChild(node);
    node = null;

    // Create a processing instruction targeted for xml-stylesheet.
    node = dom.createProcessingInstruction(
              "xml-stylesheet",
              "type='text/xml' href='test.xsl'");
    dom.appendChild(node);
    node = null;

    // Create a comment for the document.
    node = dom.createComment(
        "sample xml file created using XML DOM object.");
    dom.appendChild(node);
    node=null;

    // Create the root element.
    var root = dom.createElement("root");

    // Create a "created" attribute for the root element and 
    // assign the "using dom" character data as the attribute value.
    var attr = dom.createAttribute("created");
    attr.value = "using dom";
    root.setAttributeNode(attr);
    attr = null;

    // Add the root element to the DOM instance.
    dom.appendChild(root);

    // Insert a newline + tab.
    root.appendChild(dom.createTextNode("\n\t"));
    
    // Create more nodes and add them to the root element just created.
    
    // Add a text node as <node1>.
    node = dom.createElement("node1");
    node.text = "some character data";
    root.appendChild(node);
    
    // Add a newline + tab.
    root.appendChild(dom.createTextNode("\n\t"));

    // Add a CDATA section as <node2>.
    node=null;
    node = dom.createElement("node2");
    var cd =dom.createCDATASection("some mark-up text");
    node.appendChild(cd);
    cd = null;
    root.appendChild(node);

    // Create an element (<node3>) to hold three empty subelements.
    node = null;
    node = dom.createElement("node3");
      // Create a document fragment to be added to <node3>.
      var frag = dom.createDocumentFragment();
 
      // Add a newline + tab + tab as a text node and an empty subnode.
      frag.appendChild(dom.createTextNode("\n\t\t"));
      frag.appendChild(dom.createElement("subNode1"));

      // Add a newline + tab + tab as a text node and an empty subnode.
      frag.appendChild(dom.createTextNode("\n\t\t"));
      frag.appendChild(dom.createElement("subNode2"));

      // Add a newline + tab + tab as a text node and an empty subnode.
      frag.appendChild(dom.createTextNode("\n\t\t"));
      frag.appendChild(dom.createElement("subNode3"));

      // Add a newline + tab.
      frag.appendChild(dom.createTextNode("\n\t"));
    node.appendChild(frag);
    root.appendChild(node);
    frag=null;
    node=null;
 
    // Add a newline.
    root.appendChild(dom.createTextNode("\n"));

    // Save the XML document to a file.
    dom.save("dynamDom.xml");

    root = null;
    dom = null;
  }
  catch (e)
  {
    alert(e.description);
  }

}

function MakeDOM(progID)
{
  if (progID == null) {
    progID = "msxml2.DOMDocument.6.0";
  }

  var dom;
  try {
    dom = new ActiveXObject(progID);
    dom.async = false;
    dom.validateOnParse = false;
    dom.resolveExternals = false;
  }
  catch (e) {
    alert(e.description);
  }
  return dom;
}

function alert(str)
{
  WScript.Echo(str);
}

To add dynamDOM.js to the project

  1. Copy the code listing above. Paste it into the JScript code editor, and save the file as the dynamDom.js in the current project folder.

Next, run the project. The result should be the output shown in the following topic.