w3schools    w3Schools
Search W3Schools :
   
HOME HTML CSS XML JAVASCRIPT ASP PHP SQL MORE...   References Examples Forum About
ADVERTISEMENTS

XML Certification
Download XML editor
Custom Programming
 
Table of contents
XML Basic
XML HOME
XML Introduction
XML How to use
XML Tree
XML Syntax
XML Elements
XML Attributes
XML Validation
XML Validator
XML Viewing
XML CSS
XML XSLT

XML JavaScript
XML Parser
XML DOM
XML to HTML
XML HTTP Request
XML Application

XML Advanced
XML Namespaces
XML CDATA
XML Encoding
XML Server
XML DOM Advanced
XML Don't
XML Technologies
XML in Real Life
XML Editors
XML Summary

Examples/Quiz
XML Examples
XML Quiz
XML Exam

Selected Reading
Web Statistics
Web Glossary
Web Hosting
Web Quality

W3Schools Tutorials
W3Schools Forum

Helping W3Schools

 

XML DOM Advanced

previous next

The XML DOM (Document Object Model) defines a standard way for accessing and manipulating XML documents.


The XML DOM

The DOM views XML documents as a tree-structure. All elements can be accessed through the DOM tree. Their content (text and attributes) can be modified or deleted, and new elements can be created. The elements, their text, and their attributes are all known as nodes.

In an earlier chapter of this tutorial we introduced the XML DOM , and used the XML DOM getElementsByTagName() method to retrieve data from a DOM tree.

In this chapter we will describe some other commonly used XML DOM methods. In the examples, we have used the XML file books.xml, and a JavaScript function to load the XML file into an DOM object called xmlDoc.

To learn all about the XML DOM, please visit our XML DOM tutorial.


Get the Value of an Element

The following code retrieves the text value of the first <title> element:

x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
txt=x.nodeValue;

Result:  txt = "Everyday Italian"

Try it yourself


Get the Value of an Attribute

The following code retrieves the text value of the "lang" attribute of the first <title> element:

txt=xmlDoc.getElementsByTagName("title")[0].getAttribute("lang");

Result:  txt = "en"

Try it yourself


Change the Value of an Element

The following code changes the text value of the first <title> element:

x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
x.nodeValue="Easy Cooking";

Try it yourself


Change the Value of an Attribute

The setAttribute() method can be used to change the value of an existing attribute, or to create a new attribute.

The following code adds a new attribute called "edition" (with the value "first") to each <book> element:

x=xmlDoc.getElementsByTagName("book");
for(i=0;i<x.length;i++)
  {
  x[i].setAttribute("edition","first");
  }

Try it yourself


Create an Element

The createElement() method creates a new element node.

The createTextNode() method creates a new text node.

The appendChild() method adds a child node to a node (after the last child).

To create a new element with text content, it is necessary to create both an element node and a text node.

The following code creates an element (<edition>), and adds it to the first <book> element:

newel=xmlDoc.createElement("edition");
newtext=xmlDoc.createTextNode("First");
newel.appendChild(newtext);
x=xmlDoc.getElementsByTagName("book");
x[0].appendChild(newel);

Example explained:

  • Create an <edition> element
  • Create a text node with value = "First"
  • Append the text node to the <edition> element
  • Append the <edition> element to the first <book> element

Try it yourself


Remove an Element

The removeChild() method removes a specified node (or element).

The following code fragment will remove the first node in the first <book> element:

x=xmlDoc.getElementsByTagName("book")[0];
x.removeChild(x.childNodes[0]);

Try it yourself

Note: The result of the example above may be different depending on what browser you use. Firefox treats new lines as empty text nodes, Internet Explorer don't. You can read more about this and how to avoid it in the XML DOM tutorial.


These were a few examples of things you can do with the XML DOM.

To learn all about the XML DOM, please visit our XML DOM tutorial.


previous next


The tools you need to build your web project!

Instant Demo

Ektron CMS400.NET Version 7.6 delivers all of the flexibility and features you need to deploy the Web site you want, quickly and efficiently.

learn more...

 

 

 

6 ways to take your site to the next level with Ektron:

Social Networking Create site stickiness through social networking. Keep it personal, relevant and interactive and they'll come back for more.
Open API Keep it open. Your site needs to be ready and able to connect to outside services. Ektron's open API gives you maximum flexibility.
Document Management Streamline content and document management. Users need to quickly and intuitively find and add information.
Content Authors Empower your content authors. Reduce IT bottlenecks by allowing business users to create and edit Web content and forms.
Taxonomy Climb to the top of search rankings. SEO tools, URL aliasing and eCommerce for your digital marketing strategy
Web 2.0 Tools Add powerful Web 2.0 tools like blogs, wikis, forums, geo-mapping, rating systems and RSS feeds easily.
See why there are 20,000+ Ektron integrations worldwide.
Request an INSTANT DEMO or download a FREE TRIAL today.

 
WEB HOSTING
dotnetbutton
Dynamic button image generation
$15 Domain Name
Registration
Save $20 / year!
Buy UK Domain Names
Register Domain Names
Cheap Domain Names
Cheap Web Hosting
Best Web Hosting
PHP MySQL Hosting
Top 10 Web Hosting
UK Reseller Hosting
Web Hosting
FREE Web Hosting
WEB BUILDING
Website Templates
Flash Templates
Website Builder
Internet Business Opportunity
Custom Programming
FREE Trial or Demo
Web Content Manager
Forms,Web Alerts,RSS
Download XML editor
FREE Flash Website
FREE Web Templates
EDUCATION
US Web Design Schools
HTML Certification
JavaScript Certification
XML Certification
PHP Certification
ASP Certification
Home HOME or Top of Page Validate   Validate   W3C-WAI level A conformance icon Printer Friendly  Printer Friendly

W3Schools is for training only. We do not warrant the correctness of its content. The risk from using it lies entirely with the user.
While using this site, you agree to have read and accepted our terms of use and privacy policy.
Copyright 1999-2009 by Refsnes Data. All Rights Reserved.