这是基于对“>”字符分割字符串,然后在三种不同的情况处理令牌的解决方案:CDATA,开放标签,和结束标记
下面的代码应该工作(见下面的程序输出)。然而,标签“ID”存在问题 - 我们如何知道它的骆驼案件应该是“id”而不是“iD”?这需要一本词典来捕捉这些知识。所以下面的例程convert()有两种模式 - useDictionary为true或false。看看下面的解决方案是否满足您的要求。
要使用“useDictionary”模式,您还需要维护一个合适的字典(程序中称为“dict”的散列表,现在只有一个字典中的条目“ID”应该是骆驼式的“ ID”)。 注意,字典可以憋足了增量 - 你只需要在特殊情况下添加到字典中(例如,“ID”的驼峰是“ID”不“ID”)
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CamelCase {
private static Map<String, String> dict = new HashMap<>();
static {
dict.put("ID", "id");
}
public static void main(String[] args) {
String input = "<HeaderFirst> "
+ "\n <HeaderTwo>"
+ "\n <ID>id1</ID>"
+ "\n <TimeStamp>2016-11-04T02:46:34Z</TimeStamp>"
+ "\n <DetailedDescription>"
+ "\n <![CDATA[di]]>"
+ "\n </DetailedDescription>"
+ "\n </HeaderTwo> "
+ "\n</HeaderFirst>";
System.out.println("===== output without using a dictionary =====");
System.out.println(convert(input, false /* useDictionary */));
System.out.println("===== output using a dictionary =====");
System.out.println(convert(input, true /* useDictionary */));
}
private static String convert(String input, boolean useDictionary) {
String splitter = ">";
String[] tokens = input.split(splitter);
StringBuilder sb = new StringBuilder();
Pattern cdataPattern = Pattern.compile("([^<]*)<!\\[CDATA\\[([^\\]]*)\\]\\]");
Pattern oTagPattern = Pattern.compile("([^<]*)<(\\w+)");
Pattern cTagPattern = Pattern.compile("([^<]*)</(\\w+)");
String prefix;
String tag;
String newTag;
for (String token : tokens) {
Matcher cdataMatcher = cdataPattern.matcher(token);
Matcher oTagMatcher = oTagPattern.matcher(token);
Matcher cTagMatcher = cTagPattern.matcher(token);
if (cdataMatcher.find()) { // CDATA - do not change
sb.append(token);
} else if (oTagMatcher.find()) {// open tag - change first char to lower case
prefix = oTagMatcher.group(1);
tag = oTagMatcher.group(2);
newTag = camelCaseOneTag(tag, useDictionary);
sb.append(prefix + "<" + newTag);
} else if (cTagMatcher.find()) {// close tag - change first char to lower case
prefix = cTagMatcher.group(1);
tag = cTagMatcher.group(2);
newTag = camelCaseOneTag(tag, useDictionary);
sb.append(prefix + "<" + newTag);
}
sb.append(splitter);
}
return sb.toString();
}
private static String camelCaseOneTag(String tag, boolean useDictionary) {
String newTag;
if (useDictionary
&& dict.containsKey(tag)) {
newTag = dict.get(tag);
} else {
newTag = tag.substring(0, 1).toLowerCase()
+ tag.substring(1);
}
return newTag;
}
}
该程序的输出是这样的:匹配角括号中的字符串,如“<[a-zA-Z]+>”,并从那里SUBSTRING查找和替换取决于起始或结束标记的第二或第三的字符串
===== output without using a dictionary =====
<headerFirst>
<headerTwo>
<iD>id1<iD>
<timeStamp>2016-11-04T02:46:34Z<timeStamp>
<detailedDescription>
<![CDATA[di]]>
<detailedDescription>
<headerTwo>
<headerFirst>
===== output using a dictionary =====
<headerFirst>
<headerTwo>
<id>id1<id>
<timeStamp>2016-11-04T02:46:34Z<timeStamp>
<detailedDescription>
<![CDATA[di]]>
<detailedDescription>
<headerTwo>
<headerFirst>
查找模式。也许如果检查是否substring [1]是“/”。 – kar