【问题标题】:Android data store in XML filesXML 文件中的 Android 数据存储
【发布时间】:2013-01-05 13:41:32
【问题描述】:

在我的 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类在android API级别8以下不可用。所以注释之前的所有东西//------- -> 适用于低于 8 的 API。

有谁知道我可以在不使用 Transformer API 的情况下写入 xml 文件的任何方法。提前谢谢...

编辑.....

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

【问题讨论】:

    标签: java android xml


    【解决方案1】:

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

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

    【讨论】:

    • 根据要求,我必须将信息存储在 XMl 文件中。我不能使用 Sqlite 或 sharedpreferences。这就是为什么我希望将其存储在一个 xml 文件中......难道不能用 android 中的 xml 文件来做到这一点吗?
    • 是的,这是可能的,但过于复杂(正如您所经历的那样)。保存数据的首选方法是 SQLLite 或 SharedPrefs。祝你好运。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-22
    • 1970-01-01
    • 2013-06-07
    • 2011-08-06
    • 2010-12-20
    • 2014-01-03
    • 1970-01-01
    相关资源
    最近更新 更多