4.Displaying XML using javascript

/* Click here to see a list of all XML tutorials */

A number of porgramming and scripting languages like VB.net javascript etc have the ability to read  XML.

While reading an XML files using javascript, one must remeber which browser it is to be displayed in. Different browsers read XML in different ways.
For eg   IE explorer uses the ActiceXObject    while      Firefox,Opera on the other hand uses  document.implementation.createDocument method.
If you are not sure  which browser will be used, then use both methods.

Now for the company .xml

company.xml

<?xml version=”1.0″ encoding=”ISO-8859-1″?>
<company>
<employee>
<name>Jack</name>
<post>Manager</post>
<employee>
<company>
<employee>
<name>John</name>
<post>CEO</post>
<employee>
</company>

so open notepad(or any other text editor):

<!– @page { margin: 2cm } P { margin-bottom: 0.21cm } –>

company.html
<!– @page { margin: 2cm } P { margin-bottom: 0.21cm } –> <!– @page { margin: 2cm } P { margin-bottom: 0.21cm } –><html>
<body>
<script type=”text/javascript”>    //usual javascript syntax
var xmlDoc=null;
if (window.ActiveXObject)
{// code for IE
xmlDoc=new ActiveXObject(“Microsoft.XMLDOM”);
}
else if (document.implementation.createDocument)
{// code for Mozilla, Firefox, Opera, etc.
xmlDoc=document.implementation.createDocument(“”,”",null);
}
else
{
alert(‘Your browser cannot handle this script’);
}
if (xmlDoc!=null)
{
xmlDoc.async=false;
xmlDoc.load(“SalesOrder.xml”);
var x=xmlDoc.getElementsByTagName(“SALESORDER”);

//The information is to be displayed in a table form so we start writing the HTML table code using javascript

document.write(“<table border=’1′>”);
document.write(“<thead>”);
document.write(“<tr><th >Empolyee Name</th><th >Post</th></tr>”);//writing the table headers

var x=xmlDoc.getElementsByTagName(“company”); //creating a variable x which gets all elements having company tag

for (var i=0;i<x.length;i++)
{
document.write(“<tr>”); //start row
document.write(“<td>”);

//  To get employeename ,we first must go to employee then look inside for name tag       so,    x[i] (company)–>employee–> name
document.write(x[i].getElementsByTagName(“employee”)[0].getElementsByTagName(“name”)[0].childNodes[0].nodeValue);
document.write(“</td>”);

document.write(“<td>”);
document.write(x[i].getElementsByTagName(“DATE”)[0].childNodes[0].nodeValue);
document.write(“</td>”);
document.write(“<td>”);

document.write(“</tr>”);//end row

}

</script>

</body>
</html>

for more information visit : w3schools.com

Hope this help
Roy

document.implementation.createDocumen

/* Click here to see a list of all XML tutorials */

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.