3

I am newbie in javascript xml data handling. Currently, my ajax call to a server, and the server returns xml data, I don't know how to parse the xml data to get some values.

My ajax call:

$.ajax({
            url: 'http://localhost:8080/someinterface/the-id',
            type: 'GET',
            async: false,
            dataType: 'application/xml',
            data: {id: 43},
            success: function(data) {

                //handle the data

            },
            error: function(xhr, status, error){
                alert('error happens');
            }
         })

the returned xml data looks like:

<DOCUMENT>
  <AGE>16</AGE>
  <USERNAME>default user</USERNAME>
  <SECURITYID>1111</SECURITYID>
...

how to parse the xml data to get for example "USERNAME" in my javascript ??

------------EDIT------------

I tried to use your proposed ways, but now I got error message:

XML Parsing Error: no element found Location: moz-nullprincipal:{120e8c1d-5174-4e94-9ebb-2bffda80b170} Line Number 1, Column 1: ^

and the ajax call result to the error function

-------------------PARTLY SOLVED------------------------

Finally, I found the reason is that my application is running on jetty server on localhost:8080, while my ajax is requesting xml data on tomcat server which is running on localhost:8085. Because of the domain port are different, I got the weired problem. Now, after I move my application to tomcat server and request tomcat server, I got the xml response successfully.

The ajax call goes to the success function, and I use

success: function(data){
           xmlDoc = $.parseXML(data);
            $xml = $(xmlDoc);
            $userName = $xml.find('USERNAME');  
            var userName = $userName.text();
            alert('1');
            alert(userName);
}

I got alert('1') in my browser, BUT I do not get alert(userName) , I don't know why... So I put here "PARTLY SOLVED", so...why I do not get my userName alert..

15
  • @Mallon: which one did you use. What is the jquery version u r using? Commented Apr 12, 2011 at 14:10
  • Hi, I have tried all the suggested ways, also tried directly alert out the data like "alert(data)" in success function, but when run it in browser, it calls the error function and pop up the alert in error function. Commented Apr 12, 2011 at 14:13
  • @Mallon: are u sure your server is not returning empty xml? Commented Apr 12, 2011 at 14:17
  • Hi, Amitabh, I am sure my server returned an xml, because I copied and pasted the URL with parameter to the browser, and the browser showd the xml data correctly. The only exception is in Firefox, the browswer popup an message "This XML file does not appear to have any style information associated with it." But it still show the xml data in a xml format like what I showed in the above post. Commented Apr 12, 2011 at 14:24
  • try dataType : 'xml' instead of 'application/xml' Commented Apr 12, 2011 at 14:30

5 Answers 5

3

You can use the jquery xml parsing.

http://api.jquery.com/jQuery.parseXML/

So in your Success function.

success: function(data) {
    //xmlDoc = $.parseXML( data);
    $xml = $( data);
    $userName = $xml.find('USERNAME');  
    var uName = $userName.text();
}
Sign up to request clarification or add additional context in comments.

Comments

1

The browser will basically handle it for you:

success: function(data) {
  var username = $(data).find('username').text();
  // ...
}

Comments

1

You are misusing the dataType parameter. It's not expected to be a MIME type. Instead, it should one of these: "xml", "html", "script", "json", "jsonp" or "text". In your case, "xml" (or omit it completely and jQuery will guess it for you):

Update:

So far, it looks that your problem is that your server side script is crashing and you don't even have valid XML. Whatever, I've put together a small example in case it helps:

$.ajax({
    url: "/test.xml",
    type: "GET",
    async: false,
    dataType: "xml",
    data: {id: 43},
    success: function(data) {
        var output = "Usernames:\n";
        $(data).find("USERNAME").each(function(){
            output += "\n- " + $(this).text();
        });
        alert(output);
    },
    error: function(xhr, status, error){
        alert("error happens");
    }
})

4 Comments

Sorry, I dont get your point, what do you mean about "misusing" the datatype. I checked your link, I found the text "In particular, XML must be declared by the server as text/xml or application/xml for consistent results." If you check my post, I do use dataType: 'application/xml'. But thank you anyhow :)
Hi, I tried omit it, the browser do guessed it for me, but this does not solve my problem, the problem is still there . Thank you.
@Mellon: Please read my answer carefully. I said you can not use dataType: "application/xml" and you reply that you are using it.
Sorry, I misunderstood your answer. I will try it tomorrow and let you know. Thank you:)
0

Use Json data type instead, it's designed to be used with javascript.

Example:

$.ajax({
url: url,
dataType: 'json',
data: data,
success: callback
});

2 Comments

Since this project required xml data response, so I can not use json now.
OK, sorry. Didn't think of that.
0

Use the following snippet in your function to put the XML result string in a wrapped jQuery set.

var xmlString = data;

//IE XML parser needs to use an ActiveXObject
if ($.browser.msie) {
    unwrappedXml = new ActiveXObject("Microsoft.XMLDOM");
    unwrappedXml.async = false;
    unwrappedXml.loadXML(xmlString);
    }
else {
    unwrappedXml = xmlString;
}

var xml = $(unwrappedXml);
xml.find(selector);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.