Android uses DOM to generate and output XML format data

Source: Internet
Author: User

Android uses DOM to generate and output XML format data
Android uses DOM to generate and output XML format data

This document describes how to use DOM to generate and output XML data.

1. Generate and output XML data

The code and comments are as follows:

Try {DocumentBuilderFactory factory = DocumentBuilderFactory. newInstance (); DocumentBuilder builder = factory. newDocumentBuilder (); // create a new Document object, instead of obtaining Document xmlDocument = builder. newDocument (); // create the root node and add the attribute value Element root = xmlDocument. createElement ("Ages"); root. setAttribute ("foo", "bar");/*** create the first element node * // create the corresponding element node, and add the property value and text content Element lan_1 = xmlDocument. createElement ("lan"); lan_1.setAttribute ("id", "1"); Element name_1 = xmlDocument. createElement ("name"); name_1.setTextContent ("Java"); Element ide_1 = xmlDocument. createElement ("ide"); ide_1.setTextContent ("Eclipse"); // Add the name tag and ide tag to the lan tag lan_1.appendChild (name_1); lan_1.appendChild (ide_1 ); // Add the lan label to the ages label root. appendChild (lan_1);/*** create the second Element node * // create the corresponding Element node and add the attribute value and text content Element lan_2 = xmlDocument. createElement ("lan"); lan_2.setAttribute ("id", "2"); Element name_2 = xmlDocument. createElement ("name"); name_2.setTextContent ("Swift"); Element ide_2 = xmlDocument. createElement ("ide"); ide_2.setTextContent ("XCode"); // Add the name tag and ide tag to the lan tag lan_2.appendChild (name_2); lan_2.appendChild (ide_2 ); // Add the lan label to the ages label root. appendChild (lan_2); // Add the root node to xmlDocument in the Document Object. appendChild (root); // The XML data output needs to be converted, using Transformer TransformerFactory transformerFactory = TransformerFactory. newInstance (); Transformer transformer = transformerFactory. newTransformer (); // sets the output attribute transformer. setOutputProperty ("encoding", "UTF-8"); // create a new character output stream for output data StringWriter stringWriter = new StringWriter (); // convert the XML Document Object and output it to transformer in the output stream. transform (new DOMSource (xmlDocument), new StreamResult (stringWriter); Log. I ("XMLDATA", stringWriter. toString ();} catch (ParserConfigurationException e) {e. printStackTrace ();} catch (TransformerConfigurationException e) {e. printStackTrace ();} catch (TransformerException e) {e. printStackTrace ();}

Related Article

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.