我需要用一个独特的TAG替换一堆xml标签,我找到了一个解决方案,但我不确定它是那里最好的......你会建议什么?
/** 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();
}
答案 0 :(得分:1)
尝试使用正则表达式解析XML是一个坏主意。以下是使用DOM的解决方案:
public static void replaceGroupTags(File input, File output, String tag, String replacement) {
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(inputFile));
XPath xpath = XPathFactory.newInstance().newXPath();
NodeList nodes = (NodeList)xpath.evaluate("//SomeNode/" + tag, doc, XPathConstants.NODESET);
// replace all occurences
for (int i = 0; i < nodes.getLength(); i++) {
nodes.item(i).setTextContent(replacement);
}
// save result to the file
Transformer xformer = TransformerFactory.newInstance().newTransformer();
xformer.transform(new DOMSource(doc), new StreamResult(output);
}
遍历文件,在tag
节点内查找SomeNode
所有出现的内容,并将其内容替换为replacement