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

PHP MySQL Hosting
Get a Freelancer
Download XML editor

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

XML Examples

XML Examples
XML Quiz
XML Exam

 

XML Parser

previous next

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

The parser converts XML into a JavaScript accessible object.


Parsing XML

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.

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


Load an XML Document

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

var xmlDoc;
xmlDoc=new window.XMLHttpRequest();
xmlDoc.open("GET","books.xml",false);
xmlDoc.send("");

Code explained:

  • Create an XMLHTTP object
  • Open the XMLHTTP object
  • Send an XML HTTP request to the server

Load an XML String

The following code loads and parses an XML string:

try //Internet Explorer
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async="false";
  xmlDoc.loadXML(txt);
  return xmlDoc;
  }
catch(e)
  {
  parser=new DOMParser();
  xmlDoc=parser.parseFromString(txt,"text/xml");
  return xmlDoc;
  }

Note: Internet Explorer uses the loadXML() method to parse an XML string, while other browsers uses 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.


The XML DOM

In the next chapter of this tutorial, you will learn how to access and retrieve data from the XML document object (the XML DOM).


previous next




W3Schools Certification

W3Schools' Online Certification Program

The perfect solution for professionals who need to balance work, family, and career building.

More than 3500 certificates already issued!

The HTML Certificate documents your knowledge of HTML, XHTML, and CSS.

The JavaScript Certificate documents your knowledge of JavaScript and HTML DOM.

The XML Certificate documents your knowledge of XML, XML DOM and XSLT.

The ASP Certificate documents your knowledge of ASP, SQL, and ADO.

The PHP Certificate documents your knowledge of PHP and SQL (MySQL).

 
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
Get a Freelancer
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
W3Schools.com HOME | TOP | PRINT | FORUM | ABOUT
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.