jQuery的转换XML标记为大写 [英] jQuery converting XML tags to uppercase

查看:454
本文介绍了jQuery的转换XML标记为大写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个AJAX请求返回的XML,我想注入到DOM的字符串。我的功能看起来像

I have a AJAX requests which returns a string of XML which I'd like to inject into the DOM. My function looks like

  $.ajax({
    type: 'POST',
    url: "myrequest",
    data: postdata,
    datatype: 'json',
    success: function (arguments) {
        newxmlstring = arguments.newxml;
        oldnode = $("someselector specified in the arguments passed");
        oldnode.replaceWith(newxmlstring);
    }
  });

这工作,但看来这replaceWith函数映射所有节点名称到由服务器发送的响应的大写版本。我假设这是试图使用jQuery来处理XML的一些怪癖?

This works but it appears that the replaceWith function maps all nodeName into capitalized versions of the response sent by the server. I'm assuming this is some quirk with trying to use jQuery to handle XML?

因此​​,例如,如果响应字符串<数据>航空自卫队< /数据> 当我访问 $(newnode)[0]。节点名称我得到数据

So for example if the response string is <data>asdf</data> when I access $(newnode)[0].nodeName I get 'DATA'.

有谁知道如何处理新的XML,而preserving的节点名称在小写?

Does anybody know how to handle the new XML while preserving the nodeName in lower case?

编辑:我的回答是JSON,因为它包含了新的XML字符串,一点点在哪里装上新的XML节点的其他数据。所以我preFER保持数据类型:JSON如果在所有可能的。

My response is JSON since it contains the new xml string and a bit of other data about where to attach the new xml node. So I'd prefer to keep datatype: 'json' if at all possible.

推荐答案

<一个href="http://stackoverflow.com/questions/949942/why-when-i-send-xml-to-php-are-the-nodes-lowercase-but-when-i-parse-them-in-php">From这个答案:

在XML(和基于XML的语言,例如   如XHTML),标签名preserves情况。在   HTML,标签名返回元素名称   在规范的大写形式。该   标记名的值是相同   的节点名称。

In XML (and XML-based languages such as XHTML), tagName preserves case. In HTML, tagName returns the element name in the canonical uppercase form. The value of tagName is the same as that of nodeName.

搜索的位显示,很多人遇到此问题,尤其是选择和XML(jQuery的发现()是大小写敏感的,在发现使用选择()是小写)。

A bit of searching shows that a lot of people experience this problem, especially with selectors and XML (jQuery's find() is case sensitive and selectors used in find() are lowercased).

这里的有人在使用,以转换功能字符串转换成XML,可能会为你工作:

Here's a function someone else is using to convert a string into XML that may work for you:

$.text2xml = function(sXML) { 
    // NOTE: I'd like to use jQuery for this, but jQuery makes all 
    // tags uppercase 
    //return $(xml)[0]; 
    var out; 
    try{ 
        var dXML = ($.browser.msie)?new ActiveXObject("Microsoft.XMLDOM"):new DOMParser(); 
        dXML.async = false; 
    }catch(e){ throw new Error("XML Parser could not be instantiated"); }; 
    try{ 
        if($.browser.msie) out = (dXML.loadXML(sXML))?dXML:false; 
        else out = dXML.parseFromString(sXML, "text/xml"); 
    } 
    catch(e){ throw new Error("Error parsing XML string"); }; 
    return out;
}

这篇关于jQuery的转换XML标记为大写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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