Create xml in androiddom mode

Source: Internet
Author: User
First, the created file will be placed under datadatacn.com. xxx (current package name) files. In the android dom parsing xml method article, we briefly introduce the dom parsing xml application. Based on the original article, let's talk about the xml application created by dom in Android.

First, the created file will be placed under/data/cn.com. xxx (current package name)/files.

The generated xml file is as follows:

   
       
            
                 
    
     
Language 90
                  
    
     
English 80
              
         
    
 

You can use the android dom to parse the xml method in the document. Note the following:

// Obtain the file from the assets folder and convert it to the input stream // inStream = this. getResources (). getAssets (). open (fileName); // doc = docBuilder. parse (inStream); InputStream fosStream = openFileInput (fileName); doc = docBuilder. parse (fosStream );


At the same time, the method for obtaining fileName is as follows:

String[] fileNames = getFilesDir().list();
String fileName = fileNames[0];


The parsed result is

Private void createXmlFile () {try {DocumentBuilderFactory factory = DocumentBuilderFactory. newInstance (); DocumentBuilder builder = factory. newDocumentBuilder (); Document doc = builder. newDocument (); // create the xml root Element rootEle = doc. createElement ("classes"); doc. appendChild (rootEle); // create the xml second-level Element groupEle = doc. createElement ("group"); groupEle. setAttribute ("name", "first year"); groupEle. setAttri Partition ("num", "10"); // create the xml person Element personEle = doc. createElement ("person"); // attributes and attribute values of personEle: personEle. setAttribute ("name", "James"); personEle. setAttribute ("age", "7"); // create a sub-Element of personELe. Element chinese = doc. createElement ("chinese"); // The value of the child element that creates personELe is chinese. appendChild (doc. createTextNode ("90 90"); personEle. appendChild (chinese); Element english = doc. createElement ("english"); englis H. appendChild (doc. createTextNode ("English 80"); personEle. appendChild (english); groupEle. appendChild (personEle); rootEle. appendChild (groupEle); TransformerFactory tf = TransformerFactory. newInstance (); Transformer transformer = tf. newTransformer (); DOMSource source = new DOMSource (doc); transformer. setOutputProperty (OutputKeys. ENCODING, "UTF-8"); transformer. setOutputProperty (OutputKeys. INDENT, "no "); // Create a file and store it in/data/cn. xxx. xxx (current package)/files FileOutputStream fos = openFileOutput ("Dom. xml ", Context. MODE_PRIVATE); // create a file and store it in/data/cn. xxx. xxx (current package)/cache // FileOutputStream fos = Op PrintWriter pw = new PrintWriter (fos); StreamResult result = new StreamResult (pw); transformer. transform (source, result); System. out. println ("XML file generated successfully! ");} Catch (ParserConfigurationException e) {System. out. println (e. getMessage ();} catch (TransformerConfigurationException e) {System. out. println (e. getMessage ();} catch (TransformerException e) {System. out. println (e. getMessage ();} catch (FileNotFoundException e) {System. out. println (e. getMessage ());}}

The above is the content for creating xml in android dom mode. For more information, see PHP Chinese website (www.php1.cn )!

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.