Android数据存储——2.文件存储_C_DOM解析XML文档
今天學(xué)習(xí)Android數(shù)據(jù)存儲(chǔ)——文件存儲(chǔ)_DOM解析XML文檔
位于org.w3c.dom操作XML會(huì)比較簡(jiǎn)單,就是將XML看做是一顆樹,DOM就是對(duì)這顆樹的一個(gè)數(shù)據(jù)結(jié)構(gòu)的描述,但對(duì)大型XML文件效果可能會(huì)不理想
首先來(lái)了解點(diǎn)Java DOM 的 API:
1.解析器工廠類:DocumentBuilderFactory
創(chuàng)建的方法:DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance();
2.解析器:DocumentBuilder
創(chuàng)建方法:通過(guò)解析器工廠類來(lái)獲得 DocumentBuilder builder = factory.newDocumentBuilder();
3.文檔樹模型Document
創(chuàng)建方法:
a.通過(guò)xml文檔 Document doc = builder.parse("member.xml");??
b.將需要解析的xml文檔轉(zhuǎn)化為輸入流 InputStream input = new FileInputStream("member.xml");
???Document doc =builder.parse(input?);?
Document對(duì)象代表了一個(gè)XML文檔的模型樹,所有的其他Node都以一定的順序包含在Document對(duì)象之內(nèi),排列成一個(gè)樹狀結(jié)構(gòu),以后對(duì)XML文檔的所有操作都與解析器無(wú)關(guān),直接在這個(gè)Document對(duì)象上進(jìn)行操作即可;
?包含的方法:
4.節(jié)點(diǎn)列表類NodeList
NodeList代表了一個(gè)包含一個(gè)或者多個(gè)Node的列表,根據(jù)操作可以將其簡(jiǎn)化的看做為數(shù)組
5.節(jié)點(diǎn)類Node
Node對(duì)象是DOM中最基本的對(duì)象,代表了文檔樹中的抽象節(jié)點(diǎn)。但在實(shí)際使用中很少會(huì)直接使用Node對(duì)象,而是使用Node對(duì)象的子對(duì)象Element,Attr,Text等
6.元素類Element
是Node類最主要的子對(duì)象,在元素中可以包含屬性,因而Element中有存取其屬性的方法
7.屬性類Attr
代表某個(gè)元素的屬性,雖然Attr繼承自Node接口,但因?yàn)锳ttr是包含在Element中的,但并不能將其看做是Element的子對(duì)象,因?yàn)锳ttr并不是DOM樹的一部分
基本的知識(shí)就到此結(jié)束,更加具體的大家可以參閱JDK API文檔
使用DOM輸出xml文件
XML布局:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MyDOMDemo" ><TableRow ><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:textSize="20sp"android:text="姓名:"/><TextViewandroid:id="@+id/name"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="李興華"/></TableRow><TableRow ><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:textSize="20sp"android:text="郵箱:"/><TextViewandroid:id="@+id/email"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="mldnqa@163.com"/></TableRow><TableRow > <Buttonandroid:id="@+id/but"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="保存"/></TableRow> </TableLayout>Java代碼:public class MyDOMDemo extends Activity {private TextView name = null;private TextView email = null;private Button but = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);super.setContentView(R.layout.main);this.name=(TextView)super.findViewById(R.id.name);this.email=(TextView)super.findViewById(R.id.email);this.but=(Button)super.findViewById(R.id.but);this.but.setOnClickListener(new OnClickListenerImpl());}private class OnClickListenerImpl implements OnClickListener{@Overridepublic void onClick(View v) {/*** DOM輸出xml文件*/if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){//SD卡不存在則不操作return;//返回到程序的被調(diào)用處}File file = new File(Environment.getExternalStorageDirectory()+File.separator+"mldndata"+File.separator+"member.xml");//要輸出的文件路徑if(!file.getParentFile().exists()){//父路徑不存在file.getParentFile().mkdirs();//創(chuàng)建父文件夾}//實(shí)例化一個(gè)DocumentBuilderFactoryDocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();DocumentBuilder builder = null;try {//創(chuàng)建一個(gè)新的DocumentBuilderbuilder = factory.newDocumentBuilder();} catch (ParserConfigurationException e) {e.printStackTrace();}Document doc = null; doc = builder.newDocument(); //創(chuàng)建一個(gè)新的文檔//新建文檔元素Element addresslist = doc.createElement("addresslist");Element linkman = doc.createElement("linkman");Element name = doc.createElement("name");Element email = doc.createElement("email");//設(shè)置子元素name.appendChild(doc.createTextNode(MyDOMDemo.this.name.getText().toString()));email.appendChild(doc.createTextNode(MyDOMDemo.this.email.getText().toString()));linkman.appendChild(name);linkman.appendChild(email);addresslist.appendChild(linkman);doc.appendChild(addresslist);//實(shí)例化一個(gè)TransformerFactoryTransformerFactory tf = TransformerFactory.newInstance();Transformer t = null;try {//創(chuàng)建一個(gè)新的Transformert = tf.newTransformer();} catch (TransformerConfigurationException e) {e.printStackTrace();}//設(shè)置輸出屬性,編碼為GBKt.setOutputProperty(OutputKeys.ENCODING, "GBK");DOMSource source = new DOMSource(doc);//XML源文檔StreamResult result = new StreamResult(file);//輸出目標(biāo)try {t.transform(source, result);//輸出xml文件} catch (TransformerException e) {e.printStackTrace(); }}} }查看產(chǎn)生的XML文件
使用DOM讀取xml文件
XML布局:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MyDOMDemo" ><TableRow ><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:textSize="20sp"android:text="姓名:"/><TextViewandroid:id="@+id/name"android:layout_width="match_parent"android:layout_height="wrap_content"/></TableRow><TableRow ><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:textSize="20sp"android:text="郵箱:"/><TextViewandroid:id="@+id/email"android:layout_width="match_parent"android:layout_height="wrap_content"/></TableRow><TableRow ><Buttonandroid:id="@+id/but"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="讀取"/></TableRow> </TableLayout>Java代碼:public class MyDOMDemo extends Activity {private TextView name = null;private TextView email = null;private Button but = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);super.setContentView(R.layout.main);this.name=(TextView)super.findViewById(R.id.name);this.email=(TextView)super.findViewById(R.id.email);this.but=(Button)super.findViewById(R.id.but);this.but.setOnClickListener(new OnClickListenerImpl());}private class OnClickListenerImpl implements OnClickListener{@Overridepublic void onClick(View v) {/*** DOM讀取xml文件*/if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){//SD卡不存在則不操作return;//返回到程序的被調(diào)用處}File file = new File(Environment.getExternalStorageDirectory()+File.separator+"mldndata"+File.separator+"member.xml");//要輸出的文件路徑if(!file.exists()){//文件不存在return;}//實(shí)例化一個(gè)DocumentBuilderFactoryDocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();DocumentBuilder builder = null;try {//創(chuàng)建一個(gè)新的DocumentBuilderbuilder = factory.newDocumentBuilder();} catch (ParserConfigurationException e) {e.printStackTrace();}Document doc = null; try {doc = builder.parse(file);//通過(guò)xml文件轉(zhuǎn)化為文檔} catch (SAXException e1) {e1.printStackTrace();} catch (IOException e1) {e1.printStackTrace();} //從文檔中獲得結(jié)點(diǎn)列表NodeList n1 = doc.getElementsByTagName("linkman");for(int x=0;x<n1.getLength();x++){Element e = (Element)n1.item(x);//取得元素MyDOMDemo.this.name.setText(e.getElementsByTagName("name").item(0).getFirstChild().getNodeValue());MyDOMDemo.this.email.setText(e.getElementsByTagName("email").item(0).getFirstChild().getNodeValue());}}} }讀取結(jié)果:
轉(zhuǎn)載于:https://www.cnblogs.com/coderookie0820/archive/2013/04/15/4367510.html
總結(jié)
以上是生活随笔為你收集整理的Android数据存储——2.文件存储_C_DOM解析XML文档的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: ProgressBar进度条颜色改变
- 下一篇: ssh证书登录(实例详解)