简体   繁体   English

在Android / Java中更改XML值

[英]Changing XML values in Android/Java

I want to take an XML file as input which contains the following: 我想将一个包含以下内容的XML文件作为输入:

<?xml version='1.0' encoding='utf-8' standalone='yes'>
<map>
    <int name="count" value="10" />
</map>

and, read and change the value from 10 to any other integer value. 并将其从10更改为任何其他整数值。

How can I do this in Android/Java. 如何在Android / Java中执行此操作。 I'm new to Android and Java and all the tutorials available on the internet are way too complicated. 我是Android和Java的新手,互联网上的所有教程都太复杂了。

Thank You 谢谢

You can find your answer here . 您可以在这里找到答案。 It is like parsing json. 就像解析json。 You can cast your string(from file) to object and do anything with parameters 您可以将字符串(从文件)转换为对象,并使用参数进行任何操作

You can change the value by matching the pattern and replacing the string as like below, 您可以通过匹配模式并替换字符串来更改值,如下所示,

String xmlString = "<int name=\"count\" value=\"10\" />";
int newValue = 100;
Pattern pattern = Pattern.compile("(<int name=\"count\" value=\")([0-9]{0,})(\" />)");
Matcher matcher = pattern.matcher(xmlString);

while (matcher.find()) {
    String match = matcher.group(2);
    xmlString = xmlString.replace(match, String.valueOf(newValue));
}

System.out.println(xmlString);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM