Trying to parse XML

193 views
Skip to first unread message

ozmium

unread,
Apr 3, 2010, 3:59:05 PM4/3/10
to Chromium-extensions
Okay, I've been developing a plugin for Chrome and I'm trying to read
an XML-file. But, I'm having some hard time figuring out how to do
this. Consider that we have an object XMLHttpRequest x and then when I
try x.responseXML it gives me nothing. I've been browsing through
internet trying to find an answer (there is some posts with the same
topic but they are lacking an answer) for this but haven't found any.
My JS looks something like this:

xhttp.open('GET', url, true);
xhttp.setRequestHeader('Content-Type', 'text/xml');
xhttp.send("");
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4) {
xml = xhttp.responseXML;
callback();
}
}

Of course I can get the data with x.responseText but then I can't
really use functions like getElementsByTagName(). There shouldn't be
anything wrong with the XML-file.

Any help would be appreciated.

Finnur Thorarinsson

unread,
Apr 3, 2010, 5:12:38 PM4/3/10
to ozmium, Chromium-extensions

So... responseXML can be blank if the mime type for the document you are downloading is not set to some form of xml mime type. Try:
xhttp.overrideMimeType('text/xml');
... where you set the request header. That will ignore the mime type the server returns and hard-code it to text/xml. Is this maybe what you are trying to accomplish by setting the request header? If so, you can probably replace that line.

Another option, if the mime type thing is not enough is to take the responseText xml and run it through a DOMParser:

    var domParser = new DOMParser();
    var doc = domParser.parseFromString(xhttp.responseText, "text/xml");
    var elements = doc.getElementsByTagName("... your elements ...");
Best regards,
Finnur


--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.
For more options, visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/?hl=en.


ozmium

unread,
Apr 3, 2010, 5:55:42 PM4/3/10
to Chromium-extensions
First of all, thank you for your reply. But still, I got some issues,

Okay, I've tried by setting the MIME-type and also with the DOMParser.
Nothing. Okay, with MIME and without DOMParser and by setting
xhttp.responseText it gives me TypeError: Object ... has no method
'getElementsByTagName'. With the another setting that you described


var domParser = new DOMParser();

domWith var doc = domParser.parseFromString(xhttp.responseText,
"text/xml");
it won't even call the callback-function but stops there.

Can this be because of the structure of the program? Because first it
calls a method to get the xml (background.html), after which it calls
a method to create a new tab which fires an event to get the XML-data
from background.html. I have tried this with the responseText and it
gets the information though in wrong format. Other options given here
and elsewhere hasn't worked. So I guess there's something wrong with
the XML-file (which I guess isn't right because FF reads it and is
from an authority which I believe to be correct in this)? Or am I
missing something here? In callback-function after creating a new tab
I have just implemented a test line something like this:
document.getElementById("div_").innerHTML = "foo";
which I can see with responseText setting but,
I can't even get there if I use xhttp.responseXML.

If I'm using xhttp.ResponseText and something like this:
function callback(data) {
document.getElementById("div_").innerHTML = data;
}
I can see the XML-data in the div as Chrome displays it by the default
but I'm not able to use the methods as I described in the first post.

ozmium

unread,
Apr 4, 2010, 5:51:52 AM4/4/10
to Chromium-extensions
Okay, there was something wrong with the other parts of the program. I
got this one solved.
Reply all
Reply to author
Forward
0 new messages