How to rewrite or modify XML in android 2.1 versio

2019-07-16 11:49发布

问题:

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