此段字符:String str="
谢谢!
[Page]1[/Page]
谢谢!谢谢!
<Notation>65</Notation>
谢谢!谢谢!谢谢!
<Notation>66</Notation>
谢谢!谢谢!谢谢!谢谢!
<Notation>67</Notation>
谢谢!谢谢!谢谢!谢谢!谢谢!
<Notation>68</Notation>
谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!
<Notation>69</Notation>
谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!
[Page]1[/Page]
谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!
"<!--就是把上面的字符串处理成类似于此XML格式的,谢谢大家 -->
<Paragraph>
<Text>谢谢!</Text>
<Break style="Page">1</Break>
<Text>谢谢!谢谢!</Text>
<Linker Style="Notation" Destination="65" />
<Text>谢谢!谢谢!谢谢</Text>
<Linker Style="Notation" Destination="66" />
<Text>谢谢!谢谢!谢谢!谢谢!</Text>
<Linker Style="Notation" Destination="67" />
<Text>谢谢!谢谢!谢谢!谢谢!谢谢!</Text>
<Linker Style="Notation" Destination="68" />
<Text>谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!</Text>
<Linker Style="Notation" Destination="69" />
<Text>谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!</Text>
<Break style="Page">1</Break>
<Text>谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!</Text>
</Paragraph>
谢谢!
[Page]1[/Page]
谢谢!谢谢!
<Notation>65</Notation>
谢谢!谢谢!谢谢!
<Notation>66</Notation>
谢谢!谢谢!谢谢!谢谢!
<Notation>67</Notation>
谢谢!谢谢!谢谢!谢谢!谢谢!
<Notation>68</Notation>
谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!
<Notation>69</Notation>
谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!
[Page]1[/Page]
谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!
"<!--就是把上面的字符串处理成类似于此XML格式的,谢谢大家 -->
<Paragraph>
<Text>谢谢!</Text>
<Break style="Page">1</Break>
<Text>谢谢!谢谢!</Text>
<Linker Style="Notation" Destination="65" />
<Text>谢谢!谢谢!谢谢</Text>
<Linker Style="Notation" Destination="66" />
<Text>谢谢!谢谢!谢谢!谢谢!</Text>
<Linker Style="Notation" Destination="67" />
<Text>谢谢!谢谢!谢谢!谢谢!谢谢!</Text>
<Linker Style="Notation" Destination="68" />
<Text>谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!</Text>
<Linker Style="Notation" Destination="69" />
<Text>谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!</Text>
<Break style="Page">1</Break>
<Text>谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!</Text>
</Paragraph>
String[] lines = str.split("[\n]");
StringBuffer sb = new StringBuffer("<Paragraph>");
for (String s : lines) {
if (s.indexOf("[Page]")>=0){
s = s.replaceAll("\\[", "<").replaceAll("\\]", ">");
sb.append(s);
} else if (s.indexOf("<Notation>")>=0) {
sb.append(s);
} else {
sb.append("<Text>").append(s).append("</Text>");
}
}
sb.append("</Paragraph>");
System.out.println(sb);结果:<Paragraph><Text>谢谢!</Text><Page>1</Page><Text>谢谢!谢谢!</Text><Notation>65</Notation><Text> 谢谢!谢谢!谢谢!</Text><Notation>66</Notation><Text> 谢谢!谢谢!谢谢!谢谢!</Text> <Notation>67</Notation><Text> 谢谢!谢谢!谢谢!谢谢!谢谢!</Text> <Notation>68</Notation><Text> 谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!</Text> <Notation>69</Notation><Text> 谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!</Text> <Page>1</Page><Text> 谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!</Text></Paragraph>