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 )!