2011-11-29 119 views
5

我想解析使用jQuery的xml响应,并只输出一个页面的元素,但我在这不成功。如何使用jQuery解析XML响应

下面是我用于解析它的响应&的代码。

$.ajax({ 
    url: UCMDBServiceUrl, 
    type: "POST", 
    dataType: "xml", 
    data: soapMessage, 
    success: UCMDBData, 
    crossDomain: true, 
    contentType: "text/xml; charset=\"utf-8\"" 
}); 
alert("Sent2"); 
return false; 
} 

function UCMDBData(xmlHttpRequest, status, msg) 
{ 
    alert("Came back1"); 
    $(xmlHttpRequest.responseXML).find('tns:CIs').each(function() 
    { 
     alert("Came back2"); 
     $(this).find("ns0:CI").each(function() 
     { 
      alert("Came back3"); 
      $("#output").append($(this).find("ns0:ID").text()); 
     }); 
    });  
} 

我收到“回来1”的警报,但它似乎没有进一步发展。下面是我试图用我上面的jQuery代码解析的XML响应。 ,我最终试图返回了响应的文本是这个元素

<?xml version='1.0' encoding='utf-8'?> 
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Header /> 
    <soapenv:Body> 
     <tns:getFilteredCIsByTypeResponse xmlns:ns0="http://schemas.hp.com/ucmdb/1/types" xmlns:ns1="http://schemas.hp.com/ucmdb/ui/1/types" xmlns:ns2="http://schemas.hp.com/ucmdb/1/types/query" xmlns:ns3="http://schemas.hp.com/ucmdb/1/types/props" xmlns:ns4="http://schemas.hp.com/ucmdb/1/types/classmodel" xmlns:ns5="http://schemas.hp.com/ucmdb/1/types/impact" xmlns:ns6="http://schemas.hp.com/ucmdb/1/types/update" xmlns:ns7="http://schemas.hp.com/ucmdb/discovery/1/types" xmlns:ns8="http://schemas.hp.com/ucmdb/1/types/history" xmlns:tns="http://schemas.hp.com/ucmdb/1/params/query"> 
      <tns:CIs> 
       <ns0:CI> 
        <ns0:ID>4d030502995a00afd989d3aeca2c990c</ns0:ID> 
        <ns0:type>nt</ns0:type> 
        <ns0:props> 
         <ns0:strProps> 
          <ns0:strProp> 
           <ns0:name>name</ns0:name> 
           <ns0:value>prodoo</ns0:value> 
          </ns0:strProp> 
         </ns0:strProps> 
         <ns0:booleanProps> 
          <ns0:booleanProp> 
           <ns0:name>host_iscomplete</ns0:name> 
           <ns0:value>false</ns0:value> 
          </ns0:booleanProp> 
         </ns0:booleanProps> 
        </ns0:props> 
       </ns0:CI> 
      </tns:CIs> 
      <tns:chunkInfo> 
       <ns0:numberOfChunks>0</ns0:numberOfChunks> 
       <ns0:chunksKey> 
        <ns0:key1 /> 
        <ns0:key2 /> 
       </ns0:chunksKey> 
      </tns:chunkInfo> 
     </tns:getFilteredCIsByTypeResponse> 
    </soapenv:Body> 
</soapenv:Envelope> 

所以我的问题是,如何正确分析数据?我相信代码语法是正确的,但我没有得到预期的返回结果。我将不胜感激任何帮助,谢谢。

编辑

我已经修改我的代码,如建议以下,但仍没有运气:

$.ajax({ 
    url: UCMDBServiceUrl, 
    type: "POST", 
    dataType: "xml", 
    data: soapMessage, 
    success: UCMDBData, 
    crossDomain: true, 
    contentType: "text/xml;" 
    }); 
alert("Sent2"); 
return false; 
} 

function UCMDBData(data, textStatus, jqXHR) { 
    alert("Came back1"); 
    $(data).find('tns:CIs').each(function() { 
     alert("Came back2"); 
     $(this).find("ns0:CI").each(function() { 
      alert("Came back3"); 
      $("#output").append($(this).find("ns0:ID").text()); 
      document.AppServerForm.outputtext.value = document.AppServerForm.outputtext.value + "http://localhost:8080/ucmdb/cms/directAppletLogin.do?objectId=" + $(this).find('ns0:ID').text() +"&infopane=VISIBLE&navigation=true&cmd=ShowRelatedCIs&interfaceVersion=8.0.0&ApplicationMode=ITU&customerID=1&userName=admin&userPassword=admin"; 

    }); 
}); 

}

当我执行的唯一警报消息我收到的回复是“回来1“,这意味着代码仍然没有通过与jquery正确的xml。还有其他建议吗?

+0

你想匹配ns0:CI的单个实例,还是可以多次出现? – JesseBuesking

+0

只有一个ns0实例:正如您在我在这里发布的XML响应中所看到的那样,返回CI。 – user1013396

回答

2

命名空间范围的名需要处理方式稍有不同。根据这个答案: jQuery XML parsing with namespaces 您需要使用属性选择器[@ nodeName = tns:CIs]来代替。

您可能需要删除晚于1.3的jQuery版本的“@”。另一个建议是逃避冒号:.find('tns \:CIs'),这是hacky,因为它将语法前缀与语义名称空间(uri)混合在一起。所以如果前缀改变了,这个方法会中断。更正确的答案将认识到前缀映射到命名空间uri。在这方面,jquery-xmlns plugin for namespace-aware selectors看起来很有希望。

+0

这工作!非常感谢! – user1013396

+0

其实我仍然有问题,这是工作在Firefox,但不是IE 8(这是什么在这里使用)。 – user1013396

+1

附近,我可以告诉它是JQuery中的一个错误:http://bugs.jquery.com/ticket/155 – jerseyboy

1

您的jQuery成功函数是错误的形式。它需要的形式

function UCMDBData(data, textStatus, jqXHR) { 
    alert("Came back1"); 
    $(data).find('tns:CIs').each(function() { 
     alert("Came back2"); 
     $(this).find("ns0:CI").each(function() { 
      alert("Came back3"); 
      $("#output").append($(this).find("ns0:ID").text()); 
     }); 
    }); 
} 

。此外,在$.ajax功能,改变contentType线为contentType: "text/xml",而不是你有什么之前(假设你发送XML到服务器)。

有关更多信息,请参阅jQuery.ajax() documentation

+0

找不到名称空间。 – Christophe

0

可能是正确的语法将

success: function(xml) { 
    $(xml).find('tns:CIs').each(function() { 
    ...... 
+0

这不适用于jQuery 1.7 – Christophe

1

根据你的评论,为什么疯狂使用jQuery?只需使用JavaScript本身!

var open = '<ns0:ID>'; 
var close = '</ns0:ID>'; 

var start = obj.indexOf(open) + open.length; 
var end = obj.indexOf(close); 

var result = obj.slice(start, end); 

这是一个jsfiddle,它显示了它的行动。

+1

它适用于此特定示例,但会在节点具有属性时中断。 – Christophe