Simple use of XML format data in Android

Source: Internet
Author: User
Tags xml attribute

Source:

 PackageCom.wangzhu.demo;Importjava.io.IOException;ImportJava.io.StringWriter;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.NodeList;Importorg.xml.sax.SAXException;ImportAndroid.content.Context;/*** XML, Extensible Markup Language (extensible Markup Language),<br/> * A subset of the standard generic markup language, a markup language for marking electronic files to make them structured. <br/> * It can be used to tag data, define data types, and is a source language that allows users to define their own markup language. <br/> * It is ideal for world Wide Web transport, providing a unified approach to describing and exchanging institutional data independent of the application or vendor. <br/> * Features:<br/> * XML is a markup language, much like html<br/> * XML is designed to transmit data rather than display data <br/> * XML tags are not predefined, You need to define your own tags <br/> * XML is designed to be self-descriptive <br/> * XML is the recommended standard for the:<br/> <br/> * * Compared to HTML * XML is not an alternative to HTML. <br/> * XML and HTML designed for different purposes:<br/> * XML is designed to transmit and store data with the focus on the content of the data. <br/> * HTML is designed to display data with the focus on the appearance of the data. <br/> * HTML is designed to display information, while XML is designed to transmit information. <br/> * * XML Syntax rules:<br/> * in XML, it is illegal to omit the close tag, and all elements must have a close tag. <br/> * XML tag is case sensitive <br/> * XML must be properly nested <br/> * XML document must have root element <br/> * XML attribute value must be quoted <br/> * The XML hollow lattice is retained <br/> * * Use the Android platform's own API to load the XML data and parse all the data according to the structure of the XML <br/> * * Using the Android platform's own API to create XML-compliant data, and output the XML data. <br/> * *@authorWangzhu * @date November 15, 2014 PM 11:38:10*/ Public classXmlutils {Privatecontext Context;  PublicXmlutils (Context context) { This. Context =context; }    /*** Read XML file*/     Public voidReadXML () {StringBuilder Accum=NewStringBuilder (); Try{documentbuilderfactory builderfactory=documentbuilderfactory. newinstance (); Documentbuilder Builder=Builderfactory.newdocumentbuilder (); Document Document=Builder.parse (Context.getassets (). Open ("Languages.xml")); //Get languages TagsElement element =document.getdocumentelement (); Accum.append ("cat="). Append (Element.getattribute ("Cat")). Append (","); NodeList List= Element.getelementsbytagname ("LAN");  for(inti = 0; I < list.getlength (); i++) {                //get each LAN labelElement LAN =(Element) List.item (i); Accum.append ("Id="). Append (Lan.getattribute ("id")). Append (","); Accum.append ("Ide="). Append (Lan.getelementsbytagname ("IDE"). Item (0). Gettextcontent ()). Append (","); Accum.append ("Name="). Append (Lan.getelementsbytagname ("Name"). Item (0). Gettextcontent ()). Append (";"); } System.err.println ("READXML:" +Accum); } Catch(parserconfigurationexception e) {e.printstacktrace (); } Catch(saxexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }    }    /*** Create an XML file*/     Public voidCreatexml () {Try{documentbuilderfactory builderfactory=documentbuilderfactory. newinstance (); Documentbuilder Builder=Builderfactory.newdocumentbuilder (); //Create a Document objectDocument document =builder.newdocument (); //creating a root elementElement languages = document.createelement ("Languages"); Languages.setattribute ("Cat", "it"); //create the first child element using a Document objectElement lan1 = document.createelement ("LAN"); Lan1.setattribute ("id", "1"); Element name1= Document.createelement ("name"); Name1.settextcontent ("Java"); //Adding child elementsLan1.appendchild (name1); Element Ide1= Document.createelement ("IDE"); Ide1.settextcontent ("Eclipse"); //Adding child elementsLan1.appendchild (IDE1); //adding to the root elementLanguages.appendchild (LAN1); //create the first child element using a Document objectElement lan2 = document.createelement ("LAN"); Lan2.setattribute ("id", "2"); Element name2= Document.createelement ("name"); Name2.settextcontent ("Swift"); //Adding child elementsLan2.appendchild (name2); Element Ide2= Document.createelement ("IDE"); Ide2.settextcontent ("Xcode"); //Adding child elementsLan2.appendchild (IDE2); //adding to the root elementLanguages.appendchild (lan2); //create the first child element using a Document objectElement lan3 = document.createelement ("LAN"); Lan3.setattribute ("id", "3"); Element Name3= Document.createelement ("name"); Name3.settextcontent ("C #"); //Adding child elementsLan3.appendchild (Name3); Element Ide3= Document.createelement ("IDE"); Ide3.settextcontent ("Visual Studio"); //Adding child elementsLan3.appendchild (IDE3); //adding to the root elementLanguages.appendchild (LAN3); //adding languages as a root element to a documentDocument.appendchild (languages); //ConversionsTransformerfactory transformerfactory =transformerfactory. newinstance (); Transformer Transformer=Transformerfactory.newtransformer (); Transformer.setoutputproperty (outputkeys.encoding,"Utf-8"); StringWriter SW=NewStringWriter (); Transformer. Transform (NewDomsource (document),NewStreamresult (SW)); System.err.println ("Createxml:" +sw.tostring ()); } Catch(parserconfigurationexception e) {e.printstacktrace (); } Catch(transformerconfigurationexception e) {e.printstacktrace (); } Catch(transformerexception e) {e.printstacktrace (); }    }}

XML file:

Simple use of XML format data in Android

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.