利用dom4j处理超大XML文件,1GB以上

jim20100728 2018-03-19 11:51:02
参考:
https://github.com/jimzhou2015/dom4j-big-xml

目标对象:
CarEngine:
public class CarEngine extends XmlBase {


@Column(name="core")
private Integer core;
@Column(name="type")
private String type;
public Integer getCore() {
return core;
}
public void setCore(Integer core) {
this.core = core;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}




}
Car:
public class Car extends XmlBase{
public String type;
public String product;
public Double price;
public List<CarEngine> carEngine;



public List<CarEngine> getCarEngine() {
return carEngine;
}
public void setCarEngine(List<CarEngine> carEngine) {
this.carEngine = carEngine;
}

public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getProduct() {
return product;
}
public void setProduct(String product) {
this.product = product;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}



}


GenXml:生成测试文件

public class GenXml {
public static void main(String[] args) throws Exception{
String xpath=System.getProperty("user.dir")+"/src/test/java/CarDataBig.xml";
System.out.println(System.getProperty("user.dir"));
File file = new File(xpath);
if(file.exists()){
file.delete();
}
int len =1000000;
file.createNewFile();
BufferedWriter bw = null;
FileWriter fw = new FileWriter(file);
bw = new BufferedWriter(fw);
bw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
bw.newLine();
bw.write("<Cars>");
bw.newLine();
String el="<Car><type>本田</type><product>中国</product><price>22.5</price><carEngine><core>16</core><type>Intel</type></carEngine><carEngine><core>16</core><type>Intel</type></carEngine></Car>";
int icount=100;
StringBuffer sbf=new StringBuffer();
for(int i=0; i<icount;i++){
sbf.append(el);
}
for(int i=0; i<len;i++){
bw.write(sbf.toString());
bw.newLine();
}

bw.write("</Cars>");
bw.flush();
bw.close();

}
}

XmlHandler1:回调处理

public class XmlHandler1 implements XmlHandler{

public void hande(Map<String, List<Object>> reMap) {

System.out.println("=======hande==========");
List<Object> list = reMap.get("Car");

for(Object obj:list){
Car car = (Car) obj;

System.out.println(car.getType());
}
}

}


Test:测试类

public class Test {

public static void main(String[] args) {
// TODO Auto-generated method stub
String xpath=System.getProperty("user.dir")+"/src/test/java/CarDataBig.xml";
System.out.println(System.getProperty("user.dir"));
String dataStartTag =null;
List<Class<? extends XmlBase>> clslist = new ArrayList<Class<? extends XmlBase>>();
clslist.add(Car.class);
Xml4bigKit.parseXml(xpath, dataStartTag,clslist,new XmlHandler1());

}

}



...全文
514 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
dong_19890208 2018-03-19
  • 打赏
  • 举报
回复
给楼主一个建议,代码可以用代码块框起来,这样看起来容易点

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧