"Android" Create, read XML file

Source: Internet
Author: User

Create:

 PackageWebdomain;ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.OutputStream;ImportJava.io.OutputStreamWriter;ImportJava.io.StringWriter;ImportJava.text.SimpleDateFormat;Importjava.util.ArrayList;Importjava.util.List;ImportJavax.xml.parsers.DocumentBuilder;Importjavax.xml.parsers.DocumentBuilderFactory;Importorg.w3c.dom.Document;Importorg.w3c.dom.Element;ImportOrg.w3c.dom.Node;Importorg.w3c.dom.NodeList;Importorg.xmlpull.v1.XmlPullParserFactory;ImportOrg.xmlpull.v1.XmlSerializer;Importform. Frmlogin;Importform. Frmshintai;Importandroid.os.Environment;ImportAndroid.util.Log;Importandroid.util.Xml;ImportAndroid.widget.ArrayAdapter; Public classJava2xml { Public Static voidXmlfilecreator (String ABC) {File newxmlfile=NewFile (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 FileOutputStreamFileOutputStream Fileos =NULL; Try{Fileos=NewFileOutputStream (Newxmlfile); }Catch(FileNotFoundException e) {LOG.E ("FileNotFoundException", "Can ' t create FileOutputStream"); }        //Create a XmlSerializer in order to write XML dataXmlSerializer serializer =Xml.newserializer (); Try {            //we set the FileOutputStream as output for the serializer, using UTF-8 encodingSerializer.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 optionSerializer.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"); }    }}

Read:

 PackageXmldomain;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStream;Importjava.util.ArrayList;Importjava.util.List;ImportJavax.xml.parsers.DocumentBuilder;Importjavax.xml.parsers.DocumentBuilderFactory;Importjavax.xml.parsers.ParserConfigurationException;Importorg.kobjects.util.Strings;Importorg.w3c.dom.Document;Importorg.w3c.dom.Element;ImportOrg.w3c.dom.Node;Importorg.w3c.dom.NodeList;Importorg.xml.sax.SAXException;ImportAndroid. r.string;Importform. Frmlogin;Importform. Frmshintai; Public classReadXML { Public Static  voidreadxmls () {File newxmlfile=NewFile (frmlogin.sdcard_path+ "filepath"); FileInputStream Fileos=NULL;Try{Fileos=NewFileInputStream (newxmlfile);} Catch(FileNotFoundException E1) {//TODO auto-generated Catch blocke1.printstacktrace ();} Documentbuilderfactory docbuilderfactory=NULL;D Ocumentbuilder Docbuilder=NULL;D ocument Doc=NULL;Try{docbuilderfactory=documentbuilderfactory.newinstance ();d Ocbuilder=Docbuilderfactory.newdocumentbuilder ();//XML FileDoc =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;d Ocbuilder=NULL;d ocbuilderfactory=NULL;}}}

"Android" Create, read XML file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.