我一直在试图弄清楚如何正确执行此操作,并且我确定自己是一个白痴(这里是代码新手)。我有一个xml文件(如下所示),我正在解析并尝试根据节点" testset"为每个数据集构建多个表。 xml文件不会改变它的设计方式,但会增加多少" testsets"有。我有下面的代码。
我目前有testspassed和testpasssed显示我需要的2个数组,但我无法弄清楚如何将每个数组的总和分开(并期望在更远的地方将有未知数量的数组)然后我必须把它写到桌子上。下面的代码目前在传递的测试中将数组的两个部分加在一起,我需要将它们分开。所以我希望数组设置为137,70。我很感谢任何帮助或建议。
XML
<?xml version="1.0" encoding="utf-8"?>
<TestSets>
<TestSet>
<TestSetID>TestSet_iOS1</TestSetID>
<Hardware>iPad Air 2</Hardware>
<Version>iOS 8.3</Version>
<Build>Pro 2.7.0.13 </Build>
<Orientation>PORTRAIT</Orientation>
<NumTestCases>46</NumTestCases>
<NumTestScripts>17</NumTestScripts>
<TotalTime>230891.363</TotalTime>
<TestResults>
<TestRun>
<Date>24 Mar 2015 09:12:00</Date>
<Hardware>iPad Air 2</Hardware>
<Version>iOS 8.1.3</Version>
<Build>2.7.0.607</Build>
<Orientation>PORTRAIT</Orientation>
<NumPassed>37</NumPassed>
<NumFailed>31</NumFailed>
</TestRun>
<TestRun>
<Date>27 Mar 2015 11:43:18</Date>
<Hardware>iPad Air 2</Hardware>
<Version>iOS 8.1.3</Version>
<Build>Pro 2.7.0.615</Build>
<Orientation>PORTRAIT</Orientation>
<NumPassed>100</NumPassed>
<NumFailed>25</NumFailed>
</TestRun>
</TestResults>
</TestSet>
<TestSet>
<TestSetID>TestSet_iOS2</TestSetID>
<Hardware>iPad Air 2</Hardware>
<Version>iOS 8.1.3</Version>
<Build>Pro 2.7.0.623</Build>
<Orientation>LANDSCAPE</Orientation>
<NumTestCases>38</NumTestCases>
<NumTestScripts>9</NumTestScripts>
<TotalTime>20800.255</TotalTime>
<TestResults>
<TestRun>
<Date>30 Mar 2015 10:29:00</Date>
<Hardware>iPad Air 2</Hardware>
<Version>iOS 8.1.3</Version>
<Build>2.7.0.615</Build>
<Orientation>LANDSCAPE</Orientation>
<NumPassed>34</NumPassed>
<NumFailed>15</NumFailed>
</TestRun>
<TestRun>
<Date>31 Mar 2015 20:00:02</Date>
<Hardware>iPad Air 2</Hardware>
<Version>iOS 8.1.3</Version>
<Build>Pro 2.7.0.620</Build>
<Orientation>LANDSCAPE</Orientation>
<NumPassed>10</NumPassed>
<NumFailed>19</NumFailed>
</TestRun>
<TestRun>
<Date>02 Apr 2015 10:15:25</Date>
<Hardware>iPad Air 2</Hardware>
<Version>iOS 8.1.3</Version>
<Build>Pro 2.7.0.623</Build>
<Orientation>LANDSCAPE</Orientation>
<NumPassed>26</NumPassed>
<NumFailed>12</NumFailed>
</TestRun>
</TestResults>
</TestSet>
</TestSets>
当前代码
document.write("<table><tr><th><st>Test Set ID</st></th><th>Hardware</th><th>Op Sys Version</th><th>App Build</th><th>Orientation</th><th>Number of Test Passed</th><th>Number of Test Failed</th></tr>");
var x = xml.getElementsByTagName("TestSet");
var sum = 0;
for (i = 0; i < x.length; i++) {
document.write("<tr class='dynamictable'><td>");
document.write(x[i].getElementsByTagName("TestSetID")[0].childNodes[0].nodeValue);
document.write("</td><td>");
document.write(x[i].getElementsByTagName("Hardware")[0].childNodes[0].nodeValue);
document.write("</td><td>");
document.write(x[i].getElementsByTagName("Version")[0].childNodes[0].nodeValue);
document.write("</td><td>");
document.write(x[i].getElementsByTagName("Build")[0].childNodes[0].nodeValue);
document.write("</td><td>");
document.write(x[i].getElementsByTagName("Orientation")[0].childNodes[0].nodeValue);
var y = x[i].getElementsByTagName("TestRun");
var testspassed = [];
var testsfailed = [];
for (j = 0; j < y.length; j++) {
testspassed.push(y[j].getElementsByTagName("NumPassed")[0].childNodes[0].nodeValue);
testsfailed.push(y[j].getElementsByTagName("NumFailed")[0].childNodes[0].nodeValue);
}
for (var k=0; k<testspassed.length; k++){
sum+=testspassed[k]<<0;
}
console.log(sum);
}