Android Open Source - JiangHomeStyle_Android_Phone Xml Parse Tools






From Project

Back to project page JiangHomeStyle_Android_Phone.

License

The source code is released under:

Apache License

If you think the Android project JiangHomeStyle_Android_Phone listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.cidesign.jianghomestylephone.tools;
/*  ww  w .j  av  a  2s  .c o m*/
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import com.cidesign.jianghomestylephone.entity.ArticleEntity;
import com.cidesign.jianghomestylephone.entity.FileListEntity;

import android.util.Log;
import android.util.Xml;

public class XmlParseTools
{
  private static final String TAG = XmlParseTools.class.getSimpleName();

  public static ArticleEntity readDocXML(String path)
  {
    ArticleEntity articleEntity = null;

    File file = new File(path);
    if (!file.exists())
    {
      return null;
    }

    InputStream inputStream = null;
    try
    {
      inputStream = new BufferedInputStream(new FileInputStream(file));
      List<ArticleEntity> list = XmlParseTools.parseArticleList(inputStream);
      if (list.size() > 0)
      {
        articleEntity = list.get(0);
      }
      return articleEntity;
    }
    catch (FileNotFoundException e)
    {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    catch (IOException e)
    {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    finally
    {
      try
      {
        if (inputStream != null)
        {
          inputStream.close();
        }
      }
      catch (IOException e)
      {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
    return null;
  }

  /**
   * ?????????XML???????????????????????????????
   * 
   * @param xmlStr
   * @return
   */
  public static List<FileListEntity> parseFileList(String xmlStr)
  {
    List<FileListEntity> fileList = null;
    FileListEntity fileListEntity = null;
    String date = TimeTools.getCurrentDateTime();
    try
    {
      XmlPullParser parser = Xml.newPullParser();
      parser.setInput(new StringReader(xmlStr));
      int event = parser.getEventType();

      while (event != XmlPullParser.END_DOCUMENT)
      {
        switch (event)
        {
        case XmlPullParser.START_DOCUMENT:
          fileList = new ArrayList<FileListEntity>();
          break;
        case XmlPullParser.START_TAG:
          String name = parser.getName();
          if ("file".equals(name))
          {
            fileListEntity = new FileListEntity();
          }
          if (fileListEntity != null)
          {
            if ("id".equals(name))
            {
              fileListEntity.setServerID(parser.nextText());
            }
            else if ("name".equals(name))
            {
              fileListEntity.setName(parser.nextText());
              fileListEntity.setInsertDate(date);
              fileListEntity.setDownloadFlag(0);
            }
            else if ("size".equals(name))
            {
              String str = parser.nextText();
              if (str != null && !str.trim().equals(""))
              {
                fileListEntity.setSize(Integer.parseInt(str));
              }
            }
            else if ("url".equals(name))
            {
              fileListEntity.setUrl(parser.nextText());
            }
            else if ("timestamp".equals(name))
            {
              fileListEntity.setTimestamp(parser.nextText());
            }
            else if ("md5".equals(name))
            {
              fileListEntity.setMd5(parser.nextText());
            }
            else if ("op".equals(name))
            {
              fileListEntity.setOperation(parser.nextText().charAt(0));
            }
          }
          break;
        case XmlPullParser.END_TAG:
          if ("file".equals(parser.getName()))
          {
            fileList.add(fileListEntity);
            fileListEntity = null;
          }
          break;
        }

        event = parser.next();
      }

    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    return fileList;
  }

  /**
   * ?????????XML???????????????????????????????
   * 
   * @param xmlStr
   * @return
   */
  public static List<FileListEntity> parseFileInputStreamList(InputStream inputStream)
  {
    List<FileListEntity> fileList = null;
    FileListEntity fileListEntity = null;
    String date = TimeTools.getCurrentDateTime();
    try
    {
      XmlPullParser parser = Xml.newPullParser();
      parser.setInput(inputStream, "UTF-8");
      int event = parser.getEventType();

      while (event != XmlPullParser.END_DOCUMENT)
      {
        switch (event)
        {
        case XmlPullParser.START_DOCUMENT:
          fileList = new ArrayList<FileListEntity>();
          break;
        case XmlPullParser.START_TAG:
          String name = parser.getName();
          if ("file".equals(name))
          {
            fileListEntity = new FileListEntity();
          }
          if (fileListEntity != null)
          {
            if ("id".equals(name))
            {
              fileListEntity.setServerID(parser.nextText());
            }
            else if ("name".equals(name))
            {
              fileListEntity.setName(parser.nextText());
              fileListEntity.setInsertDate(date);
              fileListEntity.setDownloadFlag(0);
            }
            else if ("size".equals(name))
            {
              String str = parser.nextText();
              if (str != null && !str.trim().equals(""))
              {
                fileListEntity.setSize(Integer.parseInt(str));
              }
            }
            else if ("url".equals(name))
            {
              fileListEntity.setUrl(parser.nextText());
            }
            else if ("timestamp".equals(name))
            {
              fileListEntity.setTimestamp(parser.nextText());
            }
            else if ("md5".equals(name))
            {
              fileListEntity.setMd5(parser.nextText());
            }
            else if ("op".equals(name))
            {
              fileListEntity.setOperation(parser.nextText().charAt(0));
            }
          }
          break;
        case XmlPullParser.END_TAG:
          if ("file".equals(parser.getName()))
          {
            fileList.add(fileListEntity);
            fileListEntity = null;
          }
          break;
        }

        event = parser.next();
      }

    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    return fileList;
  }

  /**
   * ???????????????????????
   * 
   * @param inputStream
   * @return
   */
  public static List<ArticleEntity> parseArticleList(InputStream inputStream)
  {
    List<ArticleEntity> articleList = null;
    try
    {
      ArticleEntity articleEntity = null;
      XmlPullParser parser = Xml.newPullParser();
      parser.setInput(inputStream, "UTF-8");

      int event = parser.getEventType();// ???????????
      while (event != XmlPullParser.END_DOCUMENT)
      {
        switch (event)
        {
        case XmlPullParser.START_DOCUMENT:// ????????????????????
          articleList = new ArrayList<ArticleEntity>();// ?????books????
          break;
        case XmlPullParser.START_TAG:// ?????????????????????
          if ("doc".equals(parser.getName()))
          {// ????????????????book
            articleEntity = new ArticleEntity();
          }
          if (articleEntity != null)
          {
            if ("title".equals(parser.getName()))
            {
              articleEntity.setTitle(parser.nextText());
            }
            else if ("profile".equals(parser.getName()))
            {
              articleEntity.setProfile_path(parser.nextText());
            }
            else if ("post_date".equals(parser.getName()))
            {
              articleEntity.setPost_date(parser.nextText());
            }
            else if ("author".equals(parser.getName()))
            {
              articleEntity.setAuthor(parser.nextText());
            }
            else if ("description".equals(parser.getName()))
            {
              articleEntity.setDescription(parser.nextText());
            }
            else if ("category".equals(parser.getName()))
            {
              // ?? ???? ???? ????
              String category = parser.nextText();
              if (category.equals("1/3"))
              {
                articleEntity.setCategory(JiangCategory.LANDSCAPE);
              }
              else if (category.equals("1/2"))
              {
                articleEntity.setCategory(JiangCategory.HUMANITY);
              }
              else if (category.equals("1/5"))
              {
                articleEntity.setCategory(JiangCategory.STORY);
              }
              else if (category.equals("1/4"))
              {
                articleEntity.setCategory(JiangCategory.COMMUNITY);
              }
            }
            else if ("main_file".equals(parser.getName()))
            {
              articleEntity.setMain_file_path(parser.nextText());
            }
            else if ("background".equals(parser.getName()))
            {
              articleEntity.setMax_bg_img(parser.nextText());
            }
            else if ("headline".equals(parser.getName()))
            {
              String temp = parser.nextText();
              if (temp.equals("true"))
              {
                articleEntity.setIsHeadline(1);
              }
              else
              {
                articleEntity.setIsHeadline(0);
              }              
            }
          }
          break;
        case XmlPullParser.END_TAG:// ??????????????????????
          if ("doc".equals(parser.getName()))
          {// ?????????????????book
            articleList.add(articleEntity);// ??book????books????
            articleEntity = null;
          }
          break;
        }
        event = parser.next();// ??????????????????????
      }// end while
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
    catch (XmlPullParserException e)
    {
      e.printStackTrace();
    }
    return articleList;
  }

}




Java Source Code List

com.cidesign.jianghomestylephone.DetailActivity.java
com.cidesign.jianghomestylephone.JiangActivity.java
com.cidesign.jianghomestylephone.MainActivity.java
com.cidesign.jianghomestylephone.SplashActivity.java
com.cidesign.jianghomestylephone.adapter.CommunityViewpagerAdapter.java
com.cidesign.jianghomestylephone.adapter.HumanityViewpagerAdapter.java
com.cidesign.jianghomestylephone.adapter.LandscapeViewpagerAdapter.java
com.cidesign.jianghomestylephone.adapter.LayoutCaculateAdapter.java
com.cidesign.jianghomestylephone.adapter.StoryViewpagerAdapter.java
com.cidesign.jianghomestylephone.async.AsyncDownTask.java
com.cidesign.jianghomestylephone.async.AsyncInitCommunityData.java
com.cidesign.jianghomestylephone.async.AsyncInitData.java
com.cidesign.jianghomestylephone.async.AsyncInitHomeData.java
com.cidesign.jianghomestylephone.async.AsyncInitHumanityData.java
com.cidesign.jianghomestylephone.async.AsyncInitLandscapeData.java
com.cidesign.jianghomestylephone.async.AsyncInitStoryData.java
com.cidesign.jianghomestylephone.db.DatabaseConfigUtil.java
com.cidesign.jianghomestylephone.db.DatabaseHelper.java
com.cidesign.jianghomestylephone.entity.ArticleEntity.java
com.cidesign.jianghomestylephone.entity.FileListEntity.java
com.cidesign.jianghomestylephone.entity.LayoutEntity.java
com.cidesign.jianghomestylephone.entity.RelativeLayoutRulesEntity.java
com.cidesign.jianghomestylephone.http.ArticalOperation.java
com.cidesign.jianghomestylephone.http.DownLoadThread.java
com.cidesign.jianghomestylephone.service.DownloadService.java
com.cidesign.jianghomestylephone.tools.CategoryDataLoadingLogic.java
com.cidesign.jianghomestylephone.tools.FileOperationTools.java
com.cidesign.jianghomestylephone.tools.JiangCategory.java
com.cidesign.jianghomestylephone.tools.LayoutMarginSetting.java
com.cidesign.jianghomestylephone.tools.LoadingDataFromDB.java
com.cidesign.jianghomestylephone.tools.LoadingImageTools.java
com.cidesign.jianghomestylephone.tools.MD5Tools.java
com.cidesign.jianghomestylephone.tools.StorageUtils.java
com.cidesign.jianghomestylephone.tools.TimeTools.java
com.cidesign.jianghomestylephone.tools.WidgetCache.java
com.cidesign.jianghomestylephone.tools.XmlParseTools.java
com.cidesign.jianghomestylephone.version.NetworkTool.java
com.cidesign.jianghomestylephone.version.VersionConfig.java
com.cidesign.jianghomestylephone.version.VersionUpdate.java
com.cidesign.jianghomestylephone.widget.CommunityRelativeLayout.java
com.cidesign.jianghomestylephone.widget.CustomScrollView.java
com.cidesign.jianghomestylephone.widget.HScrollViewTouchLogic.java
com.cidesign.jianghomestylephone.widget.HumanityRelativeLayout.java
com.cidesign.jianghomestylephone.widget.LandscapeRelativeLayout.java
com.cidesign.jianghomestylephone.widget.PopMenu.java
com.cidesign.jianghomestylephone.widget.StoryRelativeLayout.java
org.apache.tools.zip.AbstractUnicodeExtraField.java
org.apache.tools.zip.AsiExtraField.java
org.apache.tools.zip.CentralDirectoryParsingZipExtraField.java
org.apache.tools.zip.ExtraFieldUtils.java
org.apache.tools.zip.FallbackZipEncoding.java
org.apache.tools.zip.GeneralPurposeBit.java
org.apache.tools.zip.JarMarker.java
org.apache.tools.zip.NioZipEncoding.java
org.apache.tools.zip.Simple8BitZipEncoding.java
org.apache.tools.zip.UnicodeCommentExtraField.java
org.apache.tools.zip.UnicodePathExtraField.java
org.apache.tools.zip.UnixStat.java
org.apache.tools.zip.UnparseableExtraFieldData.java
org.apache.tools.zip.UnrecognizedExtraField.java
org.apache.tools.zip.UnsupportedZipFeatureException.java
org.apache.tools.zip.Zip64ExtendedInformationExtraField.java
org.apache.tools.zip.Zip64Mode.java
org.apache.tools.zip.Zip64RequiredException.java
org.apache.tools.zip.ZipConstants.java
org.apache.tools.zip.ZipEightByteInteger.java
org.apache.tools.zip.ZipEncodingHelper.java
org.apache.tools.zip.ZipEncoding.java
org.apache.tools.zip.ZipEntry.java
org.apache.tools.zip.ZipExtraField.java
org.apache.tools.zip.ZipFile.java
org.apache.tools.zip.ZipLong.java
org.apache.tools.zip.ZipOutputStream.java
org.apache.tools.zip.ZipShort.java
org.apache.tools.zip.ZipUtil.java