如何在 XML 文件中写入内容?

发布于 2024-11-19 02:19:41 字数 1658 浏览 3 评论 0原文

我在 asset 文件夹中有一个“config.xml”文件。我使用以下代码从中读取:

public static String readAppConfigKey(Context context, String section,
        String key) {
    String value = "";
    AssetManager assetManager = context.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?

谢谢,

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 = context.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,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寂寞清仓 2024-11-26 02:19:41

我不相信您可以写入资产文件夹中的文件。我认为你必须将其复制到 SD 卡并在那里读取和写入。

另外,XmlPullParser 只读取 XML,不写入。查看有关如何修改 XML 的教程:

如何用Java修改XML文件

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

旧时浪漫 2024-11-26 02:19:41

如果您有 InputStream,则您拥有文件的字节,如果您拥有文件的字节,则将它们写入磁盘(:

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

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文