[英]Android data store in XML files
In my android application I am using an xml file to store some history information within the application. 在我的Android应用程序中,我正在使用xml文件在应用程序中存储一些历史记录信息。
Following is the code I use to enter a new record to the file. 以下是我用于向文件输入新记录的代码。
String filename = "file.xml";
File xmlFilePath = new File("/data/data/com.testproject/files/" + filename);
private void addNewRecordToFile(History history)
{
try
{
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
Document doc = docBuilder.parse(xmlFilePath);
Element rootEle = doc.getDocumentElement();
Element historyElement = doc.createElement("History");
rootEle.appendChild(historyElement);
Element customerEle = doc.createElement("customer");
customerEle.appendChild(doc.createTextNode(history.getCustomer()));
historyElement.appendChild(customerEle);
Element productEle = doc.createElement("product");
productEle.appendChild(doc.createTextNode(history.getProduct()));
historyElement.appendChild(productEle);
//-------->
DOMSource source = new DOMSource(doc);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
StreamResult result = new StreamResult(xmlFilePath);
transformer.transform(source, result);
}
catch (ParserConfigurationException e)
{
Log.v("State", "ParserConfigurationException" + e.getMessage());
}
catch (SAXException e)
{
Log.v("State", "SAXException" + e.getMessage());
}
catch (IOException e)
{
Log.v("State", "IOException" + e.getMessage());
}
catch (TransformerConfigurationException e) {
e.printStackTrace();
}
catch (TransformerFactoryConfigurationError e) {
e.printStackTrace();
}
catch (TransformerException e) {
e.printStackTrace();
}
}
XML file format XML文件格式
<?xml version="1.0" encoding="UTF-8"?>
<HistoryList>
<History>
<customer>Gordon Brown Ltd</customer>
<product>Imac</product>
</History>
<History>
<customer>GG Martin and Sons</customer>
<product>Sony Vaio</product>
</History>
<History>
<customer>PR Thomas Ltd</customer>
<product>Acer Laptop</product>
</History>
</HistoryList>
So using this code I can successfully add a new rocord to the file. 因此,使用此代码,我可以成功向文件添加新的rocord。 But My minimum target version in android shoud be API level 4. This code works well with API Level 8 and above.
但我在android中的最低目标版本应该是API级别4。此代码与API级别8及以上版本配合良好。
DOMSource , TransformerFactory classes are not available in android API levels under 8. So All the things before the comment //--------> works in APIs below 8. DOMSource和TransformerFactory类在8以下的android API级别中不可用,因此,注释之前的所有内容// -------->在8以下的API中均有效。
Does anyone know any way that I can write to the xml file without using Transformer APIs. 有谁知道不使用Transformer API就可以写入xml文件的任何方式。 Thanks in advance...
提前致谢...
EDITS..... 编辑.....
In my case I have to use a xml file to store information. 就我而言,我必须使用xml文件来存储信息。 That's why I don't look for sharedpreferences or Sqlite DB to store data.
这就是为什么我不寻找sharedpreferences或Sqlite DB来存储数据的原因。 Thanks.
谢谢。
Maybe you should store this information is SharedPreferences instead? 也许您应该将此信息存储为SharedPreferences呢? If you have a specific reason why you are trying to write this to XML, that is fine.
如果您有尝试将其写入XML的特定原因,那很好。
Otherwise, I would suggest using a different method of persistence in Android - as there are a few built in ways to do this that are easier that working with XML. 否则,我建议在Android中使用其他持久性方法-因为有一些内置的方法可以做到这一点,比使用XML更容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.