Hi i am using java script to extract data from xml file.
The below given is my index.html
index.html
<html>
<head>
<title>Report</title>
<style>
</style>
<script>
function showTheList() {
let x_xmlisland = document.getElementById("template_xml");
let s_xmlsource = x_xmlisland.textContent;
// Parse xml. This may beunnecessary depending on the ajax lib used.
let parser = new DOMParser();
let xmlDoc = parser.parseFromString(s_xmlsource, "application/xml");
// Obtain the xml node set containing the needed info.
// In this case, these are the textual contents of all 'Time' elements that are children of a 'Step' node.
let xpr_time = xmlDoc.evaluate("//Step/Time/text()", xmlDoc, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
let node_time
;
let divBooks = document.getElementById('books'); // THE PARENT DIV.
// debugger; // uncomment for tracing
while ( ( node_time = xpr_time.iterateNext() ) !== null ) { // iterate over xml nodes
let divLeft = document.createElement('div');
divLeft.className = 'col1';
divLeft.innerHTML = node_time.textContent; // The xpath expression references the 'text()' function which provides a text node. String must still be extracted.
divBooks.appendChild(divLeft);
}
}
</script>
</head>
<body onLoad="showTheList()">
<script type="text/xml" id="template_xml"><?xml version="1.0"?>
<Steps>
<Step rID="T6">
<Obj ><![CDATA[Get Data Table - Passed]]></Obj>
<Details ><![CDATA[]]></Details>
<Time><![CDATA[7/5/2018 - 13:16:26]]></Time>
<TimeTick>1530810986</TimeTick>
<NodeArgs eType="User" icon="5" nRep="9" >
<Disp><![CDATA[Get Data Table - Passed]]></Disp>
</NodeArgs>
</Step>
<Step rID="T7">
<Obj ><![CDATA[GetDataTable - Successful]]></Obj>
<Details ><![CDATA[Toral Row get:65534]]></Details>
<Time><![CDATA[7/5/2018 - 13:16:27]]></Time>
<TimeTick>1530810986</TimeTick>
<NodeArgs eType="User" icon="5" nRep="10" status="Passed" >
<Disp><![CDATA[GetDataTable - Successful]]></Disp>
</NodeArgs>
</Step>
</Steps>
</script>
<p>Results of <b>Test cases</b> </p>
<div id="books"></div>
</body>
</html>
Here the time data is extracted and displayed.
But i dont want the xml data to be part of html code, instead i want the xml data to be in a seperate file , say a.xml, and load this xml data to the html page and parse the time data from it.
can anyone help me with this? for the project i am working on i want the time data to be parsed using xpath preferably.
note This post is a continuation of This stack overflow post , and i would like to thank @collapsar for the code above.
This is what i have tried , but it is not parsing the time value correctly
**index.html**
<html>
<head>
<title>Report</title>
<style>
</style>
<script>
var oXHR = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
var testcase_Number = 0;
var endOfTest= 0;
function reportStatus() {
if (oXHR.readyState == 4) // REQUEST COMPLETED.
{
showTheList(this.responseXML); // ALL SET. NOW SHOW XML DATA.
}
}
oXHR.onreadystatechange = reportStatus;
oXHR.open("GET", "a.xml", true); // true = ASYNCHRONOUS REQUEST (DESIRABLE), false = SYNCHRONOUS REQUEST.
oXHR.send();
function showTheList(ab) {
//let x_xmlisland = document.getElementById("template_xml");
let s_xmlsource = ab.textContent;
// Parse xml. This may beunnecessary depending on the ajax lib used.
let parser = new DOMParser();
let xmlDoc = parser.parseFromString(s_xmlsource, "application/xml");
// Obtain the xml node set containing the needed info.
// In this case, these are the textual contents of all 'Time' elements that are children of a 'Step' node.
let xpr_time = xmlDoc.evaluate("//Step/Time/text()", xmlDoc, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
let node_time
;
let divBooks = document.getElementById('books'); // THE PARENT DIV.
// debugger; // uncomment for tracing
while ( ( node_time = xpr_time.iterateNext() ) !== null ) { // iterate over xml nodes
let divLeft = document.createElement('div');
divLeft.className = 'col1';
divLeft.innerHTML = xpr_time.textContent; // The xpath expression references the 'text()' function which provides a text node. String must still be extracted.
divBooks.appendChild(divLeft);
}
}
</script>
</head>
<body >
<p>Results of <b>Test cases</b> </p>
<div id="books"></div>
</body>
</html>
a.xml
<Steps>
<Step rID="T6">
<Obj ><![CDATA[Get Data Table - Passed]]></Obj>
<Details ><![CDATA[]]></Details>
<Time><![CDATA[7/5/2018 - 13:16:26]]></Time>
<TimeTick>1530810986</TimeTick>
<NodeArgs eType="User" icon="5" nRep="9" >
<Disp><![CDATA[Get Data Table - Passed]]></Disp>
</NodeArgs>
</Step>
<Step rID="T7">
<Obj ><![CDATA[GetDataTable - Successful]]></Obj>
<Details ><![CDATA[Toral Row get:65534]]></Details>
<Time><![CDATA[7/5/2018 - 13:16:27]]></Time>
<TimeTick>1530810986</TimeTick>
<NodeArgs eType="User" icon="5" nRep="10" status="Passed" >
<Disp><![CDATA[GetDataTable - Successful]]></Disp>
</NodeArgs>
</Step>
</Steps>
I have managed to load xml file into html page and load data in the past, but i have never worked with xpath for extracting data from xml file,
which is neccessary for the project i am working with, so the first index.html successfully extract time data from the xml file using xpath, but i cant move the xml data to a whole seperate file from that code
Any help will come handy, Thanks in advance
showTheListfunction ;-)