2011-06-08 13 views
2

我試圖用JDOm和轉換器的概念修改XML數據,這兩個函數在2.2版本中都有效。但每當我嘗試在2.1中進行編譯時,我都會遇到異常。另外我在谷歌搜索這個問題,他們提到2.1版本從不支持變壓器的概念。什麼是修改XML文件的替代方法。如何在android 2.1版本中重寫或修改XML

String filePath = Environment.getExternalStorageDirectory() + getDir; 
File file = new File(filePath); 
if (file.exists()) { 
    Document document = (Document) builder.build(file); 

    Element root = document.getRootElement(); 
    Element EditableTag = root.getChild("firsttag"); 
    EditableTag.setText("changed String"); 

    /** 
    * Print the modified xml document 
    */ 
    String des = new XMLOutputter().outputString(document); 
    System.out.println("String: " + des); 

    /** 
    * Modify the orginal document using FileWritter 
    */ 
    FileWriter fileWriter = new FileWriter(file); 
    fileWriter.write(des); 
    fileWriter.close(); 
} 

此代碼是在2.2版本中工作,同時我在2.1編譯這我得到FleNotFound異常。

回答

4

您可以使用Simple XML讀取對象中的xml文件,修改其狀態並將其寫回。

+0

感謝您的立即答覆,請檢查我的代碼。 – RAAAAM 2011-06-08 12:33:42

+0

@HariRam:你是什麼意思請檢查我的代碼?你想讓我們爲你做嗎?只要使用簡單的XML,相信我,這是你所需要的,它會導致更快的開發時間。 – 2011-06-08 13:24:30