2014-04-04 60 views
0

使用jquery,我能夠獲取響應對象。但是,使用日誌LOG:Success [object HTMLCollection]將其打印到控制檯。但是,我沒能獲得通過標籤名稱的XML內容,例如如何將Ajax響應對象解析爲xml

 responseData.getElementsByTagName("data-list"); 

我得到的方法定義,任何人都可以幫助我在這個問題上。

+0

請您爲我們提供一點點背景。什麼是XML的結構。 – Qpirate

+0

試試這個 - '$(responseData).find(「data-list」);' – Archer

+0

你得到的是一個HTML集合,並且你不能從中獲取XML? – lshettyl

回答

0

請按照下面的示例:

<!DOCTYPE html> 
<html> 
<head> 
<title>jQuery and XML</title> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<meta name="language" content="en" /> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> 
</head> 
<body 

<div id="output"></div> 

<script type="text/javascript"> 
$(document).ready(function(){ 
$.ajax({ 
    type: "GET", 
    dataType: "xml", 
    url: "example.xml", 
    success: function(xml){ 
    $(xml).find("book").each(function(){ 
    $("#output").append($(this).attr("code") + "<br />"); 
    }); 
    } 
}); 
}); 
</script> 


</body> 
</html> 

匹配的xml文件:

<?xml version="1.0" encoding="UTF-8"?> 
<books title="A list of books"> 
<book code="abcdef" /> 
<book code="ghijklm"> 
    Some text contents 
</book> 
</books> 

查找特定項目:

$(xml).find("book[title='test']") 
0
$.ajax({ 
     type: 'POST', 
     contentType: 'application/x-www-form-urlencoded; charset=UTF-8', 
     url: rootURL, 
     dataType: "xml", 
     data: postParameter(), 
     complete:function(xml){ 
      $(xml).find('resourceMessageBean').each(function(){ 
      var data= $(this).find('data-list').text(); 
      }); 
     } 
    }); 

XML:

<?xml version="1.0" encoding="UTF-8" ?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" xmlns:xsd="http://www.w3.org/1999/XMLSchema"> 
<SOAP-ENV:Body><MYTesting_AppResponse xmlns="WebServices"> 
<return> 
<status>success</status> 
<resourceMessageBean> 
<rsa_note3>To proceed, please enter your password and click "Login"</rsa_note3> 
<phrase>???en.common.password.login.p3???</phrase> 
<data-list>Username</data-list> 
<password>Password</password> 
</resourceMessageBean> 
<loginFormBean> 
<username>tester1</username> 
</loginFormBean> 
</return> 
</MYTesting_AppResponse> 
</SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 
+0

@ user3497741,很高興爲您效勞。不用謝。如果這是你的失敗,請接受答案。 – RGS