没有合适的资源?快使用搜索试试~ 我知道了~
android中xml的创建以及一些解析xml的常用方法.doc
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 95 浏览量
2022-07-08
17:58:43
上传
评论
收藏 297KB DOC 举报
温馨提示
试读
15页
android中xml的创建以及一些解析xml的常用方法
资源推荐
资源详情
资源评论
中大软件与您分享 android 中 xml 的创建以及一些解析 xml 的常用方法。
首先是创建,我们用 XmlSerializer 这个类来创建一个 xml 文件,其次是解析 xml 文件,常用的有
dom,sax,XmlPullParser 等方法,由于 sax 代码有点复杂,本节只讲解一下 dom 与 XmlPullParser 解析,sax
我将会在下一节单独讲解,至于几种解析 xml 的优缺点我就不再讲述了。
为了方便理解,我做了一个简单的 Demo。首先首界面有三个按钮,点击第一个按钮会在 sdcard 目录下创
建一个 books.xml 文件,另外两个按钮分别是调用 dom 与 XmlPullParser 方法解析 xml 文件,并将结果显示
在一个 TextView 里。大家可以按照我的步骤一步步来:
第一步:新建一个 Android 工程,命名为 XmlDemo.
第二步:修改 main.xml 布局文件,代码如下:
view plaincopy to clipboardprint?
1. <?xml version="1.0" encoding="utf-8"?>
2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. android:orientation="vertical"
4. android:layout_width="fill_parent"
5. android:layout_height="fill_parent"
6. >
7. <Button
8. android:id="@+id/btn1"
9. android:layout_width="fill_parent"
10. android:layout_height="wrap_content"
11. android:text="创建 XML 文件"
12. />
13. <Button
14. android:id="@+id/btn2"
15. android:layout_width="fill_parent"
16. android:layout_height="wrap_content"
17. android:text="DOM 解析 XML"
18. />
19. <Button
20. android:id="@+id/btn3"
21. android:layout_width="fill_parent"
22. android:layout_height="wrap_content"
23. android:text="XmlPullParse 解析 XML"
24. />
25. <TextView
26. android:id="@+id/result"
27. android:layout_width="fill_parent"
28. android:layout_height="wrap_content"
29. />
30. </LinearLayout>
1. <?xml version="1.0" encoding="utf-8"?>
2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. android:orientation="vertical"
4. android:layout_width="fill_parent"
5. android:layout_height="fill_parent"
6. >
7. <Button
8. android:id="@+id/btn1"
9. android:layout_width="fill_parent"
10. android:layout_height="wrap_content"
11. android:text="创建 XML 文件"
12. />
13. <Button
14. android:id="@+id/btn2"
15. android:layout_width="fill_parent"
16. android:layout_height="wrap_content"
17. android:text="DOM 解析 XML"
18. />
19. <Button
20. android:id="@+id/btn3"
21. android:layout_width="fill_parent"
22. android:layout_height="wrap_content"
23. android:text="XmlPullParse 解析 XML"
24. />
25. <TextView
26. android:id="@+id/result"
27. android:layout_width="fill_parent"
28. android:layout_height="wrap_content"
29. />
30. </LinearLayout>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="DOM解析XML"
/>
<Button
android:id="@+id/btn3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="XmlPullParse解析XML"
/>
<TextView
android:id="@+id/result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
第三步:修改主核心程序 XmlDemo.java,代码如下:
view plaincopy to clipboardprint?
1. package com.tutor.xml;
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.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. 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. 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(savedInstanceState);
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)findViewById(R.id.btn3);
43. mButton1.setOnClickListener(this);
44. mButton2.setOnClickListener(this);
剩余14页未读,继续阅读
资源评论
oligaga
- 粉丝: 50
- 资源: 2万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功