如何在jQuery中通过DOM查询XML字符串 [英] How to query an XML string via DOM in jQuery

查看:127
本文介绍了如何在jQuery中通过DOM查询XML字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个字符串并将其视为XML。那么我将能够通过jQuery.find与DOM进行查询。一切都在Firefox中运行正常,但我意识到这在IE中不起作用。



我这样做:

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

在Firefox 3中打印:

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

在IE 7中,它打印:

  [] 

任何解决方案可以让浏览器工作?



谢谢。

解决方案

已经有一段时间了,但我只是意识到我忘了发布我如何解决您的组合想法的问题。

$我需要一个完整的客户端数据库(没有PHP)。



我创建了一个包含XML的HTML注释的div 。我使用此HTML解析HTML注释,然后将XML转换为JSON这个

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

您可以在这里查看我的方法:
http://wesculpt.net/art.html



也许我应该转这个方法变成一个jQuery插件。



感谢大家的帮助。


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.

解决方案

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.

这篇关于如何在jQuery中通过DOM查询XML字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆