android上dom读写sdcard上的xml

向手机的sdcard读写xml文件肯定要用到系统设备,而在android平台上,只要用到系统设备都需要授权,所以首先要在androidManifest.xml中进行授权。加入如下权限

	<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
向sdcard写入xml时,首先要判断sdcard是否存在,其次要判断目录是否存在,java代码如下:

//判断sdcard是否为可以读写状态
			if (!Environment.MEDIA_MOUNTED.equals(Environment
					.getExternalStorageState())) {
				Toast.makeText(MainActivity.this, "sdcard not exist",
						Toast.LENGTH_LONG).show();
				return;
			}
			File file = new File(Environment.getExternalStorageDirectory()
					+ File.separator + MainActivity.this.DIR + File.separator
					+ MainActivity.this.FILE_NAME);
			//判读路径是否存在,不存在创建
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdir();
			}
用dom写xml文件可以分为如下6步:

//1,创建DocumentBuilderFactory
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();
			//2,创建DocumentBuilder
			DocumentBuilder builder = null;
			try {
				builder = factory.newDocumentBuilder();
			} catch (ParserConfigurationException e) {
				e.printStackTrace();
			}
			Document doc = null;
			//3,创建document
			doc = builder.newDocument();
			//4,创建相关节点
			Element rootElement = doc.createElement("connect-info");
			Element peopleElement = doc.createElement("people");
			Element nameElement = doc.createElement("name");
			Element addressElement = doc.createElement("address");
			nameElement.appendChild(doc.createTextNode(MainActivity.this.name
					.getText().toString()));
			addressElement.appendChild(doc
					.createTextNode(MainActivity.this.address.getText()
							.toString()));
			//5,组装相关节点间的父子关系
			doc.appendChild(rootElement);
			rootElement.appendChild(peopleElement);
			peopleElement.appendChild(nameElement);
			peopleElement.appendChild(addressElement);
			//6,创建TransformerFactory,transformer,执行transform方法
			TransformerFactory tf = TransformerFactory.newInstance();
			Transformer transformer = null;
			try {
				transformer = tf.newTransformer();
			} catch (TransformerConfigurationException e) {
				e.printStackTrace();
			}
			DOMSource domSource = new DOMSource(doc);
			StreamResult streamResult = new StreamResult(file);
			transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
			try {
				transformer.transform(domSource, streamResult);
			} catch (TransformerException e) {
				e.printStackTrace();
			}


当然从sdcard读取xml和向sdcard写入xml类似,也要确保sdcard为可读写状态,并且要读取的文件存在。

if (!Environment.MEDIA_MOUNTED.equals(Environment
					.getExternalStorageState())) {
				Toast.makeText(MainActivity.this, "sdcard not exist",
						Toast.LENGTH_LONG);
				return;
			}
			File file = new File(Environment.getExternalStorageDirectory()
					+ File.separator + MainActivity.this.DIR + File.separator
					+ MainActivity.this.FILE_NAME);
			if (!file.exists()) {
				return;
			}

dom读取xml可以分为如下4步:

//1,创建DocumentBuilderFactory
			DocumentBuilderFactory factory = null;
			factory = DocumentBuilderFactory.newInstance();
			//2,创建DocumentBuilder
			DocumentBuilder builder = null;
			try {
				builder = factory.newDocumentBuilder();
			} catch (ParserConfigurationException e) {
				e.printStackTrace();
			}
			//3,获得要解析的document
			Document doc = null;
			try {
				doc = builder.parse(file);
			} catch (SAXException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			LayoutInflater layoutInflater = LayoutInflater
					.from(MainActivity.this);
			popuView = (View) layoutInflater.inflate(R.layout.popu_win, null);
			TextView nameValue = (TextView) popuView
					.findViewById(R.id.nameValue);
			TextView addressValue = (TextView) popuView
					.findViewById(R.id.addressValue);
			popupWin = new PopupWindow(popuView, 200, 300);
			Button OK = (Button) popuView.findViewById(R.id.sure);
			OK.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					popupWin.dismiss();
				}
			});
			popupWin.showAtLocation(MainActivity.this.save, Gravity.BOTTOM, 0,
					0);
			//4,取得NodeList循环遍历得到下面的节点和内容
			NodeList nodeList = doc.getElementsByTagName("people");
			for (int i = 0; i < nodeList.getLength(); i++) {
				Element element = (Element) nodeList.item(i);
				nameValue.setText(element.getElementsByTagName("name").item(0)
						.getFirstChild().getNodeValue());
				addressValue.setText(element.getElementsByTagName("address")
						.item(0).getFirstChild().getNodeValue());
			}

本来有效果图片的,我用onenote截图直接复制粘贴,发表前有图片,发表后就没有图片,上传图片还要我图片另存为命令,麻烦死就不上传图片了。csdn就这一点上传图片太麻烦了。



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值