我单独使用xml,我想在名为scl
所以例如
<documents>
<rnl></rnl>
<snl></snl>
<scl>This is scl</scl>
</documents>
所以<scl>This is scl</scl
&gt;将转换为<credit><scl>This is scl</scl></credit>
我如何修改以下来实现这一目标?到目前为止我已经尝试过。
String content5 = "<scl>this is scl</scl>";
System.out.println(content5.replaceAll("^(?:<scl>)(?:.*?)(?:</scl>)", "<credit>$1</credit>" ));
答案 0 :(得分:1)
一种方法是这样做:
String content5 = "<scl>this is scl</scl>";
System.out.println(content5.replace("<scl>", "<credit><scl>").replace("</scl>", "</scl></credit>"));
答案 1 :(得分:0)
试试这个
StringBuilder sb = new StringBuilder(content5);
sb.insert(sb.indexOf("<scl>"), "<credit>");
sb.insert(sb.indexOf("</scl>") + "</scl>".length(), "</credit>");
String result = sb.toString();
答案 2 :(得分:0)
你走了:
String content5 =
"<scl>this is scl</scl>\n" +
"<blah>this isn't</blah>\n" +
"<scl>this is scl</scl>\n" +
"<blah>this isn't</blah>\n";
System.out.println(content5.replaceAll("(?:<scl>)(?:.*?)(?:</scl>)", "<credit>$0</credit>" ));
输出:
<credit><scl>this is scl</scl></credit>
<blah>this isn't</blah>
<credit><scl>this is scl</scl></credit>
<blah>this isn't</blah>
答案 3 :(得分:0)
您可以完全改变对问题的思考方式,并使用正确的库来处理XML Dom4j 。非常容易学习,非常简单易用。您需要的一切都在此链接中解释。