Android数据存储——2.文件存储_C_DOM解析XML文档
今天學習Android數據存儲——文件存儲_DOM解析XML文檔
位于org.w3c.dom操作XML會比較簡單,就是將XML看做是一顆樹,DOM就是對這顆樹的一個數據結構的描述,但對大型XML文件效果可能會不理想
首先來了解點Java DOM 的 API:
1.解析器工廠類:DocumentBuilderFactory
創建的方法:DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance();
2.解析器:DocumentBuilder
創建方法:通過解析器工廠類來獲得 DocumentBuilder builder = factory.newDocumentBuilder();
3.文檔樹模型Document
創建方法:
a.通過xml文檔 Document doc = builder.parse("member.xml");??
b.將需要解析的xml文檔轉化為輸入流 InputStream input = new FileInputStream("member.xml");
???Document doc =builder.parse(input?);?
Document對象代表了一個XML文檔的模型樹,所有的其他Node都以一定的順序包含在Document對象之內,排列成一個樹狀結構,以后對XML文檔的所有操作都與解析器無關,直接在這個Document對象上進行操作即可;
?包含的方法:
4.節點列表類NodeList
NodeList代表了一個包含一個或者多個Node的列表,根據操作可以將其簡化的看做為數組
5.節點類Node
Node對象是DOM中最基本的對象,代表了文檔樹中的抽象節點。但在實際使用中很少會直接使用Node對象,而是使用Node對象的子對象Element,Attr,Text等
6.元素類Element
是Node類最主要的子對象,在元素中可以包含屬性,因而Element中有存取其屬性的方法
7.屬性類Attr
代表某個元素的屬性,雖然Attr繼承自Node接口,但因為Attr是包含在Element中的,但并不能將其看做是Element的子對象,因為Attr并不是DOM樹的一部分
基本的知識就到此結束,更加具體的大家可以參閱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;//返回到程序的被調用處}File file = new File(Environment.getExternalStorageDirectory()+File.separator+"mldndata"+File.separator+"member.xml");//要輸出的文件路徑if(!file.getParentFile().exists()){//父路徑不存在file.getParentFile().mkdirs();//創建父文件夾}//實例化一個DocumentBuilderFactoryDocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();DocumentBuilder builder = null;try {//創建一個新的DocumentBuilderbuilder = factory.newDocumentBuilder();} catch (ParserConfigurationException e) {e.printStackTrace();}Document doc = null; doc = builder.newDocument(); //創建一個新的文檔//新建文檔元素Element addresslist = doc.createElement("addresslist");Element linkman = doc.createElement("linkman");Element name = doc.createElement("name");Element email = doc.createElement("email");//設置子元素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);//實例化一個TransformerFactoryTransformerFactory tf = TransformerFactory.newInstance();Transformer t = null;try {//創建一個新的Transformert = tf.newTransformer();} catch (TransformerConfigurationException e) {e.printStackTrace();}//設置輸出屬性,編碼為GBKt.setOutputProperty(OutputKeys.ENCODING, "GBK");DOMSource source = new DOMSource(doc);//XML源文檔StreamResult result = new StreamResult(file);//輸出目標try {t.transform(source, result);//輸出xml文件} catch (TransformerException e) {e.printStackTrace(); }}} }查看產生的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;//返回到程序的被調用處}File file = new File(Environment.getExternalStorageDirectory()+File.separator+"mldndata"+File.separator+"member.xml");//要輸出的文件路徑if(!file.exists()){//文件不存在return;}//實例化一個DocumentBuilderFactoryDocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();DocumentBuilder builder = null;try {//創建一個新的DocumentBuilderbuilder = factory.newDocumentBuilder();} catch (ParserConfigurationException e) {e.printStackTrace();}Document doc = null; try {doc = builder.parse(file);//通過xml文件轉化為文檔} catch (SAXException e1) {e1.printStackTrace();} catch (IOException e1) {e1.printStackTrace();} //從文檔中獲得結點列表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());}}} }讀取結果:
轉載于:https://www.cnblogs.com/coderookie0820/archive/2013/04/15/4367510.html
總結
以上是生活随笔為你收集整理的Android数据存储——2.文件存储_C_DOM解析XML文档的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ProgressBar进度条颜色改变
- 下一篇: ssh证书登录(实例详解)