Software Archive
Read-only legacy content
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
17060 Discussions

how to read from a local xml file

fabrizio_d_
Beginner
623 Views

hi, i created a new hybrid html5 app, added one xml file under www folder and this code to a button click

but the code binded to onreadystatechange event never get executed. can someone help me to understand what's wrong?

:Thanks.

$(document).on("click", ".uib_w_1", function(evt)
    {
        var path = window.location.href ;
        path = path.substring( 0, path.lastIndexOf('/') ) ;
        var xmlhttp = new XMLHttpRequest();
        var FileName = path + "/test.xml";
        xmlhttp.open("GET", FileName, false);
        xmlhttp.onreadystatechange  = function(){
            if(xmlhttp.readyState==4){
                response = xmlhttp.responseText;
                var doc = new DOMParser().parseFromString(response, "text/xml");
                console.log(doc);
                var nodes = document.evaluate("/items/item/@id",doc, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);
                console.log(nodes);
                console.log(nodes.snapshotLength);
                for(var i =0; i<nodes.snapshotLength; i++){
                    thisElement = nodes.snapshotItem(i);
                    console.log(thisElement.nodeName);
                }
            }
        };
    });

 

xml file contents:

<?xml version="1.0" encoding="utf-8"?>
<items>
  <item id="1" />
  <item id="2" />
  <item id="3" />
</items>

0 Kudos
1 Reply
Chris_P_Intel
Employee
623 Views

Perhaps there was an error? Probably mal-formed path.

I strongly recommend using a library for this, rather than hand-codding all the XmlHttpRequest stuff from scratch.

jQuery is very popular for this.  For example:

$.ajax("test.xml")
.then(function(data){ console.log("got the data:", data); })
.fail(function(er){ console.log("there was an error", er); })

 

There are other libraries as well. (I've also used qwest: https://github.com/pyrsmk/qwest ).  Google "JS Ajax Library" .

 

0 Kudos
Reply