android xml设置位置,android 自定义xml存储方式以及存储位置

讲一下android 自定义xml存储方式以及存储位置。首先要说明一下,要区分android sharedpreferences存储。因为一般情况sharedpreferences存储也是成为xml存储,因为存储方式是以xml形式存在的。那么先看看我说的这种存储形式。

本文代码:点击

1、存储形式

先看一张图片。这个就是存储的形式。

可以看出开头还是很标准的。然后是custom这个标签是我自定义的。紧跟着是name也是我自定义标签,存的内容是cyuvvuh,就是值啦。

2、看看代码的实现

package com.example.xmlsavedemo;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.transform.Transformer;

import javax.xml.transform.TransformerConfigurationException;

import javax.xml.transform.TransformerException;

import javax.xml.transform.TransformerFactory;

import javax.xml.transform.TransformerFactoryConfigurationError;

import javax.xml.transform.dom.DOMSource;

import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;

import org.w3c.dom.NodeList;

import org.w3c.dom.Text;

import org.xml.sax.SAXException;

/*

* 自定义xml存储

*/

public class XmlSaveUtil {

private static XmlSaveUtil mXmlSaveUtil = null;

private String mstrConfigPath;

public static XmlSaveUtil GetInstance() {

if (mXmlSaveUtil == null) {

mXmlSaveUtil = new XmlSaveUtil();

}

return mXmlSaveUtil;

}

//设置保存位置

public void setConfigPath(String strPath) {

mstrConfigPath = strPath;

}

//写如xml

public boolean writeXML(String strContent ,String strFileName)

{

File pathFile = new File(mstrConfigPath);

if (mstrConfigPath.length() <= 0 || !pathFile.exists()) {

return false;

}

DocumentBuilderFactory dbfFactory = DocumentBuilderFactory.newInstance();

DocumentBuilder dbBuilder = null;

try {

dbBuilder = dbfFactory.newDocumentBuilder();

} catch (Exception e) {

return false;

}

Document doc = dbBuilder.newDocument();

//创建根元素“Custom”

org.w3c.dom.Element root = doc.createElement("Custom");

doc.appendChild(root);

// 名称信息

org.w3c.dom.Element configNode = doc.createElement("Name");

root.appendChild(configNode);

Text configValue = doc.createTextNode(String.format("%s", strContent));

configNode.appendChild(configValue);

//将xml写成文件

Transformer trans;

try {

trans = TransformerFactory.newInstance().newTransformer();

trans.setOutputProperty("indent", "yes");

trans.transform(new DOMSource(doc), new StreamResult(new FileOutputStream(mstrConfigPath + "/" + strFileName)));

return true;

} catch (TransformerConfigurationException e) {

e.printStackTrace();

} catch (TransformerFactoryConfigurationError e) {

e.printStackTrace();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (TransformerException e) {

e.printStackTrace();

}

return false;

}

//读取xml

public String readXML(String strFileName)

{

File pathFile = new File(mstrConfigPath);

if (!pathFile.exists()) {

return "";

}

String strName = "";

DocumentBuilderFactory dbfFactory = DocumentBuilderFactory.newInstance();

DocumentBuilder dbBuilder = null;

try {

dbBuilder = dbfFactory.newDocumentBuilder();

} catch (Exception e) {

return "";

}

File inFile = new File(mstrConfigPath, strFileName);

Document doc = null;

try {

inFile.createNewFile();

doc = dbBuilder.parse(inFile);

} catch (SAXException e) {

e.printStackTrace();

return "";

} catch (IOException e) {

e.printStackTrace();

return "";

}

org.w3c.dom.Element root = doc.getDocumentElement();

//获取内容

NodeList configNodes = root.getElementsByTagName("Name");

Text configValue = null;

if (configNodes.getLength() == 1) {

configValue = (Text) configNodes.item(0).getFirstChild();

strName = configValue.getNodeValue();

}

return strName;

}

}

3、怎么使用这个工具类

package com.example.xmlsavedemo;

import java.io.File;

import android.os.Bundle;

import android.os.Environment;

import android.app.Activity;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.EditText;

import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener{

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

//获取数据存储路径

String strSDpath = Environment.getExternalStorageDirectory().getPath();

//存放目录

File subFile = new File(strSDpath, "xml");

if (!subFile.exists()) {

subFile.mkdir();

}

//设置存储路径

XmlSaveUtil.GetInstance().setConfigPath(subFile.toString());

initView();

}

private void initView() {

findViewById(R.id.button1).setOnClickListener(this);

findViewById(R.id.button2).setOnClickListener(this);

}

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.button1:

EditText eText = (EditText)findViewById(R.id.editText1);

String strContent = eText.getText().toString().trim();

XmlSaveUtil.GetInstance().writeXML(strContent, "data.xml");

break;

case R.id.button2:

TextView tView = (TextView)findViewById(R.id.textView2);

String string = XmlSaveUtil.GetInstance().readXML("data.xml");

tView.setText(string);

break;

default:

break;

}

}

}

其实用这种方式存储,要区别于sharedpreferences存储,以及一般的txt存储。

sharedpreferences:会更加方便,都已经封装好了。灵活使用。

txt存储:不便于管理,都是一堆或者一行行存储的文本。

自定义xml存储:可以自定义存储结构以及存储位置都方便。

最后提醒大家:记得加上权限

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值