| View previous topic :: View next topic |
| Author |
Message |
david
Joined: 27 Aug 2007 Posts: 14
|
Posted: Wed Apr 01, 2009 4:25 am Post subject: Traversing through XML file using Java Script |
|
|
Hi,
I have a XML file with following data.
- <NewDataSet>
- <Table>
<DOCDESC>TEST</DOCDESC>
<DATETIME>03/29/94 12:00:28</DATETIME>
<COMPANY>*</COMPANY>
<PAGE>1</PAGE>
<SECTION>42</SECTION>
<DOCTYPE>G</DOCTYPE>
</Table>
- <Table>
<DOCDESC>TEST1</DOCDESC>
<DATETIME>03/29/94 11:59:44</DATETIME>
<COMPANY>*</COMPANY>
<PAGE>1</PAGE>
<SECTION>44</SECTION>
<DOCTYPE>G</DOCTYPE>
</Table>
</NewDataSet>
I have to create Hyper link for each element of xml file in a html page. please post the code in javascript for this task. it is very urgent.
thanks in advance,
regards,
david |
|
| Back to top |
|
 |
vamsy
Joined: 21 Aug 2007 Posts: 61
|
Posted: Wed Apr 01, 2009 4:28 am Post subject: |
|
|
Hi,
here is the code for your requirement. simple copy the code and use it.
note: html file and XML file both should be in same directory.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/javascript" language="javascript">
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
function loadXML(xmlFile)
{
xmlDoc.async="false";
xmlDoc.onreadystatechange=verify;
xmlDoc.load(xmlFile);
xmlObj=xmlDoc.documentElement;
}
function verify()
{
// 0 Object is not initialized
// 1 Loading object is loading data
// 2 Loaded object has loaded data
// 3 Data from object can be worked with
// 4 Object completely initialized
if (xmlDoc.readyState != 4)
{
return false;
}
}
function display()
{
var len = xmlObj.childNodes.length;
for(var i=0; i<len; i++)
{
document.getElementById('data').innerHTML = document.getElementById('data').innerHTML + " <a href = 'http://www.google.com' style = 'text-decoration:none;'>" + xmlObj.childNodes(i).childNodes(0).firstChild.text + " " + xmlObj.childNodes(i).childNodes(1).firstChild.text+ "</a><br/>";
}
}
</script>
</head>
<body onLoad="loadXML('test.xml');display();">
<div id="data">
</div>
</body>
</html>
I hope this will meet your requirement.
regards,
vamsy |
|
| Back to top |
|
 |
|