java 读取dat文件&&写入xml文件

[color=darkblue]一、20090619084650625.dat文件如下[/color]


<OffenceSnap>
<snap_id>17127</snap_id>
<snap_nbr>20090619084650625</snap_nbr>
<snap_frames>3</snap_frames>
<snap_type>雷达抓拍</snap_type>
<snap_type_int>2</snap_type_int>
<offence_time>2009-06-19 08:46:50</offence_time>
<road>G107国道1175公里处</road>
<road_code>1301</road_code>
<road_distance>1175000</road_distance>
<drive_pane>0</drive_pane>
<drive_direction/>
<black_list_type/>
<black_list_type_code/>
<offence/>
<offence_code>1303</offence_code>
<licence_plate/>
<plate_type/>
<plate_type_code/>
<plate_color/>
<plate_color_code/>
<vehicle_owner/>
<vehicle_color_code/>
<vehicle_color/>
<vehicle_addr/>
<vehicle_model/>
<vehicle_xh/>
<vehicle_owner_tel/>
<vehicle_audit_date/>
<device_no>000001</device_no>
<base_velocity>0</base_velocity>
<obj_velocity>80</obj_velocity>
<max_velocity>70</max_velocity>
<min_velocity>0</min_velocity>
<snap_status>新记录</snap_status>
<snap_status_int>0</snap_status_int>
<duty_police_no/>
<duty_department>孝感市公安局交警支队直属二大队</duty_department>
<duty_department_code/>
<snap_memo/>
<process_date/>
<field_process>0</field_process>
<penalty_type>罚款</penalty_type>
<penalty_type_int>0</penalty_type_int>
<penalty_regulation/>
<offence_regulation/>
<penalty_amount>0</penalty_amount>
<penalty_score>0</penalty_score>
<driver_id/>
<driver_name/>
<driver_addr/>
<alcohol_value>0</alcohol_value>
<evidence_type>0</evidence_type>
<hang1>0</hang1>
<hang2>0</hang2>
<hang3>0</hang3>
</OffenceSnap>


[color=darkblue]二、WW.java如下[/color]

package com.dat;
import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

public class WW {
SAXBuilder builder = null;
Document readDocument = null;
Map<String,Object> map=null;
/*
* 这个方法的作用是制造一个中间文件bb.dat,因为要读的文件中没有"<?xml version='1.0' encoding='gbk'?>"
* 这句话,导致jdom无法解析xml文件,所以我必须新建一个过度文件在文件头加入该句话,然后把要读的文件追加到后面,而且
* 必须一行一行的读一行一行的写,保证格式正确。
* */
public void tt(String path) {//要读文件的绝对路径
try {

File read = new File(path);//建立文件对象
File write = new File(path+".xml");//建立文件对象

BufferedReader br = new BufferedReader(new FileReader(read));//建立读文件流
BufferedWriter bw = new BufferedWriter(new FileWriter(write));//建立写文件流
String temp = null;
temp = "<?xml version='1.0' encoding='gbk'?>";
bw.append(temp + "\r\n");//写入一行后换行

temp = br.readLine();// 一行一行读入

while (temp != null) {
// 写文件
bw.append(temp + "\r\n"); // 只适用Windows系统
// 继续读文件
temp = br.readLine();
}

bw.close();
br.close();

} catch (FileNotFoundException e) { // 文件未找到
System.out.println(e);
} catch (IOException e) {
System.out.println(e);
}
}
/*
* 该方法用来读指定的xml格式的文件
* */
public Map<String,Object> readXml(String path) {
builder = new SAXBuilder();//得到SAXBuilder对象
map=new HashMap();

try {
readDocument = builder.build(path+".xml");// 读值定的dat,xml文件,得到指定的xml对象
} catch (JDOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//得到根节点
Element rootElement = readDocument.getRootElement();
//得到根节点下的所有子节点
List list = rootElement.getChildren();
//迭代循环输出指定的节点名字和节点的值
for (Iterator i = list.iterator(); i.hasNext();) {
Element current = (Element) i.next();
String tagName=current.getName();
String tagText=current.getText();
map.put(tagName, tagText);
}
return map;
}

public static void main(String[] args) {
new WW().tt("F:/20090619084650625.dat");
Map map= new WW().readXml("F:/20090619084650625.dat");

Iterator<String> it=map.keySet().iterator();
while(it.hasNext()){
String keyName=it.next();
System.out.println("["+keyName+"]="+map.get(keyName));
}
}
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值