Parsing plain xml using jquery from asp.net webservice

I’m cheating on this now, and I don’t know what I am doing wrong. The script, as it should, I use swfupload to upload files with the progress bar through a web service. the web service needs to return the name of the generated thumbnail. This all goes well, and although I prefer to get the returned data in json (may change it later in the swfupload js files), by default the xml data is fine too.

So, when the download is complete, webservice returns the following xml as expected (note: I removed the namespace in the webservice):

<?xml version="1.0" encoding="utf-8"?> <string>myfile.jpg</string> 

Now I want to analyze this result using jquery and thought the following:

  var xml = response; alert($(xml).find("string").text()); 

But I can not get the string value. I tried many combinations (.html () ,. innerhtml (), response.find ("string"). Text () but nothing works. This is my first time when you try to parse xml via jquery, so maybe , I “Doing something fundamentally wrong.” The “answer” is populated with xml.

Hope someone can help me. Thank you for your time.

Regards, Mark

+4
source share
2 answers

I think $ (xml) is looking for a dom object with a selector that matches the string value of XML, so I assume that it returns null or empty?

The first plugin mentioned below xmldom looks pretty good, but if your returned XML is as simple as your example above, the line parsing bit may be faster, for example:

 var start = xml.indexOf('<string>') + 8; var end = xml.indexOf('</string>'); var resultstring = xml.substring(start, end); 

From this answer to this question: How to request XML string via DOM in jQuery

Quote:

There are two ways to approach this.

  • Convert XML string to DOM, parse it using plugin or follow this tutorial
  • Convert XML to JSON using this plugin .
+2
source

jQuery cannot parse XML. If you pass a string filled with XML content to the $ function, it usually tries to parse it as HTML instead of the standard innerHTML . If you really need to parse a string full of XML, you will need browser-supported and non-globally supported methods, such as new DOMParser and XMLDOM ActiveXObject, or a plugin that wraps them.

But you almost never need to do this, because XMLHttpRequest should return a fully parsed XML DOM in the responseXML property. If your web service correctly configured the Content-Type response header to tell the browser that XML is being returned, then the data argument for your callback function should be an XML document object, not a string. In this case, you can use your example with find() and text() without any problems.

If the server side does not return the XML Content-Type header, and you cannot fix it, you can pass the type: 'xml' option to ajax settings as an override.

0
source

All Articles