在 Android 中更新\修改 XML 文件 [英] Update\Modify an XML file in Android

查看:46
本文介绍了在 Android 中更新\修改 XML 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何实时更新 XML 文件.例如,我有这个文件:

I want to know how I can update an XML file in real time. I had this file for example:

<?xml version="1.0" encoding="UTF-8"?>
    <Cars>
        <car make="Toyota" model="95" hp="78" price="120"/>
        <car make="kia" model="03" hp="80" price="300"/>
    </Cars>

我应该怎么做才能像这样更新价格值?:

What should I do to update the price value like this one? :

<?xml version="1.0" encoding="UTF-8"?>
    <Cars>
        <car make="Toyota" model="95" hp="78" price="50"/>
        <car make="kia" model="03" hp="80" price="100"/>
    </Cars>

我在网上搜索过,但我发现的只是如何解析,以及如何使用 XmlSerializer 编写整个文件,而不是如何修改.我还发现 this 在 Java 中,但我未能在 Android 上实现它,因为我对 android-xml 世界很陌生.

I have searched the web but all I found was how to parse, and how to write the whole file using XmlSerializer, but not how to modify. Also I have found this in Java but I failed to implement it on Android because I'm very new to the android-xml world.

推荐答案

经过漫长的一天搜索和尝试,我已经使用 Java 的 DOM 达到了我的目标.要修改 XML 文件,请首先实例化这些文件以处理 XML:

OK after long day with search and tries I have reached my Goal using Java's DOM . To modify an XML file first instantiate these to handle the XML:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(context.openFileInput("MyFileName.xml")); // In My Case it's in the internal Storage

然后通过以下方式为所有汽车"元素创建一个 NodeList :

then make a NodeList for all "car" elements by :

NodeList nodeslist = doc.getElementsByTagName("car");

或通过将汽车 String 替换为 "*" 的所有元素.

or all elements by replacing the car String with "*".

现在它可以很好地搜索每个节点属性,直到它确定 KIA 的 "price" 值,例如:

Now it well search in every node attributes till it fine the "price" value of KIA for example:

for(int i = 0 ; i < nodeslist.getLength() ; i ++){
            Node node = nodeslist.item(i);
            NamedNodeMap att = node.getAttributes();
            int h = 0;
            boolean isKIA= false;
            while( h < att.getLength()) {
                Node car= att.item(h);
                if(car.getNodeValue().equals("kia"))
                   isKIA= true;      
                if(h == 3 && setSpeed)   // When h=3 because the price is the third attribute
                   playerName.setNodeValue("100");   
                 h += 1;  // To get The Next Attribute.
           }
}

OK 最后,使用 Transformer 将新文件保存在同一位置,如下所示:

OK Finally , Save the new File In the same location using Transformer like this :

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource dSource = new DOMSource(doc);
StreamResult result = new StreamResult(context.openFileOutput("MyFileName.xml", Context.MODE_PRIVATE));  // To save it in the Internal Storage
transformer.transform(dSource, result);

就是这样:).我希望这会有所帮助.

That's it :) . I hope this will helps .

这篇关于在 Android 中更新\修改 XML 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆