|
February 10, 2002 Adding A Document Fragment to An XML Tree Tips: February 2002
Yehuda Shiran, Ph.D.
|
|
You can add a document fragment node to an XML tree with the createDocumentFragment() method. The syntax of the method is:
Let's look at an example. First, let's read in our mydvd XML file:
Now, create the new docFragment object:
The node docFragment is a container of three elements, elem1, elem2, and elem3. They hold the proceeds from selling soda, pop-corn, and snacks in our mydvd store. We first create elem1, and assign its content (proceeds in dollars):
We insert elem1 to docFragment using the appendChild() method:
We repeat these steps for elem2 and elem3:
And finally, we append the new docFragment node to the DOM's root:
Let's summarize the above calls in one function:
Try it now. The first alert box echoed the XML file before adding the document fragment node. The second alert box reflects the addition. Notice the three new elements, <soda>, <pop-corn>, and <snacks>.
People who read this tip also read these tips: Look for similar tips by subject: |