In the article on xml parsing by androiddom, we briefly introduce the xml parsing application by dom. Based on the original article, let's talk about the xml application created by dom in Android. In
Android dom xml Parsing methodThis article briefly introduces the dom-based xml parsing 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
Can be used directlyAndroid dom xml Parsing methodThe method in the article is parsed. 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
The following code creates an xml file:
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. setAttribute ("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"); english. appendChild (doc. create TextNode ("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"); // The created file is stored in/data/cn. xxx. xxx (when Pre-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 = OpPrintWriter 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 )!