XMl文件解析

// xml 文件

<meimei>
 <product id="1" video="xxx">
  <type>类型1</type>
  <info id="1">
   <starttime>00:25</starttime>
   <endtime>00:30</endtime>
   <content>精彩内容,welcome to !</content>
  </info>

  <info id="2">
   <starttime>00:40</starttime>
   <endtime>00:46</endtime>
   <content>精彩内容,the first demo!</content>
  </info>

  <info id="3">
   <starttime>00:57</starttime>
   <endtime>00:65</endtime>
   <content>精彩内容,the last talk!</content>
  </info>
  <info id="4">
   <starttime>01:45</starttime>
   <endtime>01:50</endtime>
   <content>精彩内容,the last talk!</content>
  </info>
  <info id="5">
   <starttime>02:45</starttime>
   <endtime>02:50</endtime>
   <content>精彩内容,the last talk!</content>
  </info>
  <info id="6">
   <starttime>03:45</starttime>
   <endtime>03:50</endtime>
   <content>精彩内容,the last talk!</content>
  </info>
 </product>
 <product id="2" type="爱上巧克力">
  <type>类型1</type>
  <info id="1">
   <starttime>02:09</starttime>
   <endtime>02:19</endtime>
   <content>金彩内容</content>
  </info>

  <info id="2">
   <starttime>03:53</starttime>
   <endtime>04:58</endtime>
   <content>金彩</content>
  </info>

  <info id="3">
   <starttime>06:32</starttime>
   <endtime>07:33</endtime>
   <content>精彩内容</content>
  </info>
 </product>
</meimei>

 

// 文件下载

package com.vincent.player;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import android.os.Environment;

public class DownloadXml {
 public String download(String urlStr) {
  StringBuffer buffer = new StringBuffer();
  String line = null;
  BufferedReader reader = null;
  try {
   URL url = new URL(urlStr);
   HttpURLConnection urlConnection = (HttpURLConnection) url
     .openConnection();
   reader = new BufferedReader(new InputStreamReader(urlConnection
     .getInputStream()));
   while ((line = reader.readLine()) != null) {
    buffer.append(line).append("\n");
    System.out.println("buffer---line---->" + line);

   }
  } catch (Exception e) {
   // TODO: handle exception
  } finally {
   try {
    reader.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  return buffer.toString();
 }

 public void saveXmlFile(String xmlFile) {
  // String filePath=Environment.getExternalStorageDirectory();
 }

}

 

// xml 解析

package com.vincent.player;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

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

import android.R.integer;
import android.util.Xml;

public class MyXmlParse {

 public List<VideoInfo> getInfos(InputStream inputStream)
   throws XmlPullParserException, IOException {
  List<VideoInfo> videoInfos = null;
  VideoInfo videoInfo = null;
  List<VideoInfo.FragmentInfo> fragmentInfos = null;
  VideoInfo.FragmentInfo fragmentInfo = null;

  // XmlPullParser parser=Xml.newPullParser();
  XmlPullParserFactory parserFactory = XmlPullParserFactory.newInstance();
  XmlPullParser parser = parserFactory.newPullParser();
  /*
   * URL url=new URL(xmlUrlPath); XmlPullParserFactory
   * parserFactory=XmlPullParserFactory.newInstance(); XmlPullParser
   * xmlPullParser=parserFactory.newPullParser();
   * xmlPullParser.setInput(url
   * .openConnection().getInputStream(),"UTF-8");
   */

  parser.setInput(inputStream, "UTF-8");
  int event = parser.getEventType();
  while (event != parser.END_DOCUMENT) {
   switch (event) {
   case XmlPullParser.START_DOCUMENT:
    videoInfos = new ArrayList<VideoInfo>();
    // delete
    // fragmentInfos=new ArrayList<VideoInfo.FragmentInfo>();
    break;
   case XmlPullParser.START_TAG:
    if ("product".equals(parser.getName())) {
     videoInfo = new VideoInfo();
     videoInfo.setMovieId(Integer.parseInt(parser
       .getAttributeValue(0)));
     videoInfo.setMovieName(parser.getAttributeValue(1));
     // add
     if (fragmentInfos == null) {
      fragmentInfos = new ArrayList<VideoInfo.FragmentInfo>();
     }
    }
    if (videoInfo != null) {
     if ("type".equals(parser.getName())) {
      videoInfo.setType(parser.nextText());
     } else if ("info".equals(parser.getName())) {
      fragmentInfo = videoInfo.new FragmentInfo();
      fragmentInfo.setId(Integer.parseInt(parser
        .getAttributeValue(0)));
     }
     if (fragmentInfo != null) {
      if ("starttime".equals(parser.getName())) {
       fragmentInfo.setStartTime(parser.nextText());
      } else if ("endtime".equals(parser.getName())) {
       fragmentInfo.setEndTime(parser.nextText());
      } else if ("content".equals(parser.getName())) {
       fragmentInfo.setContent(parser.nextText());
      }
     }
    }
    break;
   case XmlPullParser.END_TAG:
    if ("info".equals(parser.getName())) {
     fragmentInfos.add(fragmentInfo);
     fragmentInfo = null;
    }
    if ("product".equals(parser.getName())) {
     videoInfo.setFragmentInfos(fragmentInfos);
     // add
     fragmentInfos = null;
     videoInfos.add(videoInfo);
     videoInfo = null;
    }
    break;
   default:
    break;
   }
   event = parser.next();
  }

  return videoInfos;

 }

 public InputStream getInputStream(String urlStr) {
  InputStream inputStream = null;
  try {

   URL url = new URL(urlStr);
   HttpURLConnection urlConnection = (HttpURLConnection) url
     .openConnection();
   inputStream = urlConnection.getInputStream();
  } catch (Exception e) {
   // TODO: handle exception
  }

  return inputStream;
 }
}

// 文件类

package com.vincent.player;

import java.util.List;

public class VideoInfo {
 private String movieName;
 private int movieId;
 private String type;
 private List<FragmentInfo> fragmentInfos;

 public List<FragmentInfo> getFragmentInfos() {
  return fragmentInfos;
 }

 public void setFragmentInfos(List<FragmentInfo> fragmentInfos) {
  this.fragmentInfos = fragmentInfos;
 }

 public int getMovieId() {
  return movieId;
 }

 public void setMovieId(int movieId) {
  this.movieId = movieId;
 }

 public String getMovieName() {
  return movieName;
 }

 public void setMovieName(String movieName) {
  this.movieName = movieName;
 }

 public String getType() {
  return type;
 }

 public void setType(String type) {
  this.type = type;
 }

 public class FragmentInfo {
  private int id;
  private String startTime;
  private String endTime;
  private String content;

  public int getId() {
   return id;
  }

  public void setId(int id) {
   this.id = id;
  }

  public String getStartTime() {
   return startTime;
  }

  public void setStartTime(String startTime) {
   this.startTime = startTime;
  }

  public String getEndTime() {
   return endTime;
  }

  public void setEndTime(String endTime) {
   this.endTime = endTime;
  }

  public String getContent() {
   return content;
  }

  public void setContent(String content) {
   this.content = content;
  }

 }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值