【问题标题】:Parse XML using JQuery in Safari在 Safari 中使用 JQuery 解析 XML
【发布时间】:2011-01-01 22:47:43
【问题描述】:

我在下面的 XML 中有以下内容,并希望在 Safari 网络浏览器中使用 JQuery 对其进行解析。我只想打印出用户名,需要一些关于如何编写函数的帮助。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns3:Account xmlns:ns2="http://oec.api.opsource.net/schemas/organization" .. >
<ns3:userName>rdyer</ns3:userName>
<ns3:fullName>Joe Public</ns3:fullName>
<ns3:firstName>Joe</ns3:firstName>
<ns3:lastName>Public</ns3:lastName>
<ns3:emailAddress>jpublic24583@pop.net</ns3:emailAddress>
<ns3:orgId>1831c1a9-9c03-44df-a5a4-f2a4662d6bde</ns3:orgId>
<ns3:roles>
<ns3:role>
<ns3:name>primary administrator</ns3:name>
</ns3:role>
</ns3:roles>
</ns3:Account>

【问题讨论】:

标签: jquery xml parsing namespaces


【解决方案1】:

显然$('ns3\\:userName',xml) 在 webkit 下的 jQuery 1.3.2 中崩溃了,但不要害怕,Sizzle 仍然有能力,只需过滤属性 'nodeName' 试试$('[nodeName=ns3:userName]',xml)

例子:

<div id='username'></div>
<script>
$(function() {
  // When the DOM is ready - perform AJAX to get the XML:
  $.get('/myxml.xml', {}, function(xml) {
    var $username = $("[nodeName=ns3:userName]", xml);
    // put the text from our node into the text of the empty div:
    $("#username").text($username.text());
  }, "xml");
});
</script>

【讨论】:

  • 如果我不使用 jQuery 会怎样?还有其他选择吗?
  • 该方法适用于 jQuery(嘶嘶声是其中的一部分)对不起,如果这令人困惑......我不确定非 jQuery 解析它的方式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-08
  • 1970-01-01
  • 2011-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多