0

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

4
  • Make a HTTP GET request to the file using whatever method you find most suitable (fetch, $.get, axios.get, ...) and parse what comes back Commented Jul 9, 2018 at 16:33
  • @Luca , edited the post now, please have a look at it,Thanks Commented Jul 9, 2018 at 17:27
  • You arent accepting any arguments in the showTheList function ;-) Commented Jul 9, 2018 at 17:31
  • @Luca, thanks for that! but still it is not outputting time value, it seems that xpr_time has null value in it, and obviously xpath parsing didnt happend Commented Jul 9, 2018 at 17:56

1 Answer 1

1

Thanks everyone for the help, Finally was able to do it..below given is the answer to any folks who come here in desperation. Thanks

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.