1

I'm currently developing a Firefox add-on(using https://addons.mozilla.org/en-US/developers/docs/sdk/1.0/ ) that consumes an API where the return data is in xml.

My problem is that I need to parse the returned data, and would like to do that using a xml object.

Since the request module only supports JSON and Text ( https://addons.mozilla.org/en-US/developers/docs/sdk/1.0/packages/addon-kit/docs/request.html#Response ) I need to convert the response.text to XML. The code looks like this:

var Request = require('request').Request  
.......
var req = Request({
        url: https://to-the-api.com,
        content: {
          op: 'get-the-data-op',
          password: "super-sec",
          user: "username"
        },
        onComplete: function (response) {

         dataAsText = response.text;
         console.log("output: " + dataAsText);
        }
    });
    req.post();

I have tried to user (new DOMParser).parseFromString(response.text, 'text/xml') but unfortunately it just fails with a error like ReferenceError: DOMParser is not defined

The question is if anyone of you guys have been able to create a Xml object inside a Firefox add-on, and if so, how?

  • Related: http://stackoverflow.com/questions/9171590/how-to-parse-a-xml-string-in-a-firefox-addon-using-add-on-sdk/13089414#13089414 – cprcrack Oct 26 '12 at 15:02

4 Answers4

1

Looks like the capability to parse response as xml was present, but has been removed. check out this bugzilla reference

Bhaskar
  • 11
  • 1
0

If DOMParser is unavailable you can try E4X:

var xml = new XML(response.text);
alert(xml.children().length());
Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
  • Unfortunately I forgot to say that I have tried new XML but without luck. Code like: `x = new XML('01230456'); console.log(x)` Just ends up in errors like: `error: An exception occurred. Traceback (most recent call last): TypeError: can't wrap XML objects` – Olle Dahlström Jun 28 '11 at 16:29
0

Can't you use a normal XMLHttpRequest if you want to process the response as XML?

Matthew Gertner
  • 4,487
  • 2
  • 32
  • 54
  • Yeah, why did I not think about that...Thanks a lot. The `XMLHttpRequest` surly have some limitations compared to the `Request`object, but I do think this solution will do for me... – Olle Dahlström Jun 29 '11 at 13:35
0

You want to use the XMLHttpRequest object to handle your xhr request. Then when you get a response back access the responseXML object of the request variable. In the responseXML you'll have the documentElement and can use the querySelectorAll or querySelector to find elements you want. In each element you want just grab the textContent you need.

Here's an example to get you going (this looks for the 'xmls' element in the response):

var request = new require("xhr").XMLHttpRequest();
request.open('GET', 'https://to-the-api.com', true);
request.onreadystatechange = function (aEvt) {
  if (request.readyState == 4) {
     if(request.status == 200) {
      var xmls = request.responseXML.documentElement.querySelectorAll("xmls");
      for (var i = 0; i < xmls.length; i++) {
        console.log("xml", i, xmls[i], xmls[i].textContent);
      }
     }
     else {
       console.log('Error', request.responseText);
     }
  }
};
request.send(null);
Bryan Clark
  • 2,542
  • 1
  • 15
  • 19