Create an xml file using androidsax

Source: Internet
Author: User
The first two articles briefly describe the method for parsing xml by using sax to implement the parsing function. The following uses sax to create an xml file. The first two articles briefly describe the method for parsing xml by using sax to implement the parsing function. The following uses sax to create an xml file.

The specific method of using sax to create xml is to add relevant comments to the program, or directly add the code.

Package cn.com. sax; import java. io. outputStream; import java. io. stringWriter; import javax. xml. transform. outputKeys; import javax. xml. transform. result; import javax. xml. transform. transformer; import javax. xml. transform. transformerConfigurationException; import javax. xml. transform. sax. SAXTransformerFactory; import javax. xml. transform. sax. transformerHandler; import javax. xml. transform. stream. streamRe Sult; import org. xml. sax. SAXException; import org. xml. sax. helpers. attributesImpl; import android. util. log; class SxaCreateXml {/*** generate an XML *** @ param list * @ return */public String saxToXml (OutputStream output) {String xmlStr = null; try {// used to generate an XML file // The Object implementing this interface contains the information required to build the conversion Result tree Result resultXml = new StreamResult (output ); // It is used to obtain the XML string format // A response stream, which can be used to collect the output in the string buffer to construct the string StringWriter writer Str = new StringWriter (); // information required to build the conversion result tree. Result resultStr = new StreamResult (writerStr); // Create a SAX conversion factory SAXTransformerFactory sff = (SAXTransformerFactory) SAXTransformerFactory. newInstance (); // The conversion processor that listens to the SAX ContentHandler // parses events and converts them to the Result tree Result TransformerHandler th = sff. newTransformerHandler (); // Convert the source tree to the result tree Transformer transformer = th. getTransformer (); // sets the character encoding transformer. setOutputProperty (OutputKeys. ENCODING, "UTF-8"); // whether to indent transformer. setOutputProperty (OutputKeys. INDENT, "yes"); // sets the Result associated with the TransformerHandler used for conversion. // Note: The two th. setResult cannot be enabled at the same time. setResult (resultXml); // th. setResult (resultStr); // create the root element
 
  
And set its property to null th. startDocument (); AttributesImpl attr = new AttributesImpl (); th. startElement ("", "calsses", "calsses", attr); // creates a level-1 subelement.
  
   
And set its attributes to attr. clear (); attr. addAttribute ("", "name", "name", "", ""); attr. addAttribute ("", "num", "num", "", "10"); th. startElement ("", "", "group", attr); // creates a level-2 subelement.
   
    
And set its attributes to attr. clear (); attr. addAttribute ("", "name", "name", "", "James"); attr. addAttribute ("", "age", "age", "", "7"); th. startElement ("", "", "person", attr); // create a third-level sub-element
    
     
And set its value to attr. clear (); th. startElement ("", "", "chinese", attr); th. characters ("90 90 ". toCharArray (), 0, "90 ". length (); th. endElement ("", "", "chinese"); // creates a level 3 subelement.
     
      
And set its value th. startElement ("", "", "english", attr); th. characters ("English 80 ". toCharArray (), 0, "English 80 ". length (); th. endElement ("", "", "english"); th. endElement ("", "", "person"); th. endElement ("", "", "group"); th. endElement ("", "calsses", "calsses"); th. endDocument (); xmlStr = writerStr. getBuffer (). toString ();} catch (TransformerConfigurationException e) {Log. e ("TEST", "" + e. toString ();} catch (SAXException e) {Log. e ("TEST", "" + e. toString ();} catch (Exception e) {Log. e ("TEST", "" + e. toString ();} Log. e ("TEST", "generated" + xmlStr); return xmlStr ;}}
     
    
   
  
 

You can call this method to create an xml file.

new SxaCreateXml().saxToXml(openFileOutput("sax.xml",  Context.MODE_PRIVATE));

The generated sax. xml file is still in the data/cn. xxx. xxx/files folder.

 
   
    
     
    
     
Language 90
      
    
     
English 80
      
     
    
 

The above is the content of the xml file created by android sax. 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.