0

我有一个将 txt 文件转换为 xml 文件的 java 代码:(它有效)

Pattern p = Pattern.compile(".+@.+\\.[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
...

我在 Serial_Number_in_module 中有 2 个字母“ff”,我不需要保存在 xml 文件中。可以不保存这些字母吗?

4

1 回答 1

1

您在读取时从文件中获取字符串,然后写入序列号的条件

例子

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

输出: 282826228

于 2013-08-16T10:23:58.977 回答