0

I want to take an XML file as input which contains the following:

<?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.

How can I do this in Android/Java. I'm new to Android and Java and all the tutorials available on the internet are way too complicated.

Thank You

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

Comments

1

You can find your answer here. It is like parsing json. You can cast your string(from file) to object and do anything with parameters

1 Comment

I mentioned "way too complicated" keeping your link in mind. I asked here because I want a simple solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.