XML以字符串形式返回,而不是对象

时间:2013-03-08 11:57:28

标签: javascript jquery xml xml-parsing

我不完全理解为什么这不起作用。我认为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

2 个答案:

答案 0 :(得分:1)

根据jQuery文档

  

如果您希望将文本响应视为XML,请使用“text xml”   dataType

所以试试这样做:

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

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

答案 1 :(得分:1)

试试这个:

$.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'));
     }
});
相关问题