【Android】建立、讀取XML檔案

來源:互聯網
上載者:User

標籤:

建立:

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 blocke1.printStackTrace();}DocumentBuilderFactory docBuilderFactory = null;DocumentBuilder docBuilder = null;Document doc = null;try{docBuilderFactory =DocumentBuilderFactory.newInstance();docBuilder =docBuilderFactory.newDocumentBuilder();//xml file doc = docBuilder.parse(fileos);//root elementElement root = doc.getDocumentElement();//Do something here//Get a NodeList by tagnameNodeList shenchang = root.getElementsByTagName_r("123");Node nd  = shenchang.item(0);FrmShintai.G_ItmZenkaiShincho.setText(nd.getTextContent());//Read NodeNodeList tizhong = root.getElementsByTagName_r("456");Node nd1  = tizhong.item(0);FrmShintai.G_ItmZenkaiTaiju.setText(nd1.getTextContent());//Read NodeNodeList 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;}}}

 

【Android】建立、讀取XML檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.