DomAPI Home
DomAPI
Author: Patrice Fricard 06/19/2002
Modified: Darin Kadrioski 11/01/2004
XMLCore

Xmlcore in practice

Overview

This is a tutorial on using xmlcore extension to load and use Xml Dom Document.

Three samples:

  • loadXML: Create an XML Dom document and load it using a string
  • synchronous load: Create an XML Dom document and load an Xml document synchronously
  • asynchronous load: Create an XML Dom document and load an Xml document asynchronously

Pre-requisite

core.loadUnit("xmlcore")

LoadXml

This method show how to simply load an xml into a Dom Document.

//create a dom document
xmldoc = core.xml.getDomDocument();
//load an xml string
xmldoc.loadXML("<xml><node>test</node></xml>");

Synchronous load

You can use it to load an xml document from a server.For security reason you can only load those file from a remote source.The load function prevent any access to the local file system.

//create a dom document
xmldoc = core.xml.getDomDocument();
//set the loading mode to synchronous (default is asynchrous)
xmldoc.async =false;
//load the xml file (could be a static file or a dynamic one which output a well-formed xml) 
xmldoc.load("myxml.xml");

Asynchronous load

Two steps are needed in this case

Step 1: create a function to handle result and link it to the Dom Document

//create a dom document
xmldoc = core.xml.getDomDocument();

//this function will be called once the xml document is loaded
xonload = function(){
alert(xmldoc.xml);
}

xmldoc.onreadystatechange = xonload;
e

Step 2: set mode to asynchronous and load file

//set mode to asynchronous (not require cause it's the default value ;)
xmldoc.async =true;
//load the xml file 
xmldoc.load("myxml.xml");

Use it to load a listbox

Here, it's complete sample.

First load domapi an required library

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html>
<head>
<script src="../src/core.js"></script>
<script type="text/javascript">
 core.loadUnit("xmlcore");
 core.loadUnit("listbox");

we can use them once the page is loaded

onload=function(){
//create a listbox that will be feed by the xmldoc
elm1=Listbox({x:20,y:20,w:100,h:100});
Create a dom and load an xml string
//create dom
xmldoc = core.xml.getDomDocument(null,null);
//load xml
xmldoc.loadXML("<list><item>item1</item><item>item2</item><item>item3</item></list>");

walk through the childNodes and create items in the listbox

var root = xmldoc.documentElement;
	
  for(var a=0;a<root.childNodes.length;a++)
	//nodeType = 1 indicates an Element
	 if(root.childNodes[a].nodeType == 1)
	   this.addItem({text:root.childNodes[a].firstChild.nodeValue});

let's finish the page

}
</script>
</head>
<body>
</body>
</html>
Hope that could help and give some ideas...

   
DHTML by www.domapi.com