关闭

ajax_javascript解析xml

447人阅读 评论(0) 收藏 举报
  直接解析:
<html>
<head>
<script>
var req = null;
function processReqChange() {
  if (req.readyState == 4 && req.status == 200 && req.responseXML ) {
    var dtable = document.getElementById( 'dataBody' );
    var nl = req.responseXML.getElementsByTagName( 'book' );
    for( var i = 0; i < nl.length; i++ ) {
      var nli = nl.item( i );
      var elAuthor = nli.getElementsByTagName( 'author' );
      var author = elAuthor.item(0).firstChild.nodeValue;
      var elTitle = nli.getElementsByTagName( 'title' );
      var title = elTitle.item(0).firstChild.nodeValue;

      var elTr = dtable.insertRow( -1 );

      var elAuthorTd = elTr.insertCell( -1 );
      elAuthorTd.innerHTML = author;

      var elTitleTd = elTr.insertCell( -1 );
      elTitleTd.innerHTML = title;
} } }

function loadXMLDoc( url ) {
  if(window.XMLHttpRequest) {
    try { req = new XMLHttpRequest();
    } catch(e) { req = false; }
  } else if(window.ActiveXObject) {
    try { req = new ActiveXObject('Msxml2.XMLHTTP');
    } catch(e) {
    try { req = new ActiveXObject('Microsoft.XMLHTTP');
    } catch(e) { req = false; }
  } }
  if(req) {
    req.onreadystatechange = processReqChange;
    req.open('GET', url, true);
    req.send('');
  }
}

var url = window.location.toString();
url = url.replace( /pat2_xml.html/, 'pat2_xml_data.xml' );
loadXMLDoc( url );
</script>
</head>
<body>
<table cellspacing="0" cellpadding="3" width="100%">
<tbody id="dataBody">
<tr>
  <th width="20%">Author</th>
  <th width="80%">Title</th>
</tr>
</tbody>
</table>
</body>
</html>
 
data.xml
<books>z
  <book>
    <author>Jack Herrington</author>
    <title>Code Generation in Action</title>
  </book>
  <book>
    <author>Jack Herrington</author>
    <title>Podcasting Hacks</title>
  </book>
  <book>
    <author>Jack Herrington</author>
    <title>PHP Hacks</title>
  </book>
</books>
 
XMLDOM解析:

    function processXML(xml){
        //var xml="<?xml version='1.0' encoding='utf-8'?><p><id>hello</id><name first='yes'>linda</name></p>";
          alert(xml);
          var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = false;
        xmlDoc.loadXML(xml);
          var node = xmlDoc.documentElement;
          for(var i=0;i<node.childNodes.length;i++){
             
              if(node.childNodes[i].nodeName=="policyId"){
                 
                  var policyId = node.childNodes[i].text;
                  //alert("policyId="+policyId);
              }
              if(node.childNodes[i].nodeName=="policyHoder"){//16
                  var name = node.childNodes[i].getAttribute('name');
                  //alert(name);
              }
              if(node.childNodes[i].nodeName=="insuredList"){
                  var insuredList = node.childNodes[i];
                  var insured = insuredList.childNodes;
                  for(var index=0;index<insured.length;index++){
                      var name       = insured[index].getAttribute('name');
                      var certiType = insured[index].getAttribute('certiType');
                      var certiName = insured[index].getAttribute('certiName');
                      var certiCode = insured[index].getAttribute('certiCode');
                      var telephone = insured[index].getAttribute('telephone');
                      var s = "name="+name + " certiType="+certiType + " certiName="+certiName + " certiCode="+certiCode +" telephone="+ telephone;
                      //alert(s);
                  }
                 
              }
              if(node.childNodes[i].nodeName=="persuadeContent"){
                 
                  var persuadeContent = node.childNodes[i].text;
                  alert(persuadeContent);
                  document.getElementById("xmldata").innerHTML=persuadeContent;
              }
          }   
    }
   
   
</script>


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:15105次
    • 积分:273
    • 等级:
    • 排名:千里之外
    • 原创:8篇
    • 转载:24篇
    • 译文:0篇
    • 评论:6条
    文章分类
    最新评论