XML作为字符串返回,而不是对象返回

XML returning as a string, not an object

本文关键字:返回 对象 字符串 XML      更新时间:2023-09-26

我不完全明白为什么这不起作用。我认为XML很容易与之交互,但我不禁觉得是XML的标记导致了这个问题。我知道它验证XML,但仍然:

XML 79. XML

<TREE xmlns:autn="http://schemas.autonomy.com/aci/">
      <ITEM id="753" name="Report an IT Issue for a Corporate Finance Application." link="http://ithelp-remedy.gsk.com/ars/ITHelpHDsubmit_Application/SubmitProblemTicket.asp?qSummary=CORPFINANCEIT">
                 <HELPLINKS/>
      </ITEM>
</TREE>

同样值得注意的是,这是我得到的整个XML,我不应该在XML头有更多的细节吗?

jQuery

 $.ajax({
     url:'xml/79.xml',
     dataType : 'xml',
     success: function(data){
         console.info(data);
     }
});

这不会返回一个对象给我玩:(我怎么能得到它,这样我就可以轻松地玩data

试试这个:

$.ajax({
     url:'xml/79.xml',
     dataType : 'text',
     success: function(data){
        //I'm adding the xml tags alright, but I don't think you
        //really need to, or you could just put a check.
        var omgXmlObj = $($.parseXML('<xml>' + data + '</xml>'));
        console.log(omgXmlObj.find('TREE'));
        console.log(omgXmlObj.find('TREE').attr('xmlns:autn'));
     }
});

根据jQuery文档

如果希望将文本响应视为XML,则使用"text XML " for数据类型

所以试着这样做:

$.ajax({
     url:'xml/79.xml',
     dataType : 'text xml',
     success: function(data){
         console.info(data);
     }
});

从源代码(_ajaxConvert函数)判断,如果只指定一种数据类型,似乎根本没有转换,我可能是错的,虽然