简体   繁体   中英

parsing simple xml with jquery from asp.net webservice

I'm breaking my head over this for a while now and I have no clue what I do wrong. The scenario is as followed, I'm using swfupload to upload files with a progressbar via a webservice. the webservice needs to return the name of the generated thumbnail. This all goes well and though i prefer to get the returned data in json (might change it later in the swfupload js files) the default xml data is fine too.

So when an upload completes the webservice returns the following xml as expected (note I removed the namespace in webservice):

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

Now I want to parse this result with jquery and thought the following would do it:

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

But I cannot get the string value. I've tried lots of combinations (.html(), .innerhtml(), response.find("string").text() but nothing seems to work. This is my first time trying to parse xml via jquery so maybe I'm doing something fundemantally wrong. The 'response' is populated with the xml.

I hope someone can help me with this. Thanks for your time.

Kind regards, Mark

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

The First Plugin mentioned below xmldom looks pretty good, but if your returned XML really is as simply as your example above, a bit of string parsing might be quicker, something like:

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 query an XML string via DOM in jQuery

Quote:

There are a 2 ways to approach this.

  1. Convert the XML string to DOM, parse it using this plugin or follow this tutorial
  2. Convert the XML to JSON using this plugin .

jQuery cannot parse XML. If you pass a string full of XML content into the $ function it will typically try to parse it as HTML instead using standard innerHTML . If you really need to parse a string full of XML you will need browser-specific and not-globally-supported methods like new DOMParser and the XMLDOM ActiveXObject, or a plugin that wraps them.

But you almost never need to do this, since an XMLHttpRequest should return a fully-parsed XML DOM in the responseXML property. If your web service is correctly setting a Content-Type response header to tell the browser that what's coming back is XML, then the data argument to your callback function should be an XML Document object and not a string. In that case you should be able to use your example with find() and text() without problems.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM