利用dom4j处理超大XML文件,1GB以上
参考:
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());
}
}