Android数据存储在XML文件中

时间:2013-01-22 10:40:36

标签: java android xml

在我的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及更高版本。

DOMSource TransformerFactory 类在8以下的Android API级别中不可用。所以评论之前的所有内容 // ------- - &gt; 适用于8以下的API。

有没有人知道如何在不使用 Transformer API的情况下写入xml文件。提前谢谢......

编辑.....

在我的情况下,我必须使用xml文件来存储信息。这就是为什么我不寻找共享偏好或Sqlite DB来存储数据的原因。谢谢。

1 个答案:

答案 0 :(得分:0)

也许您应该将此信息存储为SharedPreferences而不是?如果您有特定原因要将其写入XML,那很好。

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