通过正则表达式在典型的XML文件中选择需要保存的数据:
<Sms>
<MobileNumber>你的电话号码</MobileNumber> <!--
<Body>卢队:你有课吗?</Body>
<Type>1</Type>
<MessageType>0</MessageType>
<Read>1</Read>
<Status>-1</Status>
<ReportDate>0</ReportDate>
<SendDate>1257348421</SendDate>
<PartNumber>1</PartNumber>
<ID>2103</ID>
</Sms>
上面我需要的数据是
<MobileNumber>
<Body>
<SendDate>
之前由于写习惯了php中对于任意字符的判断(.*?)(.+?)导致中文弄不出来
贴代码:
/*
* M8 SMS data converter
*/
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MyFileFilter {
public static void main(String[] args) throws Exception {
Pattern phone = Pattern.compile("<MobileNumber>(.*)</MobileNumber>", Pattern.DOTALL);
Pattern body = Pattern.compile("<Body>(.*)</Body>", Pattern.DOTALL);
Pattern date = Pattern.compile("<SendDate>(.*)</SendDate>", Pattern.DOTALL);
Matcher matcher;
//文件需要在工程目录
File file = new File("jonny.txt");
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String data = null;
while ((data = reader.readLine()) != null){
//显示行号
if((matcher = phone.matcher(data)).find())
System.out.println("Phone: "+matcher.group(1));
else if((matcher = body.matcher(data)).find()){
System.out.println("Body: "+matcher.group(1));
}
else if((matcher = date.matcher(data)).find()){
//System.out.println("Date: "+matcher.group(1));
System.out.println("Date: "+getDate(matcher.group(1)));
}
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null){
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
public static String getDate(String unixDate) {
SimpleDateFormat fm = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
long unixLong = 0;
String date = "";
try {
unixLong = Long.parseLong(unixDate) * 1000;
} catch(Exception ex) {
System.out.println("String转换Long错误,请确认数据可以转换!");
}
try {
date = fm.format(unixLong);
} catch(Exception ex) {
System.out.println("String转换Date错误,请确认数据可以转换!");
}
return date;
}
}