简体   繁体   中英

Parsing XML from URL with plain javascript

I am trying to create a chart (with amCharts) from data we store in one of our internal Sharepoint lists.

These lists can thankfully be converted to a XML format via a RESTful URL query. These queries look like this:

https://sharepoint/_vti_bin/owssvr.dll?Cmd=Display&List=%7B812FC713-F916-4284-B50F-2EC3B8E80C98%7D&View=%7BAAFAA130-33B1-4A21-9E98-C25FECEF79E4%7D&XMLDATA=TRUE?Cmd=Display&List=%7B812FC713-F916-4284-B50F-2EC3B8E80C98%7D&View=%7BAAFAA130-33B1-4A21-9E98-C25FECEF79E4%7D&XMLDATA=TRUE

The resulting XML data doesn't have a standard header / mime-type / etc. though and goes like:

<xml xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882'
 xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882'
 xmlns:rs='urn:schemas-microsoft-com:rowset'
 xmlns:z='#RowsetSchema'>

<s:Schema id='RowsetSchema'>
...
</s:Schema>

<rs:data>
...
</rs:data>

</xml>

Now what I need to do in order to transform and work with the data in javascript, so that I can ultimately insert it into the chart, is to be able to parse it in my web application (html / php / plain js).

Normally I'd do a xmlhttprequest() in this case, however, I have tried this already and it failed me. Seemingly it can't utilize an URL as the xml source. Unfortunately there aren't any error messages either...

Here's the function I've used (including debug alerts):

<script type="text/javascript"> 
function loadXMLDoc(XMLname)
{
alert("Function loaded");
var xmlDoc;
if (window.XMLHttpRequest)
{
alert("Chrome, getting XML");
xmlDoc=new window.XMLHttpRequest();
xmlDoc.open("GET",XMLname,false);
xmlDoc.send("");
alert("Chrome, got XML");
return xmlDoc.responseXML;
}
// IE 5 and IE 6
else if (ActiveXObject("Microsoft.XMLDOM"))
{
alert("IE, getting XML");
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load(XMLname);
alert("IE, got XML");
return xmlDoc;
}
alert("Error loading document!");
return null;
}
</script>

The function loads correctly, then correctly chooses the Chrome/Firefox path and starts requesting the data via a "GET" query.

This query doesn't ever give back a result or error message though and the

alert("Chrome, got XML");

is never reached.

Does anyone have any idea why there are no error messages or how to actually parse a XML URL with javascript?

best regards

daZza

I assume you are using SharePoint 2010. It is not clear why you are consuming SharePoint Foundation RPC Protocol methods to access SharePoint List data.

In addition to RPC , SharePoint 2010 supports REST , JSOM or SOAP Services .

The following example demonstrate how to read list items using SharePoint 2010 REST API:

function getListItems(webUrl,listName, success, failure) {
    var url = webUrl + "/_vti_bin/listdata.svc/" + listName;
    $.ajax({
        url: url,
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: function (data) {
            success(data.d.results);
        },
        error: function (data) {
            failure(data.responseJSON.error);
        }
    });
}


//Usage
getListItems('https://contoso.sharepoint.com/project/','Tasks',function(taskItems){
    console.log(taskItems); 
    for(var i = 0; i < taskItems.length;i++) {
        console.log(taskItems[i].TaskName);
    }
  },
  function(error){
    console.log(JSON.stringify(error));
  }
);

References

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM