android dom 解析xml方式文章中,简单介绍了dom解析xml的应用。今天在原文章的基础上,说一下android中dom创建xml的应用。

首先:创建的文件会放在/data/data/cn.com.xxx(当前包名)/files下面。

创建生成的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文件夹下获取文件 转换成输入流
//			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];

解析出来的结果是



以下是创建xml文件的代码:

private void createXmlFile(){
		
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document doc  = builder.newDocument();
			//创建xml根元素
			Element rootEle = doc.createElement("classes");
			doc.appendChild(rootEle);
			//创建xml二级元素
			Element groupEle = doc.createElement("group");
			groupEle.setAttribute("name", "一年级");
			groupEle.setAttribute("num", "10");
			//创建xml person元素
			Element personEle = doc.createElement("person");
			//personEle 的属性和属性值
			personEle.setAttribute("name", "小明");
			personEle.setAttribute("age", "7");
			//创建personELe的子元素
			Element chinese = doc.createElement("chinese");
			//创建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");
			//创建文件存放在 /data/data/cn.xxx.xxx(当前包)/files 
			FileOutputStream fos = openFileOutput("Dom.xml", Context.MODE_PRIVATE);
			//创建文件存放在 /data/data/cn.xxx.xxx(当前包)/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());
		}
		
	}

以上就是android dom方式创建xml的内容,更多相关内容请关注PHP中文网(www.php.cn)!




声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

  • 相关标签:android dom xml
  • 程序员必备接口测试调试工具:点击使用

    Apipost = Postman + Swagger + Mock + Jmeter

    Api设计、调试、文档、自动化测试工具

    网页生成APP,用做网站的技术去做APP:立即创建

    手机网站开发APP、自助封装APP、200+原生模块、2000+映射JS接口按需打包

    • 上一篇:android dom 解析xml方式
    • 下一篇:android sax解析xml文件(二)

    相关文章

    相关视频


    • 使用xmlhttp为网站增加域名查询功能的示例代码...
    • 四种XML解析方式详解
    • 基于PHP对XML的操作详解
    • XML和Tomcat的入门知识的详细介绍
    • android dom方式创建xml
    • XML DOM
    • PHP开发基础教程之PHP XML DOM
    • PHP 新手入门之XML与DOM
    • PHP XML DOM
    • PHP XML DOM

    视频教程分类

    • php视频教程
    • html视频教程
    • css视频教程
    • JS视频教程
    • jQuery视频教程
    • mysql视频教程
    • Linux视频教程
    • Python视频教程
    • Laravel视频教程
    • Vue视频教程

    专题

    android dom方式创建xml