XML解析

 public void parse(InputStream in) throws IOException {
  KXmlParser parserMail = new KXmlParser();
  String[] tempName = new String[10];
  String[] tempSize = new String[10];
  int index = 0;
  try {
   parserMail.setInput(in, "UTF-8");// 将返回的XML构建成一个输入流传给KXmlParser对象进行解析
   parserMail.nextTag();
   parserMail.require(XmlPullParser.START_TAG, null, "Mail");
   while (parserMail.nextTag() != XmlPullParser.END_TAG) {// 每循环一次则增加一封邮件
    // 定义一封邮件的参数
    String GUID = "", From = "", Subject = "", MailBody = "", MailDate = "", MailSize = "";
    // Vector AttachFile = new Vector();// 附件,这里你可以做优化,可以使用数组
    KXmlParser parserMessage = parserMail;
    parserMessage.require(XmlPullParser.START_TAG, null, "Message");
    while (parserMessage.nextTag() != XmlPullParser.END_TAG) {
     parserMessage.require(XmlPullParser.START_TAG, null, null);
     String name = parserMessage.getName();
     String text = parserMessage.nextText();
     // System.out.println("<" + name + ">" + text);
     if (name.equals("GUID"))
      GUID = text;
     else if (name.equals("From"))
      From = text;
     else if (name.equals("Subject"))
      Subject = text;
     else if (name.equals("MailBody"))
      MailBody = text;
     else if (name.equals("MailDate"))
      MailDate = text;
     else if (name.equals("MailSize"))
      MailSize = text;
     parserMessage.require(XmlPullParser.END_TAG, null, name);
     if (name.equals("MailSize")) {// 如果当前解析到MailSize则下一个为附件节点
      // System.out.println("AttachList");
      KXmlParser parserAttachList = parserMail;
      parserAttachList.nextTag();
      parserAttachList.require(XmlPullParser.START_TAG, null,
        "AttachList");
      while (parserAttachList.nextTag() != XmlPullParser.END_TAG) {// 每循环一次则增加一个附件
       String FileName = "", FileSize = "";
       KXmlParser parserAttachFile = parserMail;
       parserAttachFile.require(XmlPullParser.START_TAG,
         null, "AttachFile");
       // System.out.println("ccccccccc");
       while (parserAttachFile.nextTag() != XmlPullParser.END_TAG) {
        parserAttachFile.require(
          XmlPullParser.START_TAG, null, null);
        String name2 = parserAttachFile.getName();
        String text2 = parserAttachFile.nextText();
        // System.out.println("<" + name2 + ">" +
        // text2);
        if (name2.equals("FileName"))
         FileName = text2;
        else if (name2.equals("FileSize"))
         FileSize = text2;
        parserAttachFile.require(XmlPullParser.END_TAG,
          null, name2);
       }
       tempName[index] = FileName;
       tempSize[index] = FileSize;
       index++;
       // 此处为一个附件的结束
      }
     }
    }
    // 此处为一封邮件解析结束,可以进行保存SaveMail()
    String[] AttachFileName = new String[index];
    String[] AttachFileSize = new String[index];
    System.out.println("index = " + index);
    // 处理附件
    for (int i = 0; i < index; i++) {
     AttachFileName[i] = tempName[i];
     AttachFileSize[i] = tempSize[i];
    }
    Mail mail = new Mail(Subject, From, MailDate, MailBody, index,
      AttachFileName, AttachFileSize, GUID);
    mail.save();
    index = 0;
    parserMessage.require(XmlPullParser.END_TAG, null, "Message");
   }
   parserMail.require(XmlPullParser.END_TAG, null, "Mail");
   parserMail.next();
   parserMail.require(XmlPullParser.END_DOCUMENT, null, null);
  } catch (XmlPullParserException e) {
   e.printStackTrace();
  }
  mainCanvas.setTempCue("邮件下载完成", MainCanvas.TEMP_WAIT);
  // 释放临时字符串数组
  MainCanvas.releaseString(tempName);
  MainCanvas.releaseString(tempSize);
  // 更新邮箱数据
  mainCanvas.updateMailBoxData();
 }

 // String RssErr;

 /**
  * @param isRss
  *            true为RSS下载,false为推荐RSS频道更新
  */
 public boolean parseRssFeedXml(InputStream is, boolean isRss) {
  String[][] tempRss = new String[10][3];
  String[][] tempRssChanne = null;
  int index = 0;
  String title = "", description = "", link = "";
  /** Initialize XML parser and parse RSS XML */
  KXmlParser parserRss = new KXmlParser();
  try {
   parserRss.setInput(is, null); // 设置读取用UTF-8编码
   parserRss.nextTag();
   parserRss.require(XmlPullParser.START_TAG, null, "RSS");
   while (parserRss.nextTag() != XmlPullParser.END_TAG) {
    // 每循环一次则增加一条RSS
    KXmlParser parserItem = parserRss;
    parserItem.require(XmlPullParser.START_TAG, null, "Item");
    while (parserItem.nextTag() != XmlPullParser.END_TAG) {
     parserItem.require(XmlPullParser.START_TAG, null, null);
     String name = parserItem.getName();
     String text = parserItem.nextText();
     // RSS解析
     if (isRss) {
      if (name.equals("Title"))
       title = text;
      else if (name.equals("Description"))
       description = text;
      else if (name.equals("Link"))
       link = text;
     } else {// RSS频道解析
      if (name.equals("Title"))
       title = text;
      else if (name.equals("Link"))
       link = text;
     }
     parserItem.require(XmlPullParser.END_TAG, null, name);
     System.out.println(name + " == " + text);
    }

    // 一条Rss记录读取完毕
    tempRss[index][0] = title;
    tempRss[index][1] = link;
    if (isRss)
     tempRss[index][2] = description;
    index++;
    parserItem.require(XmlPullParser.END_TAG, null, "Item");
   }
  } catch (Exception e) {
   return false;
  }
  MainCanvas.releaseString(MainCanvas.rssConnList);
  if (isRss) {
   MainCanvas.rssConnList = new String[index][3];
   for (int i = 0; i < index; i++) {
    MainCanvas.rssConnList[i][0] = tempRss[i][0];
    MainCanvas.rssConnList[i][1] = tempRss[i][1];
    MainCanvas.rssConnList[i][2] = tempRss[i][2];
   }
   // 释放临时字符串数组
   MainCanvas.releaseString(tempRss);
  } else {
   tempRssChanne = new String[index][2];
   for (int i = 0; i < index; i++) {
    tempRssChanne[i][0] = tempRss[i][0];
    tempRssChanne[i][1] = tempRss[i][1];
   }
   if (!MainCanvas.updateRss(tempRssChanne, "channel"))
    return false;
   MainCanvas.releaseString(tempRssChanne);
  }
  parserRss = null;
  index = 0;
  return true;
 }

 /** 豆荚列表解析 */
 private boolean parseBeanpodList(InputStream is, String parName) {
  String id = "", title = "", link = "", price = "", status = "", type = "";
  String rowCount = "", pageCount = "";
  String name = "", text = "";
  String name1 = "", text1 = "";
  Vector v1 = new Vector();
  /** Initialize XML parser and parse RSS XML */
  KXmlParser parserPar = new KXmlParser();
  try {
   parserPar.setInput(is, null); // 设置读取用UTF-8编码
   parserPar.nextTag();
   parserPar.require(XmlPullParser.START_TAG, null, parName);
   while (parserPar.nextTag() != XmlPullParser.END_TAG) {//
    // 每循环一次则增加一条记录
    name = parserPar.getName();
    text = parserPar.nextText();
    if (name.equals("RowCount"))
     rowCount = text;
    else if (name.equals("PageCount"))
     pageCount = text;
    parserPar.require(KXmlParser.END_TAG, null, name);
    if (name.equals("PageCount")) {
     KXmlParser parserItemList = parserPar;
     parserItemList.nextTag();
     parserItemList.require(XmlPullParser.START_TAG, null,
       "ItemList");
     while (parserItemList.nextTag() != XmlPullParser.END_TAG) {
      KXmlParser parserItem = parserPar;
      parserItem.require(XmlPullParser.START_TAG, null,
        "Item");
      while (parserItem.nextTag() != XmlPullParser.END_TAG) {
       parserItem
         .require(KXmlParser.START_TAG, null, null);
       name1 = parserItem.getName();
       text1 = parserItem.nextText();
       if (name1.equals("ID"))
        id = text1;
       else if (name1.equals("Title"))
        title = text1;
       else if (name1.equals("Link"))
        link = text1;
       else if (name1.equals("Price"))
        price = text1;
       else if (name1.equals("Status"))
        status = text1;
       parserItem.require(KXmlParser.END_TAG, null, name1);
      }
      // 一条记录解析完成
      // 根据产品编号获得类型
      switch (id.charAt(1)) {
      case 'A':
       type = "[铃音]";
       break;
      case 'P':
       type = "[图片]";
       break;
      case 'V':
       type = "[视频]";
       break;
      case 'G':
       type = "[游戏]";
       break;
      case 'B':
       type = "[书报]";
       break;
      case 'T':
       type = "[工具]";
       break;
      default:
       type = "[其他]";
      }
      v1.addElement(new Beampod(id, title, link, price,
        status, type));
      parserItem.require(XmlPullParser.END_TAG, null, "Item");
     }
    }
   }
   // 解析完成返回数据
   mainCanvas.beampodList = new Beampod[v1.size()];
   for (int i = 0; i < v1.size(); i++) {
    mainCanvas.beampodList[i] = (Beampod) v1.elementAt(i);
   }
   // 获得当前总页面数量和当前所在页
   mainCanvas.nowPage = PageIndex;
   mainCanvas.sumPages = MainCanvas.stringToInt(pageCount);
  } catch (Exception e) {
   e.printStackTrace();
   return false;
  }
  parserPar = null;
  v1.removeAllElements();
  v1 = null;
  return true;
 } 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值