Android编程实现XML解析与保存的三种方法详解

本文实例讲述了Android编程实现XML解析与保存的三种方法。分享给大家供大家参考,具体如下:

简介

在Android开发中,关于XML解析有三种方式,分别是:

1. SAX 基于事件的解析器,解析速度快,占用内存少。非常适合在Android移动设备中使用。
2. DOM 在内存中以树形结构存放,因此检索和更新效率会更高。但是对于特别大的文档,解析和加载整个文档将会很耗资源
3. PULL 基于事件的解析器,不同于SAX是,PULL是主动请求下一个事件,所以在可控上PULL要比SAX实用。Android系统内部在解析各种XML时也是用PULL解析器。

下面实现各种解析的类以及工程结构

这是我们将要解析的XML文件id.xml以及其对应的Book类

<?xml version="1.0" encoding="UTF-8"?>
<books>
 <book id="1001">
  <name>Thinking In Java</name>
  <price>80.0</price>
 </book>
 <book id="1002">
  <name>Core Java</name>
  <price>90.0</price>
 </book>
 <book id="1003">
  <name>Hello, Andriod</name>
  <price>100.0</price>
 </book>
</books>

Book.Java

public class Book {
  public static final String BOOKS = "books";
  public static final String BOOK = "book";
  public static final String ID = "id";
  public static final String NAME = "name";
  public static final String PRICE = "price";
  private int id;
  private String name;
  private float price;
  public int getId() {
    return id;
  }
  public void setId(int id) {
    this.id = id;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public float getPrice() {
    return price;
  }
  public void setPrice(float price) {
    this.price = price;
  }
  @Override
  public String toString() {
    return "id:" + id + ",name:" + name + ",price:" + price;
  }
}

下面是XML解析类的实现

抽象类用于 XML文件读写的继承

XMLParseFactory.java

import java.io.InputStream;
import java.util.List;
public abstract class XMLParseFactory {
  /**
   * 读取指定的XML文件
   * @param inputStream XML文件输入流
   */
  public abstract void readXML(InputStream inputStream);
  /**
   * 保存XML到指定的文件
   * @param filePath 文件的绝对路径
   */
  public abstract void writeXML(String filePath);
  /**
   * 获取Book对象列表
   * @return
   */
  public abstract List<Book> getBookList();
  /**
   * 设置Book对象列表
   */
  public abstract void setBookList(List<Book> bookList);
}

用SAX实现的方法

SAXParserTool.java

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
import org.xml.sax.helpers.DefaultHandler;
public class SAXParserTool extends XMLParseFactory {
// private static final String TAG = "SAXParserTool";
  private SAXHandler mHandler = new SAXHandler();
  private List<Book> mBookList;
  @Override
  public void readXML(InputStream inputStream) {
    try {
      SAXParserFactory factory = SAXParserFactory.newInstance();
      SAXParser parser = factory.newSAXParser();
      parser.parse(inputStream, mHandler);
    } catch (ParserConfigurationException e) {
      e.printStackTrace();
    } catch (SAXException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (inputStream != null) {
        try {
          inputStream.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }
  @Override
  public void writeXML(String filePath) {
    SAXTransformerFactory factory = (SAXTransformerFactory) TransformerFactory.newInstance();
    try {
      TransformerHandler handler = factory.newTransformerHandler();
      Transformer transformer = handler.getTransformer();
      transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
      transformer.setOutputProperty(OutputKeys.INDENT, "yes");
      transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
      FileOutputStream fos = new FileOutputStream(filePath);
      OutputStreamWriter writer = new OutputStreamWriter(fos);
      Result result = new StreamResult(writer);
      handler.setResult(result);
      String uri = "";
      String localName = "";
      handler.startDocument();
      handler.startElement(uri, localName, Book.BOOKS, null);
      AttributesImpl attrs = new AttributesImpl();
      char[] ch = null;
      for (Book book:mBookList) {
        attrs.clear();
        attrs.addAttribute(uri, localName, Book.ID, "string", String.valueOf(book.getId()));
        handler.startElement(uri, localName, Book.BOOK, attrs);
        handler.startElement(uri, localName, Book.NAME, null);
        ch = book.getName().toCharArray();
        handler.characters(ch, 0, ch.length);
        handler.endElement(uri, localName, Book.NAME);
        handler.startElement(uri, localName, Book.PRICE, null);
        ch = String.valueOf(book.getPrice()).toCharArray();
        handler.characters(ch, 0, ch.length);
        handler.endElement(uri, localName, Book.PRICE);
        handler.endElement(uri, localName, Book.BOOK);
      }
      handler.endElement(uri, localName, Book.BOOKS);
      handler.endDocument();
    } catch (TransformerConfigurationException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (SAXException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
  /**
   * 获取BookList列表
   * @return
   */
  public List<Book> getBookList() {
    if (mHandler == null) {
      return null;
    }
    return mHandler.getBookList();
  }
  /**
   * 设置BookList列表
   * @param bookList
   */
  public void setBookList(List<Book> bookList) {
    mBookList = bookList;
  }
  class SAXHandler extends DefaultHandler {
    private List<Book> mBookList;
    private Book mBook;
    private String mTargetName;
    public List<Book> getBookList() {
      return mBookList;
    }
    @Override
    public void startDocument() throws SAXException {
      super.startDocument();
      mBookList = new ArrayList<Book>();
    }
    @Override
    public void endDocument() throws SAXException {
      super.endDocument();
    }
    @Override
    public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException {
      super.startElement(uri, localName, qName, attributes);
      if (localName.equals(Book.BOOK)) {
        mBook = new Book();
        mBook.setId(Integer.valueOf(attributes.getValue(Book.ID)));
      }
      mTargetName = localName;
    }
    @Override
    public void endElement(String uri, String localName, String qName)
        throws SAXException {
      super.endElement(uri, localName, qName);
      if (Book.BOOK.equals(localName)) {
        mBookList.add(mBook);
      }
      mTargetName = null;
    }
    @Override
    public void characters(char[] ch, int start, int length)
        throws SAXException {
      super.characters(ch, start, length);
      if (Book.NAME.equals(mTargetName)) {
        mBook.setName(new String(ch, start, length));
      } else if (Book.PRICE.equals(mTargetName)) {
        mBook.setPrice(Float.valueOf(new String(ch, start, length)));
      }
    }
  }
}

用PULL实现的方法

PULLParserTool.java

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;
import android.util.Xml;
public class PULLParserTool extends XMLParseFactory {
// private static final String TAG = "PULLParserTool";
  private List<Book> mBookList;
  private Book mBook;
  @Override
  public void readXML(InputStream inputStream) {
    try {
      XmlPullParser parser = Xml.newPullParser();
      parser.setInput(inputStream, "UTF-8");
      int eventType = parser.getEventType();
      while (eventType != XmlPullParser.END_DOCUMENT) {
        String name = parser.getName();
        switch (eventType) {
        case XmlPullParser.START_DOCUMENT:
          mBookList = new ArrayList<Book>();
          break;
        case XmlPullParser.START_TAG:
//         Log.d(TAG, "getName:"+parser.getName()+","+parser.getText());
          if (Book.BOOK.equals(name)) {
            mBook = new Book();
            mBook.setId(Integer.valueOf(parser.getAttributeValue("", Book.ID)));
          } else if (Book.NAME.equals(name)) {
            mBook.setName(parser.nextText());
          } else if (Book.PRICE.equals(name)) {
            mBook.setPrice(Float.valueOf(parser.nextText()));
          }
          break;
        case XmlPullParser.END_TAG:
          if (Book.BOOK.equals(name)) {
            mBookList.add(mBook);
          }
          break;
        default:
          break;
        }
        eventType = parser.next();
      }
    } catch (XmlPullParserException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (inputStream != null) {
        try {
          inputStream.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }
  @Override
  public void writeXML(String filePath) {
    XmlSerializer serializer = Xml.newSerializer();
    FileOutputStream fos = null;
    try {
      fos = new FileOutputStream(filePath);
      serializer.setOutput(fos, "UTF-8");
      serializer.startDocument("UTF-8", true);
      serializer.startTag("", Book.BOOKS);
      for (Book book:mBookList) {
        serializer.startTag("", Book.BOOK);
        serializer.attribute("", Book.ID, book.getId()+"");
        serializer.startTag("", Book.NAME);
        serializer.text(book.getName());
        serializer.endTag("", Book.NAME);
        serializer.startTag("", Book.PRICE);
        serializer.text(String.valueOf(book.getPrice()));
        serializer.endTag("", Book.PRICE);
        serializer.endTag("", Book.BOOK);
      }
      serializer.endTag("", Book.BOOKS);
      serializer.endDocument();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IllegalArgumentException e) {
      e.printStackTrace();
    } catch (IllegalStateException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (fos != null) {
        try {
          fos.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }
  public List<Book> getBookList() {
    return mBookList;
  }
  public void setBookList(List<Book> bookList) {
    mBookList = bookList;
  }
}

用DOM实现的方法

DOMParserTool.java

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import android.util.Log;
public class DOMParserTool extends XMLParseFactory {
  private static final String TAG = "DOMParserTool";
  private List<Book> mBookList;
  @Override
  public void readXML(InputStream inputStream) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    try {
      DocumentBuilder builder = factory.newDocumentBuilder();
      Document parse = builder.parse(inputStream);
      Element root = parse.getDocumentElement();
      NodeList nodeList = root.getElementsByTagName(Book.BOOK);
      mBookList = new ArrayList<Book>();
      Log.d(TAG,"length:"+nodeList.getLength());
      for (int i = 0; i < nodeList.getLength(); i++) {
        Book book = new Book();
        Element item = (Element) nodeList.item(i);
        book.setId(Integer.valueOf(item.getAttribute(Book.ID)));
        NodeList nodes = item.getChildNodes();
        for (int j = 0; j < nodes.getLength(); j++) {
          Node node = nodes.item(j);
          if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (Book.NAME.equals(node.getNodeName())) {
              String content = node.getTextContent();
              book.setName(content);
            } else if (Book.PRICE.equals(node.getNodeName())) {
              String content = node.getTextContent();
              book.setPrice(Float.valueOf(content));
            }
          }
          Log.d(TAG, "readXML:"+node.getLocalName()+","+node.getNodeName()
              +","+node.getNodeType()+","+node.getNodeValue()
              +","+node.getTextContent()
              +","+node.toString());
        }
        mBookList.add(book);
      }
    } catch (ParserConfigurationException e) {
      e.printStackTrace();
    } catch (SAXException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (inputStream != null) {
        try {
          inputStream.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }
  @Override
  public void writeXML(String filePath) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    try {
      DocumentBuilder builder = factory.newDocumentBuilder();
      Document document = builder.newDocument();
      Element rootElement = document.createElement("books");
      for (Book book:mBookList) {
        Element bookElement = document.createElement("book");
        bookElement.setAttribute(Book.ID, book.getId()+"");
        Element nameElement = document.createElement(Book.NAME);
        nameElement.setTextContent(book.getName());
        bookElement.appendChild(nameElement);
        Element priceElement = document.createElement(Book.PRICE);
        priceElement.setTextContent(String.valueOf(book.getPrice()));
        bookElement.appendChild(priceElement);
        rootElement.appendChild(bookElement);
      }
      document.appendChild(rootElement);
      TransformerFactory transfactory = TransformerFactory.newInstance();
      Transformer transformer = transfactory.newTransformer();
      transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");// 设置输出采用的编码方式
      transformer.setOutputProperty(OutputKeys.INDENT, "yes");// 是否自动添加额外的空白
      transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");// 是否忽略XML声明
      FileOutputStream fos = new FileOutputStream(filePath);
      Source source = new DOMSource(document);
      Result result = new StreamResult(fos);
      transformer.transform(source, result);
    } catch (ParserConfigurationException e) {
      e.printStackTrace();
    } catch (TransformerConfigurationException e) {
      e.printStackTrace();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (TransformerException e) {
      e.printStackTrace();
    }
  }
  public List<Book> getBookList() {
    return mBookList;
  }
  public void setBookList(List<Book> bookList) {
    mBookList = bookList;
  }
}

这是调用各种解析类的实现

MainActivity.java

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import com.example.xmlparsedemo.xmlparser.Book;
import com.example.xmlparsedemo.xmlparser.DOMParserTool;
import com.example.xmlparsedemo.xmlparser.PULLParserTool;
import com.example.xmlparsedemo.xmlparser.SAXParserTool;
import com.example.xmlparsedemo.xmlparser.XMLParseFactory;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
  private TextView mText;
  private XMLParseFactory parseFactory;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    init();
  }
  private void init() {
    Button saxReadBtn = (Button) findViewById(R.id.id_saxread);
    Button saxWriteBtn = (Button) findViewById(R.id.id_saxwrite);
    Button domReadBtn = (Button) findViewById(R.id.id_domread);
    Button domWriteBtn = (Button) findViewById(R.id.id_domwrite);
    Button pullReadBtn = (Button) findViewById(R.id.id_pullread);
    Button pullWriteBtn = (Button) findViewById(R.id.id_pullwrite);
    mText = (TextView) findViewById(R.id.id_text);
    saxReadBtn.setOnClickListener(this);
    saxWriteBtn.setOnClickListener(this);
    domReadBtn.setOnClickListener(this);
    domWriteBtn.setOnClickListener(this);
    pullReadBtn.setOnClickListener(this);
    pullWriteBtn.setOnClickListener(this);
  }
  @Override
  public void onClick(View v) {
    InputStream inputStream;
    try {
      inputStream = getAssets().open("id.xml");
      switch (v.getId()) {
      case R.id.id_saxread:
        parseFactory = new SAXParserTool();
        parseFactory.readXML(inputStream);
        showBookList(parseFactory.getBookList());
        break;
      case R.id.id_saxwrite:
        parseFactory.setBookList(parseFactory.getBookList());
        parseFactory.writeXML(Environment.getExternalStorageDirectory().getPath()+"/id.xml");
        break;
      case R.id.id_domread:
        parseFactory = new DOMParserTool();
        parseFactory.readXML(inputStream);
        showBookList(parseFactory.getBookList());
        break;
      case R.id.id_domwrite:
        parseFactory.writeXML(Environment.getExternalStorageDirectory().getPath()+"/id2.xml");
        break;
      case R.id.id_pullread:
        parseFactory = new PULLParserTool();
        parseFactory.readXML(inputStream);
        showBookList(parseFactory.getBookList());
        break;
      case R.id.id_pullwrite:
        parseFactory.writeXML(Environment.getExternalStorageDirectory().getPath()+"/id3.xml");
        break;
      default:
        break;
      }
    } catch (IOException e1) {
      e1.printStackTrace();
    }
  }
  private void showBookList(List<Book> bookList) {
    StringBuilder builder = new StringBuilder();
    for (Book book:bookList) {
      builder.append(book.toString());
      builder.append("\n");
    }
    mText.setText(builder.toString());
  }
}

解析实现的主布局

activity_main.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent" >
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <Button
      android:id="@+id/id_saxread"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="SAXParserRead" />
    <Button
      android:id="@+id/id_saxwrite"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="SAXParserWrite" />
    <Button
      android:id="@+id/id_domread"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="DOMParserRead" />
    <Button
      android:id="@+id/id_domwrite"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="DOMParserWrite" />
    <Button
      android:id="@+id/id_pullread"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="PULLParserRead" />
    <Button
      android:id="@+id/id_pullwrite"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="PULLParserWrite" />
    <TextView
      android:id="@+id/id_text"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" />
  </LinearLayout>
</ScrollView>

效果图

参考文章
http://www.jb51.net/article/97247.htm
http://www.jb51.net/article/97376.htm

PS:这里再为大家提供几款关于xml操作的在线工具供大家参考使用:

在线XML/JSON互相转换工具:
http://tools.jb51.net/code/xmljson

在线格式化XML/在线压缩XML:
http://tools.jb51.net/code/xmlformat

XML在线压缩/格式化工具:
http://tools.jb51.net/code/xml_format_compress

XML代码在线格式化美化工具:
http://tools.jb51.net/code/xmlcodeformat

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android操作XML数据技巧总结》、《Android编程之activity操作技巧总结》、《Android资源操作技巧汇总》、《Android文件操作技巧汇总》、《Android开发入门与进阶教程》、《Android视图View技巧总结》及《Android控件用法总结》

希望本文所述对大家Android程序设计有所帮助。

(0)

相关推荐

  • android layout XML解析错误的解决方法

    提示: org.xmlpull.v1.XmlPullParserException: PI must not start with xml (position:unknown @1:5 in java.io.InputStreamReader@47ec2770) org.xml.sax.SAXParseException: PI must not start with xml (position:unknown xm@3:5 in java.io.InputStreamReader@43e

  • Android中使用PULL方式解析XML文件深入介绍

    一.基本介绍 Android中极力推荐xmlpull方式解析xml. xmlpull不仅可用在Android上同样也适用于javase,但在javase环境中需自己获取xmlpull所依赖的类库,kxml2-2.3.0.jar,xmlpull_1_1_3_4c.jar. jar包下载网址 http://www.xmlpull.org/ http://kxml.sourceforge.net/ 二.例子 读取到xml的声明返回数字0 START_DOCUMENT; 读取到xml的结束返回数字1 E

  • Android 使用Pull方法解析XML文件的方法

    Pull解析方法给应用程序完全的控制文档该怎么样被解析.Android中对Pull方法提供了支持的API,主要是 复制代码 代码如下: org.xmlpull.v1.XmlPullParser;org.xmlpull.v1.XmlPullParserFactory; 二个类,其中主要使用的是XmlPullParser,XmlPullParserFactory是一个工厂,用于构建XmlPullParser对象.应用程序通过调用XmlPullParser.next()等方法来产生Event,然后再处

  • Android中使用pull解析器操作xml文件的解决办法

    一.使用Pull解析器读取XML文件 除了可以使用SAX或DOM解析XML文件之外,大家也可以使用Android内置的Pull解析器解析XML文件. Pull解析器是一个开源的java项目,既可以用于android,也可以用于JavaEE.如果用在javaEE需要把其jar文件放入类路径中,因为Android已经集成进了Pull解析器,所以无需添加任何jar文件.android系统本身使用到的各种xml文件,其内部也是采用Pull解析器进行解析的. Pull解析器的运行方式与SAX 解析器相似.

  • Android提高之XML解析与生成实例详解

    本文实例讲述了使用SAX来解析XML.通常来说在Android里面可以使用SAX和DOM,DOM需要把整个XML文件读入内存再解析,比较消耗内存,而SAX是基于事件驱动的处理方式,可以在各节点触发回调函数,不过SAX适合节点结构简单的XML文档,复杂的XML文档在后期的节点深度处理会有点麻烦. 本文要解析的test.xml文件如下: <?xml version="1.0" encoding="utf-8"?> <test> <title

  • Android使用Pull解析器解析xml文件的实现代码

    2个类:1个实体类Person.java,1个继承Activity的类 1.Person.java 复制代码 代码如下: package com.mrzhu.work_1_sax; public class Person { private String personId; private String name; private String address; private String tel; private String fax; private String email; publi

  • android编程之XML文件解析方法详解(附源码)

    本文实例讲述了android编程之XML文件解析方法.分享给大家供大家参考,具体如下: 在android开发中,经常用到去解析xml文件,常见的解析xml的方式有一下三种:SAX.Pull.Dom解析方式.最近做了一个android版的CSDN阅读器,用到了其中的两种(sax,pull),今天对android解析xml的这三种方式进行一次总结. 今天解析的xml示例(channels.xml)如下: <?xml version="1.0" encoding="utf-8

  • 基于Android XML解析与保存的实现

    解析XML文件:在Android平台上可以使用SAX.DOM和Android附带的pull解析器解析XML文件:pull解析器提供了各种事件,使用parser.next()方法进入下一个元素并触发相应事件,pull解析器产生的事件是一个数字,可以通过switch对感兴趣的事件进行处理:通过paser.nextText()方法可以获取下一个Text类型的节点的值:http://xmlpull.org/xmlpull-website/impls.shtml; 读取XMLXmlPullParser p

  • Android 解析XML 文件的四种方法总结

    java解析xml文件四种方式 1.介绍 1)DOM(JAXP Crimson解析器) DOM是用与平台和语言无关的方式表示XML文档的官方W3C标准.DOM是以层次结构组织的节点或信息片断的集合.这个层次结构允许开发人员在树中寻找特定信息.分析该结构通常需要加载整个文档和构造层次结构,然后才能做任何工作.由于它是基于信息层次的,因而DOM被认为是基于树或基于对象的.DOM以及广义的基于树的处理具有几个优点.首先,由于树在内存中是持久的,因此可以修改它以便应用程序能对数据和结构作出更改.它还可以

  • Android开发之XML文件解析的使用

    前言  本文主要介绍在Android中怎样来解析XML文件.主要采用的是SAX机制,SAX全称为Simple API for XML,它既是一种接口,也是一个软件包.作为接口,SAX是事件驱动型XML解析的一个标准接口.XML文件解析一般有2种方法,DOM和SAX.其中DOM需要先将xml文档全部读入到电脑内存中,当文档内容太大时,该方法并不适用.SAX就比较好的解决了该问题,它是逐行解析的,可以随时中断.但是SAX的操作比较复杂.因此,这2种方法各有优缺点,看具体应用情况.在前面的文章Qt学习

  • Android中使用sax解析xml文件的方法

    SAX是一个解析速度快并且占用内存少的xml解析器,非常适合用于Android等移动设备. SAX解析XML文件采用的是事件驱动,也就是说,它并不需要解析完整个文档,在按内容顺序解析文档的过程中,SAX会判断当前读到的字符是否合法XML 语法中的某部分,如果符合就会触发事件.所谓事件,其实就是一些回调(callback)方法,这些方法(事件)定义在ContentHandler接口.下面是一些ContentHandler接口常用的方法: startDocument():当遇到文档的开头的时候,调用

随机推荐