开发者

How can I write in an XML file?

开发者 https://www.devze.com 2023-03-19 08:34 出处:网络
I have a \"config.xml\" file in asset folder. I use the following code to read from it: public static String readAppConfigKey(Context context, String section,

I have a "config.xml" file in asset folder. I use the following code to read from it:

public static String readAppConfigKey(Context context, String section,
        String key) {
    String value = "";
    AssetManager assetManager = con开发者_运维百科text.getAssets();

    InputStream istr;
    try {
        istr = assetManager.open("config.xml");
        XmlPullParserFactory factory;
        factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xmlParser = factory.newPullParser();
        xmlParser.setInput(istr, "UTF-8");

        String strPrevElement = "";
        String strElement = "";
        String strKey = "";

        xmlParser.next();
        int eventType = xmlParser.getEventType();
        while (eventType != XmlResourceParser.END_DOCUMENT) {
            if (eventType == XmlResourceParser.START_TAG) {
                if (xmlParser.getName().compareTo(strElement) != 0) {
                    // after any change
                    strPrevElement = strElement;
                    strElement = xmlParser.getName();
                }
                strKey = xmlParser.getAttributeValue(null, "key");
                if (strPrevElement.compareTo(section) == 0
                        && strKey != null && strKey.compareTo(key) == 0) {
                    value = xmlParser.getAttributeValue(null, "value");
                    return value;
                }
            }
            eventType = xmlParser.next();
        }
    } catch (XmlPullParserException e) {

    } catch (IOException e) {

    }
    return value;
}

How can I write in it using XmlPullParser?

Thanks,


I don't believe you can write to a file in the assets folder. I think you will have to copy it to the sdcard and read and write to it there.

Also, XmlPullParser only reads XML, it does not write. Look at this tutorial on how to modify XML:

How to modify XML file in Java


If you have InputStream you have the bytes of the file and if you have the bytes of the file write them to disk (:

        //1. Convert the inputStream to Byte Array
        InputStream inputStream = . . .

        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        int nRead;
        byte[] data = new byte[Integer.MAX_VALUE]; //need to make sure here that the inputstream is less then 2g (Integer.MAX_VALUE), in case its bigger file we will need to read and write in parts
        while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
            buffer.write(data, 0, nRead);
        }
        buffer.flush();

        byte[] xmlFileBytes = buffer.toByteArray();

        //2.  write the bytes to file
        String filePath = "file.xml";
        File file = new File(filePath);
        FileOutputStream outputStream = new FileOutputStream(file);
        outputStream.write(xmlFileBytes);
        outputStream.close();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号