w3schools
  
HOME HTML CSS XML JAVASCRIPT ASP PHP SQL MORE...   References Examples Forum About
BEST WEB HOSTING JustHosts

XML DOM Parser

« Previous Next Chapter »

Most browsers have a build-in XML parser to read and manipulate XML.

The parser converts XML into a JavaScript accessible object (the XML DOM).


XML Parser

The XML DOM contains methods (functions) to traverse XML trees, access, insert, and delete nodes.

However, before an XML document can be accessed and manipulated, it must be loaded into an XML DOM object.

An XML parser reads XML, and converts it into an XML DOM object that can be accessed with JavaScript.

Most browsers have a build-in XML parser.


Load an XML Document

The following JavaScript fragment loads an XML document ("books.xml"):

Example

if (window.XMLHttpRequest)
  {
  xhttp=new XMLHttpRequest();
  }
else // Internet Explorer 5/6
  {
  xhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xhttp.open("GET","books.xml",false);
xhttp.send("");
xmlDoc=xhttp.responseXML;

Try it yourself »

Code explained:

  • Create an XMLHTTP object
  • Open the XMLHTTP object
  • Send an XML HTTP request to the server
  • Set the response as an XML DOM object

Load an XML String

The following code loads and parses an XML string:

Example

if (window.DOMParser)
  {
  parser=new DOMParser();
  xmlDoc=parser.parseFromString(text,"text/xml");
  }
else // Internet Explorer
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async="false";
  xmlDoc.loadXML(text);
  }

Try it yourself »

Note: Internet Explorer uses the loadXML() method to parse an XML string, while other browsers use the DOMParser object.


Access Across Domains

For security reasons, modern browsers do not allow access across domains.

This means, that both the web page and the XML file it tries to load, must be located on the same server.

The examples on W3Schools all open XML files located on the W3Schools domain.

If you want to use the example above on one of your web pages, the XML files you load must be located on your own server.


« Previous Next Chapter »


1,050,724 Sites built with Wix. Make your own!

Click here to design a Stunning Flash Website for Free

Wix is a revolutionary web design tool that provides anyone with the possibility to create professional and beautiful websites for free.

With e-commerce features, search engine visibility and many more professional tools, Wix is the ultimate solution for creating a spectacular site while saving tons of money.


WEB HOSTING
Best Web Hosting
PHP MySQL Hosting
Top 10 Web Hosting
UK Reseller Hosting
Web Hosting
FREE Web Hosting
Top Web Hosting
Windows Hosting
WEB BUILDING
Download XML Editor
FREE Flash Website
FREE Web Templates
FLIGHT TICKETS
Find the cheapest flight
to any destination now!
EDUCATION
US Web Design Schools
HTML Certification
JavaScript Certification
XML Certification
PHP Certification
ASP Certification
STATISTICS
Browser Statistics
Browser OS
Browser Display