android--xml的应用

在android中与xml的应用

package com.example.xmltest;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xmlpull.v1.XmlSerializer;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.util.Xml;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	static Document document;
	static Element root;
	Button btn = null;
	TextView text = null;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		btn = (Button)findViewById(R.id.MyBtn);
		text = (TextView)findViewById(R.id.mText);
		
		try {
			createXmlFile();
		} catch (Exception e) {
			e.printStackTrace();
			return ;
		}
		
		btn.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				domParseXML();
			}
			
		});
		
		
	}
	
	void createXmlFile() throws Exception{
		/*DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = dbf.newDocumentBuilder();
		document = db.newDocument();
	
		root = (Element)document.createElement("note");
		document.appendChild(root);
		*/
		
		 File linceseFile = new File(Environment.getExternalStorageDirectory().getPath() ,"books.xml");  
	        try{  
	            linceseFile.createNewFile();  
	        }catch (IOException e) {  
	            Log.e("IOException", "exception in createNewFile() method");  
	        }  
	        FileOutputStream fileos = null;  
	        try{  
	            fileos = new FileOutputStream(linceseFile);  
	        }catch (FileNotFoundException  e) {  
	            Log.e("FileNotFoundException", "can't create FileOutputStream");  
	        }  
	        XmlSerializer serializer = Xml.newSerializer();  
	        try {  
	            serializer.setOutput(fileos,"UTF-8");  
	            serializer.startDocument(null, true);  
	            serializer.startTag(null, "books");  
	            for(int i = 0; i < 3; i ++){  
	                serializer.startTag(null, "book");  
	                serializer.startTag(null, "bookname");  
	                serializer.text("Android教程" + i);  
	                serializer.endTag(null, "bookname");  
	                serializer.startTag(null, "bookauthor");  
	                serializer.text("Frankie" + i);  
	                serializer.endTag(null, "bookauthor");  
	                serializer.endTag(null, "book");  
	            }  
	          
	            serializer.endTag(null, "books");  
	            serializer.endDocument();  
	            serializer.flush();  
	            fileos.close();  
	        } catch (Exception e) {  
	            Log.e("Exception","error occurred while creating xml file");  
	        }  
	        Toast.makeText(getApplicationContext(), "创建xml文件成功!", Toast.LENGTH_LONG).show();
		
	}
	
	
	 private void domParseXML(){  
	        File file = new File(Environment.getExternalStorageDirectory().getPath()+"/books.xml");  
	        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();        
	        DocumentBuilder db = null;    
	        try {  
	            db = dbf.newDocumentBuilder();  
	        } catch (ParserConfigurationException e) {  
	            e.printStackTrace();  
	            text.setText(e.toString());
	        }  
	        Document doc = null;  
	        try {
	            doc = db.parse(file);  
	        } catch (SAXException e) {  
	            e.printStackTrace();  
	            text.setText(e.toString());
	        } catch (IOException e) {  
	            e.printStackTrace();  
	            text.setText(e.toString());
	        }  
	        Element root = doc.getDocumentElement();  
	        NodeList books = root.getElementsByTagName("book");  
	        String res = "本结果是通过dom解析:" + "\n";  
	        for(int i = 0; i < books.getLength();i++){  
	            Element book = (Element)books.item(i);  
	            Element bookname = (Element)book.getElementsByTagName("bookname").item(0);  
	            Element bookauthor = (Element)book.getElementsByTagName("bookauthor").item(0);  
	            res += "书名: " + bookname.getFirstChild().getNodeValue() + " " +  
	                   "作者: " + bookauthor.getFirstChild().getNodeValue() + "\n";  
	        }  
	          
	        text.setText(res);  
	    }  
	 
	 
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}

xml文件的存储,打开

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值