2012-01-21 56 views
0

我在以下路徑「/res/xml/keywords.xml」中有一個xml文件。 它具有以下內容如何將數據追加到Android中的xml內容中

<words> 
hdhsd 
</words> 

我用Java代碼:

OutputStreamWriter out=new OutputStreamWriter(openFileOutput("keywords.xml", MODE_APPEND)); 
    out.write("hello"); 
    out.close(); 

單詞「hello」並沒有被附加到文件中。爲什麼會發生?

回答

1

資源文件是隻讀的,無法編輯。您最好的選擇是在您第一次使用該文件時將該文件複製到應用程序的主文件夾中,並使用該副本。 (在這種情況下,最好將文件部署到/ assets而不是/ res/xml中。)

+0

感謝您的回答。您的意思是我將在以下路徑中使用我的xml文件「C:\ Documents and Settings \ Desktop \ D \ Temp \ Android \ Books \ Beginning Android 3 \ Applications \ AndroidApplication2」? –

+0

我使用下面的代碼在xml文件中寫入數據 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Document doc = docBuilder.parse(new File(「C:\\ Documents and Settings \\ Desktop \\ D \\ Temp \\ Android \\ Books \\ Beginning Android 3 \\ Applications \\ AndroidApplication2 \\ keywords.xml 「)); Node node = doc.getElementsByTagName(「words」)。item(0); org.w3c.dom.Element newelmnt = doc.createElement(「word」); newelmnt.appendChild(doc.createTextNode(「moijjjjjjjj」)); node.appendChild(newelmnt); –

+0

但IOException已經通過。這裏的問題在哪裏? –

1

要在您的Android應用程序中創建xml文件,您可以讀取,寫入和更新我的應用程序,請使用以下代碼

os=getApplicationContext().openFileOutput("keywords.xml", Context.MODE_WORLD_WRITEABLE); 
os.write("<words><word>moha</word></words>".getBytes()); 
os.close(); 
相關問題