40

I have tried and failed to find out how to get the entire XML string from the XMLDocument returned by a GET. There are a lot of questions on SO on how to find or replace specific elements in the object, but I can't seem to find any answer to how to get the entire document as a string.

The example I'm working with is from here. The "do something with xml"-part is where I'm at at the moment. I get the feeling that this should be really trivial, but I fail to find out how. Is there an "xml.data()" or similar that can be used for this purpose?

$.ajax({
    url: 'document.xml',
    type: 'GET',
    dataType: 'xml',
    timeout: 1000,
    error: function(){
        alert('Error loading XML document');
    },
    success: function(xml){
        // do something with xml
    }
});

The use case is that I want to feed the xml to flash plugin and for that I need the actual XML as a string.

icecream
  • 973
  • 2
  • 12
  • 26

6 Answers6

54

If you want both, get the response as XML Document and as string. You should be able to do

success: function(data){
  //data.xml check for IE
  var xmlstr = data.xml ? data.xml : (new XMLSerializer()).serializeToString(data);
  alert(xmlstr);
}

If you want it as string why do you specify dataType:xml wouldn't then dataType:text be more appropriate?

jitter
  • 53,475
  • 11
  • 111
  • 124
  • If I designed an XML object, why would I not have an API to get the data? – icecream Nov 04 '09 at 16:47
  • The XML object is for getting/manipulating data that's stored inside the XML. You want the XML itself, which is a subtly different thing :) – Olly Hodgson Nov 04 '09 at 16:52
  • Strange same answer as BalusC yet not a single upvote nor accepted?? – jitter Nov 04 '09 at 16:52
  • @Olly: What if I want both? Should I then get it as string so and then create an XMLDocument on the client? Changing "xml" to "text" solved my problem now, but I still think there should be a "getData()" function or similar on the XMLDocument. @jitter: I think BalusC answered before you did, but I'll upvote you. – icecream Nov 04 '09 at 17:01
  • provided answer if you want both xmldocument and string – jitter Nov 04 '09 at 17:19
  • Doesn't seem to work for me: TypeError: Argument 1 of XMLSerializer.serializeToString does not implement interface Node. – Yster Jan 06 '15 at 16:41
44

I need the actual XML as a string

You want it as plain text instead of XML object? Change dataType from 'xml' to 'text'. See the $.ajax documentation for more options.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
23

You can also easily convert an xml object to a string, in your java script:

var xmlString = (new XMLSerializer()).serializeToString(xml);
Paul Gorbas
  • 1,694
  • 20
  • 16
  • [jitter's answer](http://stackoverflow.com/a/1675058/516229) is better because it supports IE as well. – ZiggyTheHamster Jan 29 '13 at 17:34
  • 5
    Doesn't seem to work for me: TypeError: Argument 1 of XMLSerializer.serializeToString does not implement interface Node. – Yster Jan 06 '15 at 16:40
1

If you only need a string representing the xml returned from jquery, just set your datatype to "text" rather than trying to parse the xml back into text. The following should just give you raw text back from your ajax call:

$.ajax({
    url: 'document.xml',
    type: 'GET',
    dataType: 'text',
    timeout: 1000,
    error: function(){
        alert('Error loading XML document');
    },
    success: function(xml){
        // do something with xml
    }
});
Ryan Brunner
  • 14,723
  • 1
  • 36
  • 52
1

You can get the native XMLHttpRequest object used in the request. At the time i'm posting this answer, jQuery docs state a few ways to do so.

One of them is via the third argument of the success callback:

success: function(xml, status, xhr){
    console.log(arguments);
    console.log(xhr.responseXML, xhr.responseText);
    console.log('Finished!');
}

For a complete example: https://jsfiddle.net/44m09r2z/

RibeiroBreno
  • 496
  • 5
  • 12
1

Although this question has already been answered, I wanted to point out a caveat: When retrieving XML using jQuery with Internet Explorer, you MUST specify content-type to be "text/xml" (or "application/xml") or else you will not be able to parse the data as if it were XML using jQuery.

You may be thinking that this is an obvious thing but it caught me when using Mozilla/Chrome/Opera instead of IE. When retrieving a "string" of XML with a content-type of "text", all browsers except IE will still allow you to parse that data (using jQuery selectors) as if it were XML. IE will not throw an error and will simply not return any results to a jQuery selection statement.

So, in your example, as long as you only need the string-serialized version of the XML and will not expect jQuery to do any sort of selection on the XML DOM, you can set the content-type to "text". But if you ALSO need to parse the XML with jQuery, you will need to write a custom routine that serializes the XML into a string for you, or else retrieve a version of the XML with content-type "xml".

Hope that helps someone :)

vmfedor
  • 11
  • 1