-1

I have an xml alone, I wanted to add a markup outside of a tag called scl

so for example

<documents>
<rnl></rnl>
<snl></snl>
<scl>This is scl</scl>
</documents>

So <scl>This is scl</scl> will be transformed to <credit><scl>This is scl</scl></credit> How do I modify the below to achieve this ? So far I have tried.

String content5 = "<scl>this is scl</scl>";
System.out.println(content5.replaceAll("^(?:&lt;scl&gt;)(?:.*?)(?:&lt;/scl&gt;)", "<credit>$1</credit>" ));
4

4 回答 4

1

一种方法是这样做:

String content5 = "<scl>this is scl</scl>";
System.out.println(content5.replace("<scl>", "<credit><scl>").replace("</scl>", "</scl></credit>"));
于 2013-05-15T04:20:34.497 回答
0

尝试这个

    StringBuilder sb = new StringBuilder(content5);

    sb.insert(sb.indexOf("<scl>"), "<credit>");
    sb.insert(sb.indexOf("</scl>") + "</scl>".length(), "</credit>");

    String result = sb.toString();
于 2013-05-15T04:22:05.377 回答
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>
于 2013-05-15T04:23:17.593 回答
0

您可以完全改变思考问题的方式并使用适当的库来处理 XML Dom4j。非常容易学习,非常简单易用。您需要的一切都在此链接中进行了说明。

于 2013-05-15T04:36:23.660 回答