android中创建与几种解析xml的方法

上传人:第*** 文档编号:38771811 上传时间:2018-05-07 格式:DOC 页数:10 大小:258.50KB
返回 下载 相关 举报
android中创建与几种解析xml的方法_第1页
第1页 / 共10页
android中创建与几种解析xml的方法_第2页
第2页 / 共10页
android中创建与几种解析xml的方法_第3页
第3页 / 共10页
android中创建与几种解析xml的方法_第4页
第4页 / 共10页
android中创建与几种解析xml的方法_第5页
第5页 / 共10页
点击查看更多>>
资源描述

《android中创建与几种解析xml的方法》由会员分享,可在线阅读,更多相关《android中创建与几种解析xml的方法(10页珍藏版)》请在金锄头文库上搜索。

1、 大家好今天我今天给大家讲解一下 android 中 xml 的创建以及一些解析 xml 的常用方法。首先是创建,我们用 XmlSerializer 这个类来创建一个 xml 文件,其次是解析 xml 文件,常用的有 dom,sax,XmlPullParser 等方法,由于 sax 代码有点复杂,本节只讲解一下 dom与 XmlPullParser 解析,sax 我将会在下一节单独讲解,至于几种解析 xml 的优缺点我就不再讲述了。为了方便理解,我做了一个简单的 Demo。首先首界面有三个按钮,点击第一个按钮会在sdcard 目录下创建一个 books.xml 文件,另外两个按钮分别是调用 d

2、om 与 XmlPullParser方法解析 xml 文件,并将结果显示在一个 TextView 里。大家可以按照我的步骤一步步来:第一步:新建一个 Android 工程,命名为 XmlDemo.第二步:修改 main.xml 布局文件,代码如下:view plaincopy to clipboardprint?1. 2. 7. 13. 19. 25. 30. Error! Reference source not found.第三步:修改主核心程序 XmlDemo.java,代码如下:view plaincopy to clipboardprint?1.package com.tutor.x

3、ml; 2.import java.io.File; 3.import java.io.FileNotFoundException; 4.import java.io.FileOutputStream; 5.import java.io.IOException; 6.import javax.xml.parsers.DocumentBuilder; 7.import javax.xml.parsers.DocumentBuilderFactory; 8.import javax.xml.parsers.ParserConfigurationException; 9.import org.w3c

4、.dom.Document; 10. import org.w3c.dom.Element; 11. import org.w3c.dom.NodeList; 12. import org.xml.sax.SAXException; 13. import org.xmlpull.v1.XmlPullParser; 14. import org.xmlpull.v1.XmlPullParserException; 15. import org.xmlpull.v1.XmlPullParserFactory; 16. import org.xmlpull.v1.XmlSerializer; 17.

5、 import android.app.Activity; 18. import android.os.Bundle; 19. import android.util.Log; 20. import android.util.Xml; 21. import android.view.View; 22. import android.view.View.OnClickListener; 23. import android.widget.Button; 24. import android.widget.TextView; 25. import android.widget.Toast; 26.

6、 public class XmlDemo extends Activity implements OnClickListener 27.28. private static final String BOOKS_PATH = “/sdcard/books.xml“; 29. private Button mButton1,mButton2,mButton3; 30. private TextView mTextView; 31. Override 32. public void onCreate(Bundle savedInstanceState) 33. super.onCreate(sa

7、vedInstanceState); 34. setContentView(R.layout.main); 35. setupViews(); 36. 37. /初始化工作 38. private void setupViews() 39. mTextView = (TextView)findViewById(R.id.result); 40. mButton1 = (Button)findViewById(R.id.btn1); 41. mButton2 = (Button)findViewById(R.id.btn2); 42. mButton3 = (Button)findViewByI

8、d(R.id.btn3); 43. mButton1.setOnClickListener(this); 44. mButton2.setOnClickListener(this); 45. mButton3.setOnClickListener(this); 46. 47. /创建 xml 文件 48. private void createXmlFile() 49. File linceseFile = new File(BOOKS_PATH); 50. try 51. linceseFile.createNewFile(); 52. catch (IOException e) 53. L

9、og.e(“IOException“, “exception in createNewFile() method“); 54. 55. FileOutputStream fileos = null; 56. try 57. fileos = new FileOutputStream(linceseFile); 58. catch (FileNotFoundException e) 59. Log.e(“FileNotFoundException“, “cant create FileOutputStream“); 60. 61. XmlSerializer serializer = Xml.n

10、ewSerializer(); 62. try 63. serializer.setOutput(fileos,“UTF-8“); 64. serializer.startDocument(null, true); 65. serializer.startTag(null, “books“); 66. for(int i = 0; i 2. 6. 7. 9. 10. 11. 12. 13. 14. 15. 16. 17. Error! Reference source not found.第五步:运行上述工程,查看效果:启动首界面:点击创建 XML 文件按钮,生成 books.xml 文件books.xml 内容如下:view plaincopy to clipboardprint?1. 2. 3. 4.Android 教程 0 5.Frankie0 6. 7. 8.Android 教程 1 9.Frankie1 10. 11. 12. Android 教程 2 13. Frankie2 14. 15. Error! Reference source not found.点击 DOM 解析 XML 按钮:点击 XmlPullParse 解析 XML 按钮:Ok今天就先讲到这里。thx

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 学术论文 > 毕业论文

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号