Android中写入读取XML的简单实例示范.

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;

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.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xmlpull.v1.XmlSerializer;

import Android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.util.Xml;
import android.widget.TextView;

public class xml extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

Write(“test.xml”, writeXml());
 
  TextView tv = (TextView) findViewById(R.id.tv);
  tv.setText(ReadXML());
 }

private String writeXml() {
  XmlSerializer serializer = Xml.newSerializer();
  StringWriter writer = new StringWriter();
  try {
   serializer.setOutput(writer);

// <?xml version=”1.0″ encoding=”UTF-8″ standalone=”yes”?>
   serializer.startDocument(“UTF-8″, true);
   // <blog number=”1″>
   serializer.startTag(“”, “blog”);
   serializer.attribute(“”, “number”, String.valueOf(1));

// <message data=”2009-09-23″>
   serializer.startTag(“”, “message”);
   serializer.attribute(“”, “date”, “2009-09-23″);

// <title>Android XML</title>
   serializer.startTag(“”, “title”);
   serializer.text(“Android XML”);
   serializer.endTag(“”, “title”);

// <url>?p=508</url>
   serializer.startTag(“”, “url”);
   serializer.text(“”);
   serializer.endTag(“”, “url”);

// </message>
   serializer.endTag(“”, “message”);
   // </blog>
   serializer.endTag(“”, “blog”);
   serializer.endDocument();
   return writer.toString();
  } catch (Exception e) {
   throw new RuntimeException(e);
  }
 }

public boolean Write(String path, String txt) {
  try {
   OutputStream os = openFileOutput(path, MODE_PRIVATE);
   OutputStreamWriter osw = new OutputStreamWriter(os);
   osw.write(txt);
   osw.close();
   os.close();
  } catch (FileNotFoundException e) {
   return false;
  } catch (IOException e) {
   return false;
  }
  return true;
 }

private String ReadXML() {
  String re = “123″;
  DocumentBuilderFactory docBuilderFactory = null;
  DocumentBuilder docBuilder = null;
  Document doc = null;
  try {
   docBuilderFactory = DocumentBuilderFactory.newInstance();
   docBuilder = docBuilderFactory.newDocumentBuilder();
   // xml file 放到 assets目录中的
   doc = docBuilder.parse(this.openFileInput(“test.xml”));
   // root element
   Element root = doc.getDocumentElement();
   // Do something here
   // get a NodeList by tagname
   NodeList nodeList = root.getElementsByTagName(“blog”);
   Node nd = nodeList.item(0);
   re = nd.getFirstChild().getFirstChild().getNodeValue();
  // re = getAttributes().getNamedItem(“number”).getNodeValue();
  } catch (ParserConfigurationException e) {
   e.printStackTrace();
  } catch (SAXException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   doc = null;
   docBuilder = null;
   docBuilderFactory = null;
  }
  return re;
 }

}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwpzwz.html