如何将Ajax响应对象解析为xml

时间:2014-04-04 11:12:15

标签: jquery xml parsing xml-parsing

使用jquery我能够获得响应对象。但是,使用log LOG:Success [object HTMLCollection]将其打印到控制台。但是,我无法按标签名称获取xml内容,例如

      responseData.getElementsByTagName("data-list");

我的方法未定义,任何人都可以帮我解决这个问题。

2 个答案:

答案 0 :(得分: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']")

答案 1 :(得分: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>