|
February 5, 2002 Adding An Attribute Node to An XML Tree Tips: February 2002
Yehuda Shiran, Ph.D.
|
|
The addNode() function below creates a node of type "attribute":
The node is an object. Let's add it to our mydvd tree, to the root's first child node. The root of the tree is <sales>. Its first child node, (see the tree representation in Column 101's Page 2), is <summary>. You usually start navigating the tree from the root. The root of the tree is:
The collection of the root's childNodes is:
The first item on this collection is the first child:
The collection of attributes of this node (<summary>) is:
To insert the new node to this collection, you use the setNamedItem() method, with the new node object as a parameter:
Try it now. The first alert box echoes the XML file before adding the attribute node. The second alert box reflects the addition. Notice the Year attribute within the <summary> tag.
People who read this tip also read these tips: Look for similar tips by subject: |