2013-08-16 123 views
0

我有一個java代碼爲txt文件轉換爲XML文件:(它的工作原理)轉換爲XML

Pattern p = Pattern.compile("[email protected]+\\.[a-z]+"); 
BufferedReader in = new BufferedReader(new FileReader(fromFile)); 
FileOutputStream fos = new FileOutputStream(toFile); 
OutputFormat of = new OutputFormat("XML", "windows-1250", true); 
of.setIndent(1); 
of.setIndenting(true); 
XMLSerializer serializer = new XMLSerializer(fos, of); 
ContentHandler hd = serializer.asContentHandler(); 
hd.startDocument(); 
    AttributesImpl atts = new AttributesImpl(); 
    hd.startElement("", "", "CDR_FILES", atts); 
    String line = null, tag; 
    while ((line = in.readLine()) != null) { 
     if (line.equals("Cdr File")) { 
      line = in.readLine(); 
      hd.startElement("", "", "CDR", atts); 
      int i = 0; 
      while (!line.equals("Cdr File")) { 
       if (i == 0) 
       tag = "Record_No"; 
       else if (i == 1) 
       tag = "Time"; 
       else if (i == 2) 
       tag = "Serial_Number"; 
       else if (i == 3) 
       tag = "Serial_Number_in_module"; 

這是我的txt文件:

Cdr File 
1 
Time=2010/02/15 17:15:17 
Serial_Number=1924891018 
Serial_Number_in_module=282826228ff 
Cdr File 
2 
Time=2010/02/15 17:15:17 
Serial_Number=1924891018 
Serial_Number_in_module=283423627ff 
... 

我有2個字母「 ff「在Serial_Number_in_module中,我不需要保存在xml文件中。 可以不保存這些字母嗎?

+0

你有什麼問題?當讀取文件時,我看不到使用字符串的一部分的問題 - 「String.substring()方法」或「String.replace() – Piro

回答

1

你在閱讀從文件中獲取的字符串,然後寫了條件序號

String sample = "282826228ff"; 
String output = sample.replaceAll("\\D+",""); 

輸出: 282826228