爱程序网

用jQuery解析复杂的xml结构文件

来源: 阅读:

一个晚上的心血

<?xml version="1.0" encoding="UTF-8"?>
<weibo><wbContent>
<msg>
<user>yantze</user>
<content>niahoahaha</content>
<dig>0</dig>
<mid>5</mid>
</msg>
<msg>
<user>yantze</user>
<content>niaho</content>
<dig>0</dig>
<mid>4</mid>
</msg>
<msg>
<user>yantze</user>
<content>niaho</content>
<dig>0</dig>
<mid>3</mid>
<comment>
<user>yantze</user>
<uid>33</uid>
<cid>4</cid>
<content>pingcclun</content>
</comment>
<comment>
<user>yantze</user>
<uid>33</uid>
<cid>3</cid>
<content>nihao</content>
</comment>
</msg>
<msg>
<user>0</user>
<content>niaho</content>
<dig>null</dig>
<mid>1</mid>
</msg>
</wbContent>
<wbInfo>

</wbInfo>
</weibo>
View Code

 

var msg_single=null;
var msg = msg_xml.getElementsByTagName("msg");
for (var i=0; i< msg.length; i++)
{
  user    = $(msg[i].getElementsByTagName("user")).text();
  content = $(msg[i].getElementsByTagName("content")).text();
  dig     = $(msg[i].getElementsByTagName("dig")).text();
  mid     = $(msg[i].getElementsByTagName("mid")).text();

  //链接成html代码
  msg_single ="<br>user:"+ user+"<br>content:"+ content+"<br>dig:" + dig+"<br>mid:"+mid;

  comment = msg[i].getElementsByTagName("comment");
  if (comment.length>0)
  {
    for (var j=0; j<comment.length; j++)
    {
      com_user    = $(msg[i].getElementsByTagName("user")).text();
      com_uid     = $(msg[i].getElementsByTagName("uid")).text();
      com_cid     = $(msg[i].getElementsByTagName("cid")).text();
      com_content = $(msg[i].getElementsByTagName("content")).text();
      msg_single += "<br>cuser:"+com_user+"<br>cuid:" + com_uid+"<br>ccid:" + com_cid+"<br>ccon:"+content;
    }
  }
  $("body").append(msg_single);
}
View Code

效率什么应该会比较快。

关于爱程序网 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助