2014-09-01 33 views
0

我已經註釋文本文件與某些特定的標籤之後,我想放在XML標籤的標籤中普通的文本文件,註釋例子如下創建基於文本的XML標籤:如何用java

The 
reflection 
implementation 
in 
[email protected]@[email protected]@SP3#SOFTWARE, 
[email protected],@[email protected],@3.5.1,@[email protected]#MODIFIER 
does 
not 
properly 
enforce 
object 
permissions 

我想運行Java腳本到上面的文字轉換成以下格式:

The reflection implementation in <SOFTWARE> [email protected]@[email protected]@SP3</SOFTWARE>, 
<MODIFIER> [email protected],@[email protected],@3.5.1,@[email protected] </MODIFIER> does not properly enforce 
object permissions 

我與follwoing 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); 

但我弄糊塗了,當我分裂字符串和所需的字符缺失

+0

不,實際上。 – user3635168 2014-09-01 15:44:59

+0

我需要將結果轉換爲xml格式。請任何幫助 – user3635168 2014-09-01 19:04:13

+0

我想將轉換後的文本的結果保存到xml文件中 – user3635168 2014-09-02 12:55:21

回答

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()); 

輸出(如果刪除逗號軟件後,在文件中):

The reflection implementation in <SOFTWARE>[email protected]@[email protected]@SP3</SOFTWARE> <MODIFIER>[email protected],@[email protected],@3.5.1,@[email protected]</MODIFIER> does not properly enforce object permissions

+0

我在標籤內獲得了逗號! ...是什麼問題? – user3635168 2014-09-01 16:00:29

+0

這很難,因爲我有超過100個文件難以刪除所有的逗號 – user3635168 2014-09-01 16:09:09

+0

如何在一些部分例如「1.1 @ SP1,@ 2.0 @ SP2,@ 3.5.1,@和@ 4 #MODIFIER 「逗號是很重要的,而在#SOFTWARE,並不重要 – user3635168 2014-09-01 16:20:29