tags:

views:

1310

answers:

4

Using JQuery I am sending an Ajax GET to a web server. The web server is replying with an xml block like this;

<?xml version="1.0" encoding="ISO-8859-1"?>
<ajax-response>
  <response>
     some response here
  </response>
  <response>
     some response here
  </response>
</ajax-response>

The jQuery get looks like this;

jQuery.get(name,parms,function(data){xmlProcess(data);});

The xmlProcess funcion like this;

function xmlProcess(data){  
    jQuery('response',data).each(function(i){
        t = ?what?; 
    });  
}  

My problem is that the stuff being sent back from the server, in the "some response here" part is xhtml. So I can access it as xml using stuff like

t = jQuery(this).find("div").attr("id");

But what I really want is the "some response here" part as text, ie as a string. So the string can contain, for example;

<div id"bob">hello</div>
A: 

If you want it as text, then write the XML so that it is text.

i.e. use &lt; for < and so on.

David Dorward
yes, this is a possibility. In some cases though I might not have control of the server so I'm still interested in a client-side answer. However if I have no success then encoding the text on the server side is a possibility. Thanks.
A: 

If I'm understanding the question correctly, you can convert the response object to a string like this:

jQuery.get(name,parms,function(data){
    //for IE 
    if (window.ActiveXObject) {
        var string = data.xml;
        alert(string);
    }
    // code for Mozilla, Firefox, Opera, etc.
    else {
       var string = (new XMLSerializer()).serializeToString(data);
       alert(string);
    }
});
karim79
this is close, I'd like to serialize each response element separately, and the above serialises the whole response. But I'm researching more along these lines.
@Bruce - so use the above to serialize each node - I'll edit the answer.
karim79
@Karim79, I tried that first off. as in var string = (new XMLSerializer()).serializeToString(i);but it spits an error because i ie an Element Object, not an XML object.
@Bruce, I see. I'm looking into this, I'll get back to you.
karim79
@Karim79 - no that's inaccurate. It spit the error because i is not an element. I added " t = jQuery("response",data).get(i); then string = (new XMLSerializer()).serializeToString(t); and I got the XML of the element. Now all I need to do is refine it a bit so I get the _contents_ of the element, not including the element boundary itself. Thanks!
@Bruce - just wrap that in a jQuery object and use a selector on it, i.e.: jQuery(theNodeString)
karim79
@Karim79 - sorry that zoomed over my head. Can you define "it" and "that"? at the moment I have t, which contains the Element.
as it's a string, stripping off the first tag, and last close tag is pretty simple. ix = string.indexOf('>')+1; jx = string.lastIndexOf('<'); string = string.substring(ix,jx);
@Bruce - that'll do it :)
karim79
@Karim79 - thanks - really appreciate it.
A: 

Every Element should have an "innerHTML" property. If that doesn't work, try the toString() method on the element.

Paul Tarjan
A: 

This can actually be done very simply, the .text() method of jquery retrieves everything between the selected tag as a string, which seems to be what you want: t = $(this).text(); alert(t); you shouldn't have to mess around replacing < with &lt e.t.c...

Have fun, good luck, let me know if you get good results!

sillyMunky