繁体   English   中英

Android数据存储在XML文件中

[英]Android data store in XML files

在我的Android应用程序中,我正在使用xml文件在应用程序中存储一些历史记录信息。

以下是我用于向文件输入新记录的代码。

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文件格式

<?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>

因此,使用此代码,我可以成功向文件添加新的rocord。 但我在android中的最低目标版本应该是API级别4。此代码与API级别8及以上版本配合良好。

DOMSourceTransformerFactory类在8以下的android API级别中不可用,因此,注释之前的所有内容// -------->在8以下的API中均有效。

有谁知道不使用Transformer API就可以写入xml文件的任何方式。 提前致谢...

编辑.....

就我而言,我必须使用xml文件来存储信息。 这就是为什么我不寻找sharedpreferences或Sqlite DB来存储数据的原因。 谢谢。

也许您应该将此信息存储为SharedPreferences呢? 如果您有尝试将其写入XML的特定原因,那很好。

否则,我建议在Android中使用其他持久性方法-因为有一些内置的方法可以做到这一点,比使用XML更容易。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM