我需要用一些值替换命名空间
private static String REGEX1 = "(<\\/?)[ns2]+?:";
private static String INPUT = "<ns1:fso xmlns:ns2='https://www.example.com/fsoCanonical'><ns2:senderId xmlns='http://www.example.com/fsoCanonical'>abc</ns2:senderId><receiverId xmlns='http://www.example.com/fsoCanonical'>testdata</receiverId> <messageId xmlns='http://www.example.com/fsoCanonical'>4CF4DC05126A0077E10080000A66C871</messageId> </ns1:fso> ";
private static String REPLACE = "$1cc:";
public static void main(String[] args) {
Pattern p = Pattern.compile(REGEX1);
Matcher m = p.matcher(INPUT); // get a matcher object
StringBuffer sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, REPLACE);
}
m.appendTail(sb);
System.out.println(sb.toString());
我需要将我的命名空间从ns2替换为cc,但上面的代码只能替换ns2:到cc:我需要用xmlns替换标签:ns2 = with xmlns:cc = also。请告知如何使用相同的正则表达式替换它。
<ns1:fso xmlns:ns2='https://www.example.com/fsoCanonical'><ns2:senderId xmlns='http://www.example.com/fsoCanonical'>abc</ns2:senderId><receiverId xmlns='http://www.example.com/fsoCanonical'>testdata</receiverId> <messageId xmlns='http://www.example.com/fsoCanonical'>4CF4DC05126A0077E10080000A66C871</messageId> </ns1:fso> "
到
<ns1:fso xmlns:cc='https://www.example.com/fsoCanonical'><cc:senderId xmlns='http://www.example.com/fsoCanonical'>abc</cc:senderId><receiverId xmlns='http://www.example.com/fsoCanonical'>testdata</receiverId> <messageId xmlns='http://www.example.com/fsoCanonical'>4CF4DC05126A0077E10080000A66C871</messageId> </ns1:fso> "