2013-01-22 50 views
0

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

DOMSource的的TransformerFactory类不低于8.因此所有的东西在Android的API级别提供的评论// --------前>作品的API低于8

有没有人知道我可以写入xml文件的任何方式,而无需使用Transformer API。在此先感谢...

EDITS .....

在我来说,我必须使用XML文件来存储信息。这就是为什么我不寻找共享首选项或Sqlite DB来存储数据。谢谢。

回答

0

也许你应该存储这个信息是SharedPreferences而不是?如果你有一个特定的原因,你为什么试图把它写入XML,那没问题。否则,我会建议在Android中使用不同的持久化方法 - 因为有一些内置的方法可以更容易地处理XML。

+0

根据要求,我必须将信息存储在XMl文件中。我无法使用Sqlite或sharedpreferences。这就是为什么我正在寻找存储在一个XML文件中...是不是可以用android中的xml文件来做到这一点? – JibW

+0

是的,这是可能的,但过于复杂(正如你正在经历)。持久数据的首选方式是SQLLite或SharedPrefs。祝你好运。 – Booger