49. Save your phone's favorite information to a file

Source: Internet
Author: User

Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.OutputStreamWriter;Importjava.io.UnsupportedEncodingException;ImportJavax.xml.parsers.DocumentBuilder;Importjavax.xml.parsers.DocumentBuilderFactory;Importjavax.xml.parsers.ParserConfigurationException;ImportJavax.xml.transform.OutputKeys;ImportJavax.xml.transform.Transformer;Importjavax.xml.transform.TransformerConfigurationException;Importjavax.xml.transform.TransformerException;Importjavax.xml.transform.TransformerFactory;ImportJavax.xml.transform.dom.DOMSource;ImportJavax.xml.transform.stream.StreamResult;Importorg.w3c.dom.Document;Importorg.w3c.dom.Element;ImportOrg.w3c.dom.Node;Importorg.w3c.dom.NodeList;Importorg.xml.sax.SAXException; Public classParsexmldemo {PrivateDocument document=NULL;  Public Static voidMain (string[] args) {parsexmldemo pd=NewParsexmldemo ();        Pd.getdocument (); Pd.showinfo ();//Pd.add ();//pd.update ();//pd.savaxml ("New.xml");//Pd.delete ();    }         Public voidgetdocument () {Documentbuilderfactory factory=documentbuilderfactory.newinstance (); Try{Documentbuilder Builder=Factory.newdocumentbuilder (); Document=builder.parse ("collection information. Xml"); } Catch(parserconfigurationexception e) {e.printstacktrace (); } Catch(saxexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }    }            //get phone branding and properties     Public voidShowinfo () {NodeList brands=document.getelementsbytagname ("Brand");  for(intI=0;i<brands.getlength (); i++) {node node=Brands.item (i); Element Elebrand=(Element) node; System.out.println (Elebrand.getattribute ("Name")); NodeList Types=elebrand.getchildnodes ();  for(intJ=0;j<types.getlength (); j + +) {Node Typenode=Types.item (j); if(Typenode.getnodetype () = =Node.element_node) {Element Eletype=(Element) Typenode; System.out.println (Eletype.getattribute ("Name")); }            }        }    }        //Save the XML file     Public voidsavaxml (String path) {Transformerfactory Factory=transformerfactory.newinstance (); Factory.setattribute ("Indent-number", "4"); Try{Transformer Transformer=Factory.newtransformer (); Transformer.setoutputproperty (outputkeys.encoding,"GB2312"); Transformer.setoutputproperty (Outputkeys.indent,"Yes");//streamresult result=new Streamresult (new FileOutputStream (path));Streamresult result=NewStreamresult (NewOutputStreamWriter (NewFileOutputStream (Path), "gb2312")); Domsource Source=NewDomsource (document);        Transformer.transform (source, result); } Catch(transformerconfigurationexception e) {e.printstacktrace (); } Catch(transformerexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(unsupportedencodingexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }        //Add     Public voidAdd () {element element=document.createelement ("Brand"); Element.setattribute ("Name", "Samsung"); Element Ele1=document.createelement ("Type"); Ele1.setattribute ("Name", "Note3");        Element.appendchild (ELE1); document.getElementsByTagName ("Phoneinfo"). Item (0). appendchild (Element);  This. Savaxml ("New.xml"); }        //Modify     Public voidUpdate () {NodeList brands=document.getelementsbytagname ("Brand");  for(intI=0;i<brands.getlength (); i++) {Node brand=Brands.item (i); Element Elebrand=(Element) brand; Elebrand.setattribute ("id", i+ ""); }         This. Savaxml ("New.xml"); }        //Delete     Public voidDelete () {NodeList brands=document.getelementsbytagname ("Brand");  for(intI=0;i<brands.getlength (); i++) {Node brand=Brands.item (i); Element Elebrand=(Element) brand; if(Elebrand.getattribute ("name"). Equals ("Huawei")) {Elebrand.getparentnode (). removechild (Elebrand); }        }         This. Savaxml ("New.xml"); }}

Here's news.xml.

<?XML version= "1.0" encoding= "GB2312" standalone= "no"?><Phoneinfo>        <Brandname= "Apple">        <Typename= "IPhone4"/>    </Brand></Phoneinfo>

Here is the collection information. xml

<?XML version= "1.0" encoding= "GB2312"?><Phoneinfo>    <Brandname= "Huawei">        <Typename= "U8650"/>        <Typename= "HW123"/>        <Typename= "HW321"/>    </Brand>    <Brandname= "Apple">        <Typename= "IPhone4"/>    </Brand></Phoneinfo>

49. Save your phone's favorite information to a 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.