如何使用java从文本创建xml标记

时间:2014-09-01 15:30:33

标签: java xml string split tags

我有一些带有某些标签的带注释的文本文件,之后我想将这些标签放在普通文本文件中的xml标签中,注释示例如下:

The
reflection
implementation
in
Microsoft@.NET@Framework@1.0@SP3#SOFTWARE,
1.1@SP1,@2.0@SP2,@3.5.1,@and@4#MODIFIER
does
not
properly
enforce
object
permissions

我想运行java脚本将上述文本转换为以下格式:

The reflection implementation in <SOFTWARE> Microsoft@.NET@Framework@1.0@SP3</SOFTWARE>, 
<MODIFIER> 1.1@SP1,@2.0@SP2,@3.5.1,@and@4 </MODIFIER> does not properly enforce 
object permissions

我尝试了以下的java代码:

for (String line; (line = br.readLine()) != null;) {
        String split[] = line.split("\\s");
        for (String string : split) {
            if (string.indexOf("<") != -1) {

                String annotatedText = string;
                Pattern tagPattern = Pattern
                        .compile("<(\\S+?)(.*?)>(.*?)</\\1>");
                Matcher m = tagPattern.matcher(annotatedText);

但是当我分割字符串和缺少所需的字符时,我感到困惑

1 个答案:

答案 0 :(得分:0)

BufferedReader br = new BufferedReader(new InputStreamReader(Test.class.getResourceAsStream("test.txt")));
StringBuilder output = new StringBuilder();
for (String line; (line = br.readLine()) != null;) {
     String split[] = line.split("#");
        if (split.length == 1)
            output.append(" " + line);
        else 
            output.append(String.format(" <%s>%s</%s>",split[1],split[0],split[1]));
}
System.out.println(output.toString().trim());

输出(如果您在文件中删除了SOFTWARE后的逗号):

The reflection implementation in <SOFTWARE>Microsoft@.NET@Framework@1.0@SP3</SOFTWARE> <MODIFIER>1.1@SP1,@2.0@SP2,@3.5.1,@and@4</MODIFIER> does not properly enforce object permissions