How to rewrite or modify XML in android 2.1 version

江枫思渺然 提交于 2019-12-24 03:13:04

问题


I tried to modify XML data using JDOm and transformer concept, this two functions are works in 2.2 version. but whenever i tried to compile in 2.1 i am getting exception. Also i searched this issue in google, they mentioned 2.1 version never support transformer concept. what is the alternative way to modify XML file.

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();
}

This code is works in 2.2 version, at the same time i compile this in 2.1 i am getting FleNotFound exception.


回答1:


You can use Simple XML to read the xml-file in the object, modify its state and write it back.




回答2:


The problem is Environment.getExternalStorageDirectory() is not available in android API for 2.1

You need to construct the directory address yourself. The method for which has already been given here: getExternalFilesDir alternative in android 2.1



来源:https://stackoverflow.com/questions/6278489/how-to-rewrite-or-modify-xml-in-android-2-1-version

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!