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程序设计有所帮助。

时间: 2024-09-09 00:43:55

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

使用Java构造和解析Json数据的两种方法(详解二)_java

JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,是理想的数据交换格式.同时,JSON是 JavaScript 原生格式,这意味着在 JavaScript 中处理 JSON数据不须要任何特殊的 API 或工具包. 在www.json.org上公布了很多JAVA下的json构造和解析工具,其中org.json和json-lib比较简单,两者使用上差不多但还是有些区别.下面接着介绍用org.json构造和解析Json数据的方法

Android编程实现手绘及保存为图片的方法(附demo源码下载)_Android

本文实例讲述了Android编程实现手绘及保存为图片的方法.分享给大家供大家参考,具体如下: 运行效果图预览: 应 yzuo_08 要求做了此Demo,跟以前那个手写板Demo不同的是可以将画布的内容保存为图片. 附上关键代码: MainView.java package com.tszy.views; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; impor

Android 三种动画详解及简单实例

Android 三种动画详解 帧动画 一张张图片不断的切换,形成动画效果 在drawable目录下定义xml文件,子节点为animation-list,在这里定义要显示的图片和每张图片的显示时长 <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="

PHP输出XML到页面的3种方法详解_php技巧

第一种方法: 复制代码 代码如下: <?phpheader("Content-type: text/xml");echo "<?xml version=/"1.0/" encoding=/"UTF-8/"?>";echo "<users>";echo "<user>";   echo "<name>";    ec

Android 自定义弹性ListView控件实例代码(三种方法)

关于在Android中实现ListView的弹性效果,有很多不同的方法,网上一搜,也有很多,下面贴出在项目中经常用到的两种实现ListView弹性效果的方法(基本上拿来就可以用),供大家参考: 弹性ListView 第一种方法: import android.content.Context; import android.content.res.Configuration; import android.util.AttributeSet; import android.util.Display

【ANDROID游戏开发十三】(保存游戏数据 [下文])详解SQLITE存储方式,并把SQLITE的数据库文件存储在SD卡中!!!

本站文章均为 李华明Himi 原创,转载务必在明显处注明:  转载自[黑米GameDev街区] 原文链接: http://www.himigame.com/android-game/329.html ----------------------- 『很多童鞋说我的代码运行后,点击home或者back后会程序异常,如果你也这样遇到过,那么你肯定没有仔细读完Himi的博文,第十九篇Himi专门写了关于这些错误的原因和解决方法,这里我在博客都补充说明下,省的童鞋们总疑惑这一块:请点击下面联系进入阅读:

Android编程之绝对布局AbsoluteLayout和相对布局RelativeLayout实例详解_Android

本文实例分析了Android编程之绝对布局AbsoluteLayout和相对布局RelativeLayout.分享给大家供大家参考,具体如下:  一.绝对布局AbsoluteLayout 绝对定位AbsoluteLayout,又可以叫做坐标布局,可以直接指定子元素的绝对位置,这种布局简单直接,直观性强,但是由于手机屏幕尺寸差别比较大,使用绝对定位的适应性会比较差. 下面我们举一个例子看看:例子里的机器人图片大小是250X250,可以看到我们使用android:layout_x和android:l

Android编程开发之TextView控件用法(2种方法)_Android

本文实例讲述了Android编程开发之TextView控件用法.分享给大家供大家参考,具体如下: 这里我们会讲讲常用控件的使用. 在今后的大多数章节里面也是一样的,我们会具体的说说某些控件的用法.因为只要把这些控件组合在一起它们就是一个应用了. 好吧我们直接看看这个控件怎么用. 细心的同学会发现,其实这个控件的内容是定义在values文件夹里面的strings.xml中的. 那么我们只需要给它加一段代码: 复制代码 代码如下: <string name="test">Wel

Android中使用Gson解析JSON数据的两种方法_Android

Json是一种类似于XML的通用数据交换格式,具有比XML更高的传输效率. 从结构上看,所有的数据(data)最终都可以分解成三种类型: 第一种类型是标量(scalar),也就是一个单独的字符串(string)或数字(numbers),比如"北京"这个单独的词. 第二种类型是序列(sequence),也就是若干个相关的数据按照一定顺序并列在一起,又叫做数组(array)或列表(List),比如"北京,上海". 第三种类型是映射(mapping),也就是一个名/值对(