Quick References
      ADO
      ASP
      CSS2
      HTML
      JavaScript
      Jet SQL
      VBScript
      WML
      WMLScript
      WSH
      XHTML
      XML DOM
      XSLT

Features
      Knowledge Base
      Tutorials

Partners
     ZVON.ORG
     XML
     Planet Source Code
     VisualBuilder
     Web Design
     Your HTML Source
     XML/XSLT Forums
     ASPAlliance
     Scripts
     
     Programmers Heaven
     Tek-Tips Forums
     Developer Fusion
     Code Project











METHOD:  Document::createDocumentFragment

Document.createDocumentFragment( )

The createDocumentFragment method creates an empty DocumentFragment object. Creating an instance of this object does not automatically include it in the XML Document tree, and so its parentNode property is set to null. In order for it to be included you must use one of the node insertion methods: insertBefore, replaceChild or appendChild.

To demonstrate this method, we shall create a DocumentFragment for the 'names.xml' file to which we shall add three child nodes consisting of new 'name' elements. The whole entity will then be inserted into an appropriate place among the children of the document's root element. Finally the values of all the 'name' elements will be displayed. (The code uses a user-defined function to create and add the child nodes to the DocumentFragment.)

XML:
<names>
   <name>Alice</name>
   <name>Bert</name>
   <name>Charlie</name>
   <name>Diane</name>
   <name>Eric</name>
</names>

Code (JavaScript):
xml_doc = new ActiveXObject("Microsoft.XMLDOM");
xml_doc.async = false;
xml_doc.load("names.xml");

function add_elem(frag, elem, text)
{
   var new_elem = xml_doc.createElement(elem);
   var new_text = xml_doc.createTextNode(text);
   new_elem.appendChild(new_text);
   frag.appendChild(new_elem);
}

doc_frag = xml_doc.createDocumentFragment();

add_elem(doc_frag, "name", "Billy");
add_elem(doc_frag, "name", "Bobby");
add_elem(doc_frag, "name", "Bonnie");

root = xml_doc.documentElement;
root.insertBefore(doc_frag, root.childNodes.item(2));

names = xml_doc.getElementsByTagName("name");
n_names = names.length;
for (i = 0; i < n_names; i++)
   document.write(names[i].firstChild.nodeValue + "<br>");

Output:
Alice
Bert
Billy
Bobby
Bonnie
Charlie
Diane
Eric



 


Copyright 1999-2005 by Infinite Software Solutions, Inc. All rights reserved.
Trademark Information