XML DOM Advanced

Source: Internet
Author: User
XML DOM Advanced XML DOM-Advanced

In earlier chapters of this tutorial, we introduced the XML DOM and used the getElementsByTagName () method of the XML DOM to retrieve the data from the XML document.

In this chapter we will combine some of the other important XML DOM methods.

You can learn more about the XML DOM in our XML DOM tutorials. get the value of an element

The XML file that is used in the following instance: Books.xml.

The following instance retrieves the text value of the first <title> element: instance txt=xmldoc.getelementsbytagname ("title") [0].childnodes[0].nodevalue;
Try it»
gets the value of the property

The following instance retrieves the text value of the "lang" attribute of the first <title> element: instance txt=xmldoc.getelementsbytagname ("title") [0].getattribute (" Lang ");
Try it»
change the value of an element

The following example changes the text value of the first <title> element: instance x=xmldoc.getelementsbytagname ("title") [0].childnodes[0];
X.nodevalue= "Easy Cooking";
Try it»
Create a new property

The SetAttribute () method of the XML DOM can be used to alter an existing property value, or to create a new property.

The following example creates a new property (edition= "first") and adds it to each <book> element: instance x=xmldoc.getelementsbytagname ("book");

for (i=0;i<x.length;i++)
{
X[i].setattribute ("edition", "First");
}
Try it»
Creating Elements

The createelement () method of the XML DOM creates a new element node.

The createTextNode () method of the XML DOM creates a new text node.

The AppendChild () method of the XML DOM adds a child node (after the last child node) to the node.

To create a new element with textual content, you need to create a new element node and a new text node at the same time, and then append it to the existing node.

The following example creates a new element (<edition>) with the following text: First, and then adds it to the initial <book> element: instance newel=xmldoc.createelement (" Edition ");
Newtext=xmldoc.createtextnode ("first");
Newel.appendchild (NewText);

X=xmldoc.getelementsbytagname ("book");
X[0].appendchild (NEWEL);
Try it»

Instance explanation Create a <edition> element create a text node with a value of "first" append this text node to the new <edition> element append <edition> element to the one <book> element Delete Element

The following instance deletes the first node of the first <book> element: instance x=xmldoc.getelementsbytagname ("book") [0];
X.removechild (X.childnodes[0]);
Try it»

Note: The result of the above instance may vary depending on the browser you are using. Firefox treats new line characters as empty text nodes, which is not the case with Internet Explorer. You can read more about this problem and how to avoid it in our XML DOM tutorials.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.