淘先锋技术网

首页 1 2 3 4 5 6 7


 public static String writeXml(ArrayList<MyTrackPoints> list){ 
  XmlSerializer serializer = Xml.newSerializer(); 
  StringWriter writer = new StringWriter();
  try {
   serializer.setOutput(writer);
   serializer.startDocument("UTF-8",true); 
   serializer.startTag("","track");
   serializer.attribute("", "count", String.valueOf(list.size()));
   for(int i = 0;i<list.size();i++){
    serializer.startTag("", "item");
    serializer.attribute("", "id", String.valueOf(i+1));
    serializer.startTag("", "bigtime");
    serializer.text(list.get(i).time);
    serializer.endTag("", "bigtime");
    serializer.startTag("", "points");
    serializer.attribute("", "count", String.valueOf(list.get(i).list.size()));
    for(int j = 0;j<list.get(i).list.size();j++){
     serializer.startTag("", "point");
     serializer.attribute("", "id", String.valueOf(j+1));
     serializer.startTag("", "time");
     serializer.text(list.get(i).list.get(j).time);
     serializer.endTag("", "time");
     serializer.startTag("", "latitude");
     serializer.text(list.get(i).list.get(j).latitude);
     serializer.endTag("", "latitude");
     serializer.startTag("", "longitude");
     serializer.text(list.get(i).list.get(j).longitude);
     serializer.endTag("", "longitude");
     serializer.endTag("", "point");
    }
    serializer.endTag("", "points");
    serializer.endTag("", "item");
   }
   serializer.endTag("", "track");
   serializer.endDocument();
   return writer.toString();
  } catch (IOException e) {
   e.printStackTrace();
   return null; 
  }
 }

 


 public static ArrayList<MyTrackPoints> parserXml(InputStream input){
  ArrayList<MyTrackPoints> list = null;
  MyTrackPoints points = null;
  MyTrackPoint point = null;
  try {
   XmlPullParser parser = Xml.newPullParser();
   parser.setInput(input, "UTF-8");
   String tagName = null;
   int event = parser.getEventType();

   while (event != XmlPullParser.END_DOCUMENT) {
    switch (event) {
    case XmlPullParser.START_DOCUMENT:
     list = new ArrayList<MyTrackPoints>();
     break;
    case XmlPullParser.START_TAG:
     tagName = parser.getName();
     if(tagName.equals("item")){
      points = new MyTrackPoints();
     }else if(tagName.equals("point")){
      point = new MyTrackPoint();
     }else if(tagName.equals("bigtime")) {
      points.time = parser.nextText();
     }else if(tagName.equals("time")) {
      point.time = parser.nextText();
     }else if(tagName.equals("latitude")) {
      point.latitude = parser.nextText();
     }else if(tagName.equals("longitude")) {
      point.longitude = parser.nextText();
     }
     break;
    case XmlPullParser.END_TAG:
     tagName = parser.getName();
     if(tagName.equals("item")){
      list.add(points);
     }else if(tagName.equals("point")){
      points.list.add(point);
     }
     break;
    }
    event = parser.next(); 
   }
   if (input != null)
    input.close();
   input = null;
  } catch (Exception e) {
   e.printStackTrace();
   return null;
  }
  return list;
 }

 

public class MyTrackPoints {
 
 public int id;
 public String guid;
 public String time;
 public ArrayList<MyTrackPoint> list = new ArrayList<MyTrackPoint>();
}

 

public class MyTrackPoint implements Serializable{
 
 private static final long serialVersionUID = 1L;
 
 public int id = 0;
 
 public String time;
 
 public String latitude;
 
 public String longitude;
 
 
 public String guid;
}