tags:

views:

5044

answers:

7

Hi. I would like to take a string and treat it as XML. Then I will be able to query with DOM via the jQuery.find. Everything was working fine in Firefox, but I realized this is not working in IE.

I am doing this:

  var t = "<div><outer><inner>abc</inner><inner>def</inner></outer></div>";
  alert("[" + $(t).find("outer").html() + "]");

In Firefox 3 it prints:

[<inner>abc</inner><inner>def</inner>]

In IE 7 it prints:

[]

Any workarounds for getting this to work across browsers?

Thanks.

A: 

First off, the jQuery constructor takes HTML not XML... that being said, your XML may work - but it depends on a lot of browser dependent behavior. Also, you may have better success by appending the newly created elements to a hidden element on the page somewhere and then trying to query it:

var xml = "<books><book><title>Title</title></book></books>";
$(xml).appendTo("#hidden");
alert($("#hidden books").length);
Goyuix
Hmm. Your idea is having the same outcome for me. Works in FF, but not IE. Well maybe I would mention my bigger question.I have a list of things that I want to query as a database. But I want to do this all client side. Which means I cant make .ajax requests. So I am hiding the list as an XML database in the page itself. Inside a comment, inside an HTML div. Kinda weird but I cant find another way to maintain this table. Maybe I am overlooking an easier method?
Jono
+5  A: 

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.
Jose Basilio
Thanks so much. These are two very good plugins which I have been trying to get to work.
Jono
If you check the plugin source, it in turn uses ActiveX object to convert xml string to xml object.
NLV
+2  A: 

Would it be possible to store the XML as JSON (I would assume it would be)? For instance, in PHP, you can convert XML to an Array or Object and then convert that into JSON with json_encode. You could then echo that out as a javascript variable like so:

In PHP:

<?php
$xml = "<div><outer><inner>abc</inner><inner>def</inner></outer></div>";
$xml_object = simplexml_load_string(xml);
$json = json_encode($xml_object);
?>
<script language="javascript">
$(function() {
    // eval() is okay to use if you know where the JSON is 
    // coming from (and, of course, you do...) 
    var data = eval('<?php echo $json; ?>');
    $(document).data('myapp.data',data);
});
</script>

And now, whenever you need to access that data, you can just get it like so:

function some_function() {
    var data = $(document).data('myapp.data');
    $.each(data.div.outer,function() {
        // Would alert 'abc' then 'def'
        alert(this.inner);
    });
}

I hope all that makes sense. At least you won't have to worry about XML anymore on the client side. Of course, if you absolutely need to, I've found that this has worked for me in the past:

var xml = "<div><outer><inner>abc</inner><inner>def</inner></outer></div>";
var $xml = $('<div />').append(xml);
alert("[" + $xml.find("outer").html() + "]");

Edit I modified my code to use the actual XML you provide--not sure where I mixed up there (must've grabbed the snippet from someone else's answer on accident). You should really give my first suggestion a shot--it should work.

KyleFarris
Your client side example alert() produces "[null]". Fixing it by replacing $xml.find("outer") with .find("books") works in FF but produces "[]" in IE which is exactly the original problem that @Jono is reporting.
Jose Basilio
Oh ya, woops... haha; I didn't put the full XML in the string. That's too bad about the IE problem though. I wonder if he tried my JSON method. It should work as expected.
KyleFarris
A: 

With string xml in ie you need to use .filter as it doesnt want to recognise the xml node tree.

Try this in ie8 with the debugger visible to get the console output.

redsquare
A: 

So what about instead of storing the XML dataset in the DOM, convert it to an HTML table and make it invisible. That should solve the jQuery problems... at least the browser specific issues. Then back to work on refining your selectors.

Goyuix
I cannot just make an HTML table, because it is a very nested dataset. This is why I am using XML. So I just tried putting the XML in a hidden div.I want this method to work, but it does not for the following reason. I need to make sure my XML tags do not conflict with the HTML ones (ie - Title). So I do this, but then jQuery selectors will not find these tags in IE. In FF, any made up tag can be searched for. But IE seems to be restricting me to the HTML subset.
Jono
Just FYI, you could nest tables as well... anyway if I bang on a problem too long and it just isn't going anywhere or it is too complex... it usually means I am going about it the wrong way. I think you would likely be better off using JSON to serialize your structure and maybe something like google gears to persist it on the client - if that is available.
Goyuix
Yes. I am working on the JSON solution. I am curious what is meant by your reference to google gears.
Jono
Just that Gears can persist the data across page loads, so you don't have to send it across the wire with each request.
Goyuix
+1  A: 

I know this is a little late but I think this link explains why it doesn't work in IE and how to fix it: http://dev.jquery.com/ticket/3143

giagejoe
A: 

It's been a while, but I just realized that I forgot to post how I solved the problem with your combined ideas.

I needed an entirely client side database (no PHP).

I created a div with an HTML comment in it containing the XML. I parsed the HTML comment with this and then I converted the XML to JSON with this.

   var xmltext = $("#piecelist").comments();
   var json = $.xml2json(xmltext.html());

You can see my method in action here: http://wesculpt.net/art.html

Maybe I should turn this method into a jQuery plugin.

Thanks for all your help everyone.

Jono