AndRoid XmlPullParser

来源:互联网 发布:网络尖刀 合作 编辑:程序博客网 时间:2024/05/18 03:42

一 操作的XML格式

 xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<books>
<book id="1">
<name>大唐双龙传</name>
<path>1</path>
<size>0.0</size>
<label>0</label>
<font_type>0</font_type>
<font_color>0</font_color>
<font_size>0</font_size>
</book>
<book id="2">
<name>射雕英雄传</name>
<path>2</path>
<size>0.0</size>
<label>0</label>
<font_type>0</font_type>
<font_color>0</font_color>
<font_size>0</font_size>
</book>
<book id="3">
<name>猛龙过江</name>
<path>3</path>
<size>0.0</size>
<label>0</label>
<font_type>0</font_type>
<font_color>0</font_color>
<font_size>0</font_size>
</book>
<book id="4">
<name>寻情记</name>
<path>4</path>
<size>0.0</size>
<label>0</label>
<font_type>0</font_type>
<font_color>0</font_color>
<font_size>0</font_size>
</book>
<book id="5">
<name>云海玉弓缘</name>
<path>5</path>
<size>0.0</size>
<label>0</label>
<font_type>0</font_type>
<font_color>0</font_color>
<font_size>0</font_size>
</book>
</books>


===CODE=========

package com.example.FileManager;


import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;


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


import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;


/**********************
 * 保存和读写配置文件
 *  
 * @author chengxf2
 *  book_name  书名
 *  book_path  书路径
 *  book_size  书的大小
 *  book_label 书签
 *  font_type  字体类型
 *  font_color 字体颜色
 *  font_size  字体大小
 *  back_color 背景颜色
 */


public class BookServices {

/******************
* getBooks
* @param st
* @return books
* @throws Throwable
*/

=====获取XML配置  ====================
public List<Book>  getBooks(InputStream  st) throws Throwable
{

List<Book>   list = null;
Book book = null;
XmlPullParser parser = Xml.newPullParser();
parser.setInput(st, "UTF-8");
int type = parser.getEventType(); //产生事件



while (type!= XmlPullParser.END_DOCUMENT){

switch(type){

case XmlPullParser.START_DOCUMENT:
list = new ArrayList<Book>();
Log.d("==CXF==", "START_DOCUMENT");
break;

case XmlPullParser.START_TAG:
Log.d("==CXF==", "START_TAG");
String name = parser.getName(); //获得名称
Log.d("==CXF==", name);
if("book".equals(name)){
book = new Book();
book.setId(new Integer(parser.getAttributeValue(0)));

}
if(null != book){

if("name".equals(name))
{
book.setName(parser.nextText());
}else if("path".equals(name)){
book.setPath(parser.nextText());
}else if("size".equals(name)){
book.setSize(new Double(parser.nextText()));
}else if("label".equals(name)){
book.setlabel(new Integer(parser.nextText()));
}else if("font_type".equals(name)){
book.setFontType(new Integer(parser.nextText()));
}else if("font_color".equals(name)){
book.setFontColor(new Integer(parser.nextText()));
}else if("font_size".equals(name)){
book.setFontSize(new Integer(parser.nextText()));
}
}
break;
case XmlPullParser.END_TAG:
Log.d("==CXF==", "END_TAG");
if( "book".equals(parser.getName()) && book!=null ){
list.add(book);
book = null;
}
break;
}

type = parser.next();
}
st.close();
return list;



}






===============保存XML配置=============
  
public static void save(List<Book> books, OutputStream out) throws Exception{

XmlSerializer  ser  = Xml.newSerializer();

ser.setOutput(out, "UTF-8");
ser.startDocument("UTF-8",   true);
ser.text("\n");
ser.startTag(null, "books");
Log.d("==CXF==", "v1");
for (Book  book1: books){
ser.text("\n\t");
ser.startTag(null, "book");
ser.attribute(null, "id", String.valueOf(book1.getId()));



ser.text("\n\t\t");
ser.startTag(null, "name");
ser.text(book1.getName());
ser.endTag(null, "name");


ser.text("\n\t\t");
ser.startTag(null, "path");
ser.text(book1.getPath());
ser.endTag(null, "path");

ser.text("\n\t\t");
ser.startTag(null, "size");
ser.text(String.valueOf(book1.getSize()));
ser.endTag(null, "size");

ser.text("\n\t\t");
ser.startTag(null, "label");
ser.text(String.valueOf(book1.getlabel()));
ser.endTag(null, "label");


ser.text("\n\t\t");
ser.startTag(null, "font_type");
ser.text(String.valueOf(book1.getFontType()));
ser.endTag(null, "font_type");

ser.text("\n\t\t");
ser.startTag(null, "font_color");
ser.text(String.valueOf(book1.getFontColor()));
ser.endTag(null, "font_color");

ser.text("\n\t\t");
ser.startTag(null, "font_size");
ser.text(String.valueOf(book1.getFontSize()));
ser.endTag(null, "font_size");
ser.text("\n\t");


ser.endTag(null, "book");


}

Log.d("==CXF==", "v2");
ser.text("\n");
ser.endTag(null, "books");
        ser.endDocument();
        out.flush();
        out.close();
}
}

==================更改某个特定的xml项目===============


原理:

      读取原来整个xml的配置到一个list里面

     遍历list内容,与传入的book对比,如果相同,就更改这一项

      再重新写入xml


   

public void changeBook(Book  book){

 String path = "/mnt/sdcard/temp/config.xml" ;
          File  xmlFile = new File(path);
          List<Book> listBook = null;
          
          if(xmlFile.exists()){
              
       
         BookServices ser = new BookServices();
         try{
         FileInputStream input = new FileInputStream(xmlFile);
           
         try{
         listBook = ser.getBooks(input);
          }catch(Throwable e){
          Log.d("==CXF==", e.getCause().toString());}
          }catch(FileNotFoundException e){
             Log.d("==CXF==", e.getCause().toString());}
         
         for (Book bookTemp: listBook){
        if(bookTemp.getName().equalsIgnoreCase(book.getName())){
       
        bookTemp.setPath(book.getPath());
        bookTemp.setSize(book.getSize());
        bookTemp.setlabel(book.getlabel());
        bookTemp.setFontType(book.getFontType());
        bookTemp.setFontColor(book.getFontColor());
        bookTemp.setFontSize(book.getFontSize());
       
        }
   
         }
         
         try{
        OutputStream out=new FileOutputStream (xmlFile,false);
             
                try{
                       
                    ser.save(listBook, out);
                    }catch(Exception ex){
                        Log.d("==CXF==", ex.getCause().toString());}
          }catch(IOException e){
          Log.d("==CXF==", e.getCause().toString());}  
         
         
          }
}
}


=====Book 类============

package com.example.FileManager;


/********
 *  * @author chengxf2
*  book_name  书名
*  book_path  书路径
*  book_size  书的大小
*  book_label 书签
*  font_type  字体类型
*  font_color 字体颜色
*  font_size  字体大小
*  back_color 背景颜色
 * @author chengxf2
 *
 */


public class Book {

private String book_name;
private String book_path;
private double book_size;
private int    book_label;
private   int id;
private int  font_type;
private int  font_color;
private int  font_size;

public Book(){
book_name = null;
book_path = null;
book_size = 0;
book_label = 0;
    id = 0;
         font_type = 0;
font_color = 0;
 font_size = 0;
}
public Book(String str1, String str2,  double size1, int label1,  
int id1,  int type1,  int color1,  int size2){

this.book_name = str1;
this.book_path = str2;
this.book_size = size1;
this.book_label = label1;
   this.id = id1;
   this.font_color = color1;
   this.font_size = size2;
   this.font_type = type1;

}

/*********
* 获取文件名
* @return
*/
public String getName(){
return book_name;
}
    
/*******
* 设置书名
* @param name
*/
public void setName(String name){
this.book_name= name;
}

/**********
* getPath
* @return 获取路径
*/
public String getPath(){
return book_path;
}

/***********
* 设置路径
* @param path
*/
public void  setPath(String path){
this.book_path= path;
}


/**********
* getSize
* @return 获取大小
*/
public double getSize(){
return this.book_size;
}

/***********
* 设置书的大小
* @param size
*/
public void setSize(double size){

this.book_size = size;
}



/**********
* getlabel
* @return 获取标签
*/
public int getlabel(){
return this.book_label;
}

/*******************
* setlabel
* 设置书签
*/
public void setlabel(int label){
this.book_label  = label;
}


/**********
* getId
* @return 获取书ID
*/
public int  getId(){

return   id;
}

/***********
* 设置ID
* @param id
* @return
*/
public void setId(int id){
this.id = id;
}

/**********
* getFontSize
* @return 获取字体大小
*/
public int getFontSize(){
return this.font_size;
}

/**********************
* setFontSize
* 设置字体大小
*/
public void setFontSize(int size){

this.font_size = size;
}
/**********
* getFontColor
* @return 获取字体颜色
*/

public int getFontColor(){
return this.font_color;
}


/*************
* setFontColor
* 设置颜色
* @param co
*/
public void setFontColor(int co){
this.font_color = co;
}

/**********
* getFontType
* @return  获取字体类型
*/
public int getFontType(){
return this.font_type;
}
    
/**************
* setFontType
* 设置字体类型
*/
public void setFontType(int type){
this.font_type = type;
}
    
    
}

0 0