android中dom方式創(chuàng)建xml的示例-創(chuàng)新互聯(lián)

這篇文章給大家分享的是有關(guān)android中dom方式創(chuàng)建xml的示例的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

創(chuàng)新互聯(lián)建站企業(yè)建站,十多年網(wǎng)站建設(shè)經(jīng)驗,專注于網(wǎng)站建設(shè)技術(shù),精于網(wǎng)頁設(shè)計,有多年建站和網(wǎng)站代運營經(jīng)驗,設(shè)計師為客戶打造網(wǎng)絡(luò)企業(yè)風(fēng)格,提供周到的建站售前咨詢和貼心的售后服務(wù)。對于成都網(wǎng)站建設(shè)、網(wǎng)站制作中不同領(lǐng)域進行深入了解和探索,創(chuàng)新互聯(lián)在網(wǎng)站建設(shè)中充分了解客戶行業(yè)的需求,以靈動的思維在網(wǎng)頁中充分展現(xiàn),通過對客戶行業(yè)精準市場調(diào)研,為客戶提供的解決方案。

android中dom創(chuàng)建xml

首先:創(chuàng)建的文件會放在/data/data/cn.com.xxx(當(dāng)前包名)/files下面。

創(chuàng)建生成的xml文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<classes>
	<group name="一年級" num="10">
		<person name="小明" age="7">
			<chinese>語文90</chinese>
			<english>英語80</english>
		</person>
	</group>
</classes>

可以直接用android dom 解析xml方式文章中方法去解析,注意修改一點:

// 從assets文件夾下獲取文件 轉(zhuǎn)換成輸入流
//			inStream = this.getResources().getAssets().open(fileName);
//			doc = docBuilder.parse(inStream);
			InputStream fosStream = openFileInput(fileName);
			doc = docBuilder.parse(fosStream);

同時,fileName取得方法:

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

解析出來的結(jié)果是

android中dom方式創(chuàng)建xml的示例
以下是創(chuàng)建xml文件的代碼:

private void createXmlFile(){
		
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document doc  = builder.newDocument();
			//創(chuàng)建xml根元素
			Element rootEle = doc.createElement("classes");
			doc.appendChild(rootEle);
			//創(chuàng)建xml二級元素
			Element groupEle = doc.createElement("group");
			groupEle.setAttribute("name", "一年級");
			groupEle.setAttribute("num", "10");
			//創(chuàng)建xml person元素
			Element personEle = doc.createElement("person");
			//personEle 的屬性和屬性值
			personEle.setAttribute("name", "小明");
			personEle.setAttribute("age", "7");
			//創(chuàng)建personELe的子元素
			Element chinese = doc.createElement("chinese");
			//創(chuàng)建personELe的子元素的值
			chinese.appendChild(doc.createTextNode("語文90"));
			personEle.appendChild(chinese);
			Element english = doc.createElement("english");
			english.appendChild(doc.createTextNode("英語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");
			//創(chuàng)建文件存放在 /data/data/cn.xxx.xxx(當(dāng)前包)/files 
			FileOutputStream fos = openFileOutput("Dom.xml", Context.MODE_PRIVATE);
			//創(chuàng)建文件存放在 /data/data/cn.xxx.xxx(當(dāng)前包)/cache
//			FileOutputStream fos = Op
			PrintWriter pw = new PrintWriter(fos);
			StreamResult result = new StreamResult(pw);
			transformer.transform(source, result);
			
			System.out.println("生成XML文件成功!");
		} 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());
		}
		
	}

感謝各位的閱讀!關(guān)于“android中dom方式創(chuàng)建xml的示例”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

網(wǎng)站標題:android中dom方式創(chuàng)建xml的示例-創(chuàng)新互聯(lián)
網(wǎng)站鏈接:http://bm7419.com/article24/igpce.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、營銷型網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化、靜態(tài)網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)、域名注冊

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

手機網(wǎng)站建設(shè)