Vote count:
0
I need to replace a bunch of xml tags with a unique TAG, I found a solution but I am not sure it's the best out there... What would you propose?
/** Group tags replacement.
* @param input <mytag><tag>val1</tag><tag>val2</tag></mytag>
* @param tag tag
* @param replacement <tag>val</tag>
* @return <mytag><tag2>value</tag2></mytag>
*/
public static String replaceGroupTags(String input, String tag, String replacement) {
Pattern replace = Pattern.compile("<" + tag + ">" + ".*" + "</" + tag + ">");
Matcher matcher = replace.matcher(input);
int start = Integer.MIN_VALUE;
int end = Integer.MAX_VALUE;
while (matcher.find()) {
if (start == Integer.MIN_VALUE) start = matcher.start();
if (end <= Integer.MAX_VALUE) end = matcher.end();
}
StringBuffer stringBuffer = new StringBuffer(input);
return stringBuffer.replace(start, end, replacement).toString();
}
asked 2 mins ago
Aucun commentaire:
Enregistrer un commentaire