创建HTML文档以显示XML存储的文档

时间:2018-11-22 03:20:48

标签: html xml groovy

我的XML文档存储如下...

<testsuite errors="0" failures="1" skipped="1" passed="1" tests="3">
    <testcase classname=postAPI name="Validate POST API" status="tested"<PASS/>>
    <testcase classname=getAPI name="Validate GET API" status="tested"<FAIL/>>
    <testcase classname=deleteAPI name="Validate DELETE API" status="untested"<SKIP/>>
   </testcase>
</testsuite>

我需要一个HTML文档,该文档需要读取上面的XML文件,并且这里需要将日志输出以HTML格式显示,以便浏览器显示如下...

TestCaseFeature TestCaseName TestCaseSTATUS

postAPI验证POST API通过

getAPI验证GET API失败

deleteAPI验证DELETE API SKIP

如何通过HTML获得这样的结果?

1 个答案:

答案 0 :(得分:0)

以下是我添加的XML文档的答案

<testsuite errors="0" failures="1" skipped="1" passed="1" tests="3"> 
    <testcase classname="postAPI" name="Validate POST API" status="tested" pass="true"></testcase>
    <testcase classname="getAPI" name="Validate GET API" status="tested" pass="skip"></testcase>
    <testcase classname="deleteAPI" name="Validate DELETE API" status="untested" pass="false"></testcase> 
</testsuite>

您需要使用JavaScript才能打开XML文档

let readXMLFile = (url) => {
    let xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            document.body.innerHTML += "TestCaseFeature TestCaseName TestCaseSTATUS";
            for (let i = 0; i < xhttp.responseXML.getElementsByTagName('testcase').length; i++) {
                let el = xhttp.responseXML.getElementsByTagName('testcase')[i];
                let cname = el.attributes.classname.value;
                let n = el.attributes.name.value;
                let status = el.attributes.status.value;
                let pass = el.attributes.status.value;
                if (typeof pass == 'boolean') {
                    if (pass) {
                        pass = "Pass";
                    } else {
                        pass = "Fail";
                    }
                }
                let newEl = document.createElement('p');
                newEl.innerHTML = `${cname} ${n} ${pass}`;
                document.body.appendChild(newEl);
            }
        } else if (this.readyState == 4 && this.status == 404) {
            document.body.innerHTML = "404 Page Not Found";
        } else if (this.readyState == 4) {
            document.body.innerHTML = "An unexpected error occured! Page is loaded but cannot be sent. Contact us for help.";
        } else {
            document.body.innerHTML = '404 Page NOT Found or some other error has occured';
        }
    }
    xhttp.open("GET", url, true);
    xhttp.send();
}

希望这会有所帮助! 这是我的页面加载 https://darkheart527.github.io/domain/XMLTestingPage/myFirstRequestWithAJAX.html 源代码 https://github.com/DarkHeart527/domain/tree-save/gh-pages/XMLTestingPage/myFirstRequestWithAJAX.html