嗨,我正在使用Java脚本从xml文件提取数据。
下面给出的是我的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>
此处提取并显示时间数据。
但是我不希望xml数据成为html代码的一部分,相反,我希望xml数据位于单独的文件(例如a.xml)中,并将此xml数据加载到html页面并从中解析时间数据。
有人可以帮我吗?对于我正在处理的项目,我希望最好使用xpath解析时间数据。
注释 这篇文章是This stack overflow post的继续,我要感谢@collapsar提供上面的代码。
这是我尝试过的方法,但未正确解析时间值
**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>
我过去曾设法将xml文件加载到html页面并加载数据,但是我从未使用过xpath从xml文件中提取数据,
这对于我正在处理的项目是必要的,因此第一个index.html使用xpath成功地从xml文件中提取了时间数据,但是我无法将xml数据从该代码移至一个单独的文件中
任何帮助都会派上用场, 预先感谢
答案 0 :(得分:1)
感谢大家的帮助, 终于能够做到这一点。下面给出的是所有在绝望中来到这里的人的答案。 谢谢
index.html
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
showResult(xhttp.responseXML);
}
};
xhttp.open("GET", "a.xml", true);
xhttp.send();
function showResult(xml) {
var txt = "";
path = "//Step/Time"
if (xml.evaluate) {
var nodes = xml.evaluate(path, xml, null, XPathResult.ANY_TYPE, null);
var result = nodes.iterateNext();
while (result) {
txt += result.childNodes[0].nodeValue + "<br>";
result = nodes.iterateNext();
}
// Code For Internet Explorer
} else if (window.ActiveXObject || xhttp.responseType == "msxml-document") {
xml.setProperty("SelectionLanguage", "XPath");
nodes = xml.selectNodes(path);
for (i = 0; i < nodes.length; i++) {
txt += nodes[i].childNodes[0].nodeValue + "<br>";
}
}
document.getElementById("demo").innerHTML = txt;
}
</script>
</body>
</html>
a.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>