Dom4j

时间:2023-03-09 02:44:03
Dom4j

Dom4j

http://baike.baidu.com/link?url=2XOnr06saKUd-9By1GyPxIolXMQhf_C-CnMFll_yhtR4m00i27zphbkI5-dGpwEVjuKbkdosTYmNMXc4OA31ba

1

 package ec.export.checksheet;

 import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRPrintPage;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader; import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader; import ec.export.ECReportHelper;
import ec.utils.Dom4jXml; public class AddCheckSheet { public Dom4jXml dj = new Dom4jXml();
String TEMPLATES_PATH = "ec/export/resources/"; public JasperPrint FormTotalPage(JasperPrint jp,org.w3c.dom.Document document,Map mSelectCheckSheet,Map mCheckSheetAutoFill){
List l=new ArrayList();
try {
l=CheckSheetReadXml(document,mCheckSheetAutoFill);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} // String jasperpatha=getClass().getResource("")+"checksheeta.jasper";
// jasperpatha=jasperpatha.replaceAll("file:/", "");
// String jasperpathb=getClass().getResource("")+"checksheetb.jasper";
// jasperpathb=jasperpathb.replaceAll("file:/", ""); //ECReportHelper.locateResource(""); String jasperpatha=TEMPLATES_PATH+"checksheeta.jasper"; String jasperpathb=TEMPLATES_PATH+"checksheetb.jasper"; for(int i=;i<l.size();i++){ // String ec.export.ECJobReporter.buildStylesFile(); // ========for checksheeta.jasper=======
JasperPrint jasperprint = new JasperPrint();
try {
//JasperPrint jasperPrint = JasperFillManager.fillReport(getClass().getResourceAsStream("periodo.jasper"), parameters, con);
//jasperprint = JasperFillManager.fillReport(JRLoader.getResourceInputStream(jasperpathb), (Map)l.get(i));
jasperprint = JasperFillManager.fillReport(JRLoader.getResourceInputStream(jasperpatha), (Map)l.get(i), new JREmptyDataSource());
//jasperprint = JasperFillManager.fillReport(ClassLoader.getSystemResourceAsStream(jasperpathb), (Map)l.get(i), new JREmptyDataSource());
//jasperprint = JasperFillManager.fillReport(getClass().getResourceAsStream(jasperpathb), (Map)l.get(i), new JREmptyDataSource());
//jasperprint = JasperFillManager.fillReport(jasperpathb, (Map)l.get(i), new JREmptyDataSource()); } catch (Exception e) {
e.printStackTrace();
}
//System.out.println(" before jp: "+jp.getPages().size());
multipageLinking(jp,jasperprint);
//System.out.println(" after jp: "+jp.getPages().size()); // =======for checksheetb.jasper=======
jasperprint = new JasperPrint();
try {
//jasperprint = JasperFillManager.fillReport(JRLoader.getResourceInputStream(jasperpathb), (Map)l.get(i));
jasperprint = JasperFillManager.fillReport(JRLoader.getResourceInputStream(jasperpathb), (Map)l.get(i), new JREmptyDataSource());
//jasperprint = JasperFillManager.fillReport(ClassLoader.getSystemResourceAsStream(jasperpathb), (Map)l.get(i), new JREmptyDataSource());
//jasperprint = JasperFillManager.fillReport(getClass().getResourceAsStream(jasperpathb), (Map)l.get(i), new JREmptyDataSource());
//jasperprint = JasperFillManager.fillReport(jasperpathb, (Map)l.get(i), new JREmptyDataSource());
} catch (Exception e) {
e.printStackTrace();
}
//System.out.println(" before jp: "+jp.getPages().size());
multipageLinking(jp,jasperprint);
//System.out.println(" after jp: "+jp.getPages().size()); // JasperPrint result = JasperFillManager.fillReport(
// ECReportHelper.locateResource(JOB_TEMPLATE),
// params
// );
/*
// ========for checksheeta.jasper=======
JasperPrint jasperprint = new JasperPrint();
try {
jasperprint = JasperFillManager.fillReport(jasperpatha, (Map)l.get(i), new JREmptyDataSource());
} catch (Exception e) {
e.printStackTrace();
}
//System.out.println(" before jp: "+jp.getPages().size());
multipageLinking(jp,jasperprint);
//System.out.println(" after jp: "+jp.getPages().size()); // =======for checksheetb.jasper=======
jasperprint = new JasperPrint();
try {
jasperprint = JasperFillManager.fillReport(jasperpathb, (Map)l.get(i), new JREmptyDataSource());
} catch (Exception e) {
e.printStackTrace();
}
//System.out.println(" before jp: "+jp.getPages().size());
multipageLinking(jp,jasperprint);
//System.out.println(" after jp: "+jp.getPages().size());
*/
}
return jp;
} private JasperPrint multipageLinking(JasperPrint page1, JasperPrint page2) {
List pages = page2.getPages();
for (int count = ; count <pages.size(); count++) {
page1.addPage((JRPrintPage) pages.get(count));
}
return page1;
} // for checkSheet Print
public List CheckSheetReadXml(org.w3c.dom.Document doc,Map mCheckSheetAutoFill) throws Exception{
Document document=dj.parse(doc);
List pl=new ArrayList(); //count product num
List list = document.selectNodes("/report/job/product");
int num = list.size(); for(int i=;i<=num;i++)
{
Map mproduct=new HashMap();
//0 select_date
String xpathstr="/report/job/@select_date";
if(!dj.getContentString(document,xpathstr).equals("")){
mproduct.put("cs_Date", dj.getContentString(document,xpathstr));
} xpathstr="/report/job/createdDt";
if(!dj.getContentString(document,xpathstr).equals("")){
mproduct.put("cs_Date", dj.getContentString(document,xpathstr));
} //model 1 AT3ARF1R AT5CRF4R
xpathstr="/report/job/product["+i+"]/performanceData/group[name='Screw Compressor Air Cooled Package Chiller']/row/field[name='Model #']/value";
String s1=dj.getContentString(document,xpathstr);
//for acds in spec
if(s1.equals("")){
xpathstr="/report/job/product["+i+"]/performanceData/group[name='Scroll Compressor Air Cooled Package Chiller']/row/field[name='Model #']/value";
s1=dj.getContentString(document,xpathstr);
} xpathstr="/report/job/product["+i+"]/performanceData/group[name='Screw Compressor Air Cooled Package Chiller']/row/field[name='Component code']/value";
String tmep=dj.getContentString(document,xpathstr);
if(tmep==""){}else{
tmep=tmep.substring();
String tempbefore=tmep.substring();
String tempafter=tmep.substring(,);
tmep=tempbefore+tempafter;
}
mproduct.put("cs_Model", s1+tmep); xpathstr="/report/job/product["+i+"]/qty";
mproduct.put("cs_Qty", dj.getContentString(document,xpathstr)); //evap 2
xpathstr="/report/job/product["+i+"]/performanceData/group[name='Evaporator']/col1/field[contains(name,'Entering fluid temp')]/value";
mproduct.put("cs_Evap_Entering_fluid_temp", dj.getContentString(document,xpathstr));
xpathstr="/report/job/product["+i+"]/performanceData/group[name='Evaporator']/col1/field[contains(name,'Fluid flow rate')]/value";
mproduct.put("cs_Evap_Fluid_flow_rate", dj.getContentString(document,xpathstr));
xpathstr="/report/job/product["+i+"]/performanceData/group[name='Evaporator']/col2/field[contains(name,'Fouling factor')]/value";
mproduct.put("cs_Evap_Fouling_factor", dj.getContentString(document,xpathstr));
xpathstr="/report/job/product["+i+"]/performanceData/group[name='Evaporator']/col2/field[contains(name,'Leaving fluid temp')]/value";
mproduct.put("cs_Evap_Leaving_fluid_temp", dj.getContentString(document,xpathstr));
xpathstr="/report/job/product["+i+"]/performanceData/group[name='Evaporator']/col1/field[contains(name,'Fluid')]/value"; String fluidstr=dj.getContentString(document,xpathstr);
if(fluidstr!=""&& !fluidstr.equals("")&& fluidstr.contains("%")){
fluidstr=fluidstr.substring(fluidstr.indexOf("%")+)+" / "+fluidstr.substring(, fluidstr.indexOf("%")+);
}
mproduct.put("cs_Evap_Fluid",fluidstr ); //mproduct.put("cs_Evap_Fluid", dj.getContentString(document,xpathstr)); xpathstr="/report/job/product["+i+"]/performanceData/group[name='Evaporator']/col1/field[contains(name,'Number of passes')]/value";
mproduct.put("cs_Evap_Number_of_passes", dj.getContentString(document,xpathstr)); //cond 3 xpathstr="/report/job/product["+i+"]/performanceData/group[name='Condenser']/col1/field[contains(name,'Design ambient')]/value";
mproduct.put("cs_Cond_Design_ambient", dj.getContentString(document,xpathstr)); xpathstr="/report/job/product["+i+"]/performanceData/group[name='Condenser']/col2/field[contains(name,'Entering fluid temp')]/value";
mproduct.put("cs_Cond_Entering_fluid_temp", dj.getContentString(document,xpathstr));
xpathstr="/report/job/product["+i+"]/performanceData/group[name='Condenser']/col2/field[contains(name,'Fluid flow rate')]/value";
mproduct.put("cs_Cond_Fluid_flow_rate", dj.getContentString(document,xpathstr));
xpathstr="/report/job/product["+i+"]/performanceData/group[name='Condenser']/col2/field[contains(name,'Fouling factor')]/value";
mproduct.put("cs_Cond_Fouling_factor", dj.getContentString(document,xpathstr));
xpathstr="/report/job/product["+i+"]/performanceData/group[name='Condenser']/col2/field[contains(name,'Leaving fluid temp')]/value";
mproduct.put("cs_Cond_Leaving_fluid_temp", dj.getContentString(document,xpathstr));
xpathstr="/report/job/product["+i+"]/performanceData/group[name='Condenser']/col2/field[contains(name,'Fluid')]/value";
mproduct.put("cs_Cond_Fluid", dj.getContentString(document,xpathstr));
xpathstr="/report/job/product["+i+"]/performanceData/group[name='Condenser']/col2/field[contains(name,'Number of passes')]/value";
mproduct.put("cs_Cond_Number_of_passes", dj.getContentString(document,xpathstr)); //4 Cooling Conditions of service
//<name>Capacity (Tons)</name>
xpathstr="/report/job/product["+i+"]/performanceData/group[name='Conditions of service']/col1/field[contains(name,'Capacity')]/value";
mproduct.put("cs_Capacity", dj.getContentString(document,xpathstr));
//<name>Energy efficiency (kW/Ton)</name>
xpathstr="/report/job/product["+i+"]/performanceData/group[name='Conditions of service']/col2/field[contains(name,'Compressor power')]/value";
mproduct.put("cs_Energy_efficiency", dj.getContentString(document,xpathstr)); //5<name>Unit power supply</name>
xpathstr="/report/job/product["+i+"]/performanceData/group[name='Electrical characteristics']/col1/field[contains(name,'Unit power supply')]/value";
mproduct.put("cs_Unit_power_supply", dj.getContentString(document,xpathstr)); //6<name>Refrigerant</name>//<value>R134a</value>
xpathstr="/report/job/product["+i+"]/performanceData/group[name='Physical Specifications']/col2/field[contains(name,'Refrigerant')]/value";
mproduct.put("cs_Refrigerant", dj.getContentString(document,xpathstr)); //add pictureleftheader
String pic_left_header=TEMPLATES_PATH+"leftheader-cs.JPG";
//pic_left_header=pic_left_header.replaceAll("file:/", "");
mproduct.put("cs_pic_left_header", pic_left_header); mproduct.putAll((Map) mCheckSheetAutoFill.get(i));
pl.add(mproduct);
}
return pl;
} // public static void main(String[] args) throws Exception {
// Dom4jXml dj = new Dom4jXml();
// AddCheckSheet acs=new AddCheckSheet();
// SAXReader saxReader = new SAXReader();
// Document document;
// try {
// document = saxReader.read(new File("D:/Task/draw_cs3/20141218/wcfx36_more.xml"));
//
// acs.CheckSheetReadXml(dj.parse(document));
// } catch (DocumentException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
}

2

 package ec.utils;

 import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map; import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader; public class Dom4jXml { static boolean debugf=true; public static void main(String[] args) throws Exception {
Dom4jXml dj = new Dom4jXml();
SAXReader saxReader = new SAXReader();
Document document;
try {
document = saxReader.read(new File("D:/Task/draw_cs3/20141218/wcfx36_more.xml")); dj.ReadXml(parse(document));
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} //for checkSheet Print
public List ReadXml(org.w3c.dom.Document doc) throws Exception{
Document document=parse(doc);
List pl=new ArrayList(); //count product num
List list = document.selectNodes("/report/job/product");
int num = list.size(); for(int i=;i<=num;i++)
{
Map mproduct=new HashMap();
//0 select_date
String xpathstr="/report/job/@select_date";
mproduct.put("cs_Date", getContentString(document,xpathstr));
//model 1 AT3ARF1R AT5CRF4R
xpathstr="/report/job/product["+i+"]/performanceData/group[name='Screw Compressor Water Cooled Package Chiller']/row/field[name='Model #']/value";
String s1=getContentString(document,xpathstr);
xpathstr="/report/job/product["+i+"]/performanceData/group[name='Screw Compressor Water Cooled Package Chiller']/row/field[name='Component code']/value";
mproduct.put("cs_Model", s1+getContentString(document,xpathstr)); //evap 2
xpathstr="/report/job/product["+i+"]/performanceData/group[name='Evaporator_Condenser']/col1/field[contains(name,'Entering fluid temp')]/value";
mproduct.put("cs_Evap_Entering_fluid_temp", getContentString(document,xpathstr));
xpathstr="/report/job/product["+i+"]/performanceData/group[name='Evaporator_Condenser']/col1/field[contains(name,'Fluid flow rate')]/value";
mproduct.put("cs_Evap_Fluid_flow_rate", getContentString(document,xpathstr));
xpathstr="/report/job/product["+i+"]/performanceData/group[name='Evaporator_Condenser']/col1/field[contains(name,'Fouling factor')]/value";
mproduct.put("cs_Evap_Fouling_factor", getContentString(document,xpathstr));
xpathstr="/report/job/product["+i+"]/performanceData/group[name='Evaporator_Condenser']/col1/field[contains(name,'Leaving fluid temp')]/value";
mproduct.put("cs_Evap_Leaving_fluid_temp", getContentString(document,xpathstr));
xpathstr="/report/job/product["+i+"]/performanceData/group[name='Evaporator_Condenser']/col1/field[contains(name,'Fluid')]/value";
mproduct.put("cs_Evap_Fluid", getContentString(document,xpathstr));
xpathstr="/report/job/product["+i+"]/performanceData/group[name='Evaporator_Condenser']/col1/field[contains(name,'Number of passes')]/value";
mproduct.put("cs_Evap_Number_of_passes", getContentString(document,xpathstr)); //cond 3
xpathstr="/report/job/product["+i+"]/performanceData/group[name='Evaporator_Condenser']/col2/field[contains(name,'Entering fluid temp')]/value";
mproduct.put("cs_Cond_Entering_fluid_temp", getContentString(document,xpathstr));
xpathstr="/report/job/product["+i+"]/performanceData/group[name='Evaporator_Condenser']/col2/field[contains(name,'Fluid flow rate')]/value";
mproduct.put("cs_Cond_Fluid_flow_rate", getContentString(document,xpathstr));
xpathstr="/report/job/product["+i+"]/performanceData/group[name='Evaporator_Condenser']/col2/field[contains(name,'Fouling factor')]/value";
mproduct.put("cs_Cond_Fouling_factor", getContentString(document,xpathstr));
xpathstr="/report/job/product["+i+"]/performanceData/group[name='Evaporator_Condenser']/col2/field[contains(name,'Leaving fluid temp')]/value";
mproduct.put("cs_Cond_Leaving_fluid_temp", getContentString(document,xpathstr));
xpathstr="/report/job/product["+i+"]/performanceData/group[name='Evaporator_Condenser']/col2/field[contains(name,'Fluid')]/value";
mproduct.put("cs_Cond_Fluid", getContentString(document,xpathstr));
xpathstr="/report/job/product["+i+"]/performanceData/group[name='Evaporator_Condenser']/col2/field[contains(name,'Number of passes')]/value";
mproduct.put("cs_Cond_Number_of_passes", getContentString(document,xpathstr)); //4 Cooling Conditions of service
//<name>Capacity (Tons)</name>
xpathstr="/report/job/product["+i+"]/performanceData/group[name='Cooling Conditions of service']/col1/field[contains(name,'Capacity')]/value";
mproduct.put("cs_Capacity", getContentString(document,xpathstr));
//<name>Energy efficiency (kW/Ton)</name>
xpathstr="/report/job/product["+i+"]/performanceData/group[name='Cooling Conditions of service']/col2/field[contains(name,'Energy efficiency')]/value";
mproduct.put("cs_Energy_efficiency", getContentString(document,xpathstr)); //5<name>Unit power supply</name>
xpathstr="/report/job/product["+i+"]/performanceData/group[name='Electrical characteristics']/col1/field[contains(name,'Unit power supply')]/value";
mproduct.put("cs_Unit_power_supply", getContentString(document,xpathstr)); //6<name>Refrigerant</name>//<value>R134a</value>
xpathstr="/report/job/product["+i+"]/performanceData/group[name='Physical Specifications']/col2/field[contains(name,'Refrigerant')]/value";
mproduct.put("cs_Refrigerant", getContentString(document,xpathstr)); //getContent(document,xpathstr); pl.add(mproduct);
}
return pl;
} public static Document parse(org.w3c.dom.Document doc) throws Exception {
if (doc == null) {
return (null);
}
org.dom4j.io.DOMReader xmlReader = new org.dom4j.io.DOMReader();
return (xmlReader.read(doc));
} public static org.w3c.dom.Document parse(Document doc) throws Exception {
if (doc == null) {
return (null);
}
java.io.StringReader reader = new java.io.StringReader(doc.asXML());
org.xml.sax.InputSource source = new org.xml.sax.InputSource(reader);
javax.xml.parsers.DocumentBuilderFactory documentBuilderFactory = javax.xml.parsers.DocumentBuilderFactory
.newInstance();
javax.xml.parsers.DocumentBuilder documentBuilder = documentBuilderFactory
.newDocumentBuilder();
return (documentBuilder.parse(source));
} public String getContentString(Document document,String xpathstr){ List list = document.selectNodes(xpathstr);
String result=""; Iterator iter = list.iterator();
iter = list.iterator();
if (iter.hasNext()) { Object o=iter.next();
if(o instanceof Attribute){
Attribute attribute = (Attribute) o;
//hm.put(attribute.getName(),attribute.getValue());
result=attribute.getValue();
if(debugf){
System.out.println(attribute.getName()+":"+attribute.getValue());
} }
if(o instanceof Element){
Element element = (Element) o;
String name = element.getName();
String value = element.getText();
//hm.put(name, value);
result=value;
if(debugf){
System.out.println(name+":"+value);
}
}
} else {
return result;
}
return result;
}
public Map<String,String> getContent(Document document,String xpathstr){ List list = document.selectNodes(xpathstr); Map<String,String> hm=new HashMap();
Iterator iter = list.iterator();
iter = list.iterator();
if (iter.hasNext()) { Object o=iter.next();
if(o instanceof Attribute){
Attribute attribute = (Attribute) o;
hm.put(attribute.getName(),attribute.getValue());
if(debugf){
System.out.println(attribute.getName()+":"+attribute.getValue());
} }
if(o instanceof Element){
Element element = (Element) o;
String name = element.getName();
String value = element.getText();
hm.put(name, value);
if(debugf){
System.out.println(name+":"+value);
}
}
} else { }
return hm;
} }