您的位置:首页 > 移动开发 > Android开发

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

2016-05-27 10:21 603 查看
讲一下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存储:可以自定义存储结构以及存储位置都方便。

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

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: