【Android】创建、读取XML文件

创建:

package webdomain;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlSerializer; import form.FrmLogin;
import form.FrmShintai; import android.os.Environment;
import android.util.Log;
import android.util.Xml;
import android.widget.ArrayAdapter; public class Java2XML { public static void XmlFileCreator(String abc){
File newxmlfile = new File(FrmLogin.SDCARD_PATH+"***");
try{
if(!newxmlfile.exists())
newxmlfile.createNewFile();
}catch(IOException e){
Log.e("IOException", "exception in createNewFile() method");
}
//bind the new file with a FileOutputStream
FileOutputStream fileos = null;
try{
fileos = new FileOutputStream(newxmlfile);
}catch(FileNotFoundException e){
Log.e("FileNotFoundException", "can't create FileOutputStream");
}
// create a XmlSerializer in order to write xml data
XmlSerializer serializer = Xml.newSerializer();
try {
//we set the FileOutputStream as output for the serializer, using UTF-8 encoding
serializer.setOutput(fileos, "UTF-8");
//Write <?xml declaration with encoding (if encoding not null) and standalone flag (if standalone not null)
serializer.startDocument(null, null);
//set indentation option
serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); serializer.startTag(null, "jokes");
serializer.startTag(null, "tag");
serializer.startTag(null, "123");
serializer.text(FrmS.G_Itmho.getText().toString());
serializer.endTag(null, "123"); serializer.startTag(null, "456");
serializer.text(FrmS.G_Itmu.getText().toString());
serializer.endTag(null, "456"); serializer.startTag(null, "789");
serializer.text(FrmSai.G_Itm.getText().toString());
serializer.endTag(null, "789");
serializer.endTag(null, "tag");
serializer.endTag(null, "jokes"); serializer.endDocument();
serializer.flush();
fileos.close();
} catch (Exception e) {
Log.e("Exception","error occurred while creating xml file");
}
} }

读取:

package xmldomain;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; import org.kobjects.util.Strings;
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 android.R.string; import form.FrmLogin;
import form.FrmShintai; public class ReadXML {
public static void ReadXMLs()
{
File newxmlfile = new File(FrmLogin.SDCARD_PATH+"filepath");
FileInputStream fileos = null;
try {
fileos = new FileInputStream(newxmlfile);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
DocumentBuilderFactory docBuilderFactory = null;
DocumentBuilder docBuilder = null;
Document doc = null;
try{
docBuilderFactory =DocumentBuilderFactory.newInstance();
docBuilder =docBuilderFactory.newDocumentBuilder();
//xml file
doc = docBuilder.parse(fileos);
//root element
Element root = doc.getDocumentElement();
//Do something here
//Get a NodeList by tagname
NodeList shenchang = root.getElementsByTagName_r("123");
Node nd = shenchang.item(0);
FrmShintai.G_ItmZenkaiShincho.setText(nd.getTextContent());
//Read Node
NodeList tizhong = root.getElementsByTagName_r("456");
Node nd1 = tizhong.item(0);
FrmShintai.G_ItmZenkaiTaiju.setText(nd1.getTextContent());
//Read Node
NodeList bmi = root.getElementsByTagName_r("789");
Node nd2 = bmi.item(0);
FrmShintai.G_ItemZenkaiBMI.setText(nd2.getTextContent());
//Read Node2
}catch(IOException e){
}catch(SAXException e){
}catch(ParserConfigurationException e){
}finally{
doc =null;
docBuilder=null;
docBuilderFactory=null;
}
}
}
上一篇:浅谈Java的开放封闭原则


下一篇:33.Odoo产品分析 (四) – 工具板块(4) – 问题追踪及群发邮件营销(1)