JAVA读取XML文件

 

XMLTodayReportPar.java

/*
 * Created on 2005-12-27
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.ldtec.todayReport.util;

import java.util.Iterator;
import java.util.List;
import java.io.*;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import com.ldtec.todayReport.data.*;

/**
 * @author JOE
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class XMLTodayReportPar {
 
 private static final String TODAYREPORTS = "TodayReports";
 private static final String TODAYREPORT = "TodayReport";
 private static final String ID = "id";
 private static final String NAME = "name";
 private static final String REPORT = "Report";
 private static final String TABLENAME = "tableName";
 private static final String DESCRIPTION = "description";
 private static final String HEAD = "Head";
 private static final String BODY = "Body";
 private static final String STATISTICAL = "Statistical";
 private static final String FORWARD = "Forward";
 private static final String URL = "url";
 
 private static XMLTodayReports xmltodayReports = null;
 
 /**
  * 返回所有的表
  * @return XMLTodayReports
  */
    String realpath = "";
 public  XMLTodayReports getXMLTodayReports(String realpath) {
       
  this.realpath = realpath;
  if (xmltodayReports != null) return xmltodayReports;
  else {
   xmltodayReports = new XMLTodayReports();
  try {
  SAXBuilder builder = new SAXBuilder();//注册解析器
  Element root = null;
  
     String path = realpath + "WEB-INF//i2conf//todayReport//todayReport.xml" ;
  //String path = "D://Development//TOMCAT//webapps//rebis//WEB-INF//i2conf//todayReport//todayReport.xml";
        System.out.println("getrealpath = "+path);
  //Document doc = builder.build(XMLTodayReports.getClass().getResourceAsStream("..//..//..//..//..//i2conf//report//ReportDefine.xml"));
        Document doc = builder.build(new File(path));//创建文档,读入XML文件
  root = doc.getRootElement();
  getTodayReports(root, xmltodayReports);

  return xmltodayReports;
  }
  catch (Exception e) {
  e.printStackTrace();
  }
  return null;
  }
 }
 
 /**
  * 根据表名返回要找的报表信息.
  * @param tableName
  * @return XMLTable
  */
 public  XMLTodayReport getXMLTodayReport( String id ) {
  if (id != "") {
  return (XMLTodayReport) getXMLTodayReports(realpath).getTodayReportByID(id);
  } else return null;
 }
 
 /**
  * 返回TodayReport信息
  * @return
  */
 public List getAllValue(){
   return (List) getXMLTodayReports(realpath).getAllValue();
 }
 
 /**
  * @param todayReportsElem
  * @param xmltodayReports
  */
 public  void getTodayReports( Element todayReportsElem , XMLTodayReports xmltodayReports) {
  List todayReportList = todayReportsElem.getChildren(TODAYREPORT);
  for (Iterator it = todayReportList.iterator(); it.hasNext();) {
  Element todayReportEle = (Element) it.next();
  XMLTodayReport xmltodayReport = new XMLTodayReport();
  getTodayReport(todayReportEle, xmltodayReport);
  xmltodayReports.addXMLTodayReport(xmltodayReport);
  }
 }
 
 /**获取TodayReport节点
  * @param todayReportElem
  * @param xmltodayReport
  */
 public void getTodayReport( Element todayReportElem , XMLTodayReport xmltodayReport ) {
  xmltodayReport.setID(todayReportElem.getAttributeValue(ID));
  xmltodayReport.setName(todayReportElem.getAttributeValue(NAME));
  
  List todayReportList = todayReportElem.getChildren(REPORT);
  for (Iterator it = todayReportList.iterator(); it.hasNext();) {
  Element ReportEle = (Element) it.next();
  XMLReport xmlReport = new XMLReport();
  getReport(ReportEle, xmlReport);
  xmltodayReport.addXMLReport(xmlReport);
  }
  /**
  XMLReport xmlReport = new XMLReport();
  getReport(todayReportElem.getChild(REPORT),xmlReport);
  xmltodayReport.setXmlReport(xmlReport);
  */
 }
 
 /**获取Report节点的值
  * @param ReportElem
  * @param xmlreport
  */
 private void getReport(Element ReportElem , XMLReport xmlreport ) {
  xmlreport.setTableName(ReportElem.getAttributeValue(TABLENAME));
  xmlreport.setDescription(ReportElem.getAttributeValue(DESCRIPTION));
  
  XMLHead xmlHead = new XMLHead();
  if(ReportElem.getChild(HEAD)==null){
   xmlHead.setHeadSQLValue("null");
  }else{
   getHead(ReportElem.getChild(HEAD),xmlHead); 
  } 
  xmlreport.setXmlHead(xmlHead);

  XMLBody xmlBody = new XMLBody();
  getBody(ReportElem.getChild(BODY),xmlBody);
  xmlreport.setXmlBody(xmlBody);   

  XMLStatistical xmlStatistical = new XMLStatistical();
  if(ReportElem.getChild(STATISTICAL)==null){
   xmlStatistical.setStatisticalSQLValue("null");
  }else{
   getStatistical(ReportElem.getChild(STATISTICAL),xmlStatistical);
  }
  xmlreport.setXmlStatistical(xmlStatistical); 
  
  XMLForward xmlForward = new XMLForward();
  getForward(ReportElem.getChild(FORWARD), xmlForward);
  xmlreport.setXmlForward(xmlForward);
  
  }
   
    /**
  * 获取Head节点的属性值
  * @param HeadElem
  * @param xmlHead
  */
 private void getHead( Element HeadElem , XMLHead xmlHead ) {
  xmlHead.setHeadSQLValue(HeadElem.getTextTrim());
 }
 
 /**
  * 获取Body节点的属性值
  * @param BodyElem
  * @param xmlBody
  */
 private void getBody( Element BodyElem , XMLBody xmlBody ) {
  xmlBody.setBodySQLValue(BodyElem.getTextTrim());
 }
 
 /**
  * 获取Statistical节点的属性值
  * @param StatisticalElem
  * @param xmlStatistical
  */
 private void getStatistical( Element StatisticalElem , XMLStatistical xmlStatistical ) {
  xmlStatistical.setStatisticalSQLValue(StatisticalElem.getTextTrim());
 }
 
 /**
  * 获取Forward节点的属性值
  * @param ForwardElem
  * @param xmlForward
  */
 private void getForward( Element ForwardElem , XMLForward xmlForward ) {
  xmlForward.setUrl(ForwardElem.getAttributeValue(URL));
 }

 public static void main(String[] args){
  XMLTodayReportPar par = new XMLTodayReportPar();
  XMLTodayReport todayReport = par.getXMLTodayReport("1");
  System.out.println("eeee"+todayReport.getName());
   
  List todayReportList = todayReport.getTodayReportList();
  for (Iterator iter = todayReportList.iterator(); iter.hasNext();) {

  XMLReport xmlReport = (XMLReport) iter.next();
  System.out.println("pppp"+xmlReport.getDescription());
  System.out.println("qqqq"+xmlReport.getXmlHead().getHeadSQLValue());
  }
 }
}

TodayReport.java

/*
 * Created on 2005-12-29
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.ldtec.todayReport.data;

import java.io.Serializable;
import java.util.* ;

/**
 * @author JOE
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class TodayReport implements Serializable{
 
 private String id;
    private String name;
    private Vector report = new Vector();
   
    public String getID()
    {
            return id;
    }
    public void setID(String id)
    {
            this.id = id;
    }
    public String getName()
    {
            return name;
    }
    public void setName(String name)
    {
            this.name = name;
    }
   
    public Vector getReport()
    {
         return report;
    }
    public void setReport(Vector report)
    {
         this.report = report;
    }

}

XMLTodayReports.java

/*
 * Created on 2005-12-27
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.ldtec.todayReport.data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//import com.ldtec.todayReport.data.*;

/**
 * @author JOE
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class XMLTodayReports implements Serializable{
 
 private Map todayReportsMap = new HashMap();//实现了将惟一键映射到特定的值上
 List HeadSQLValueList = new ArrayList();
 List TodayReportValueList = new ArrayList();
 List ReportValueList = new ArrayList();
 
 public Map getTodayReportsMap() {
  return this.todayReportsMap;
 }
 
 public void setTodayReportsMap( Map todayReportsMap ) {
  this.todayReportsMap = todayReportsMap;
 }
 
 public void addXMLTodayReport(XMLTodayReport xmltodayReport){
  todayReportsMap.put(xmltodayReport.getID(),xmltodayReport);//put(object key,object value);
                                             //key=tablename,value=tablename对应的FORMBEAN
 }
 
 public XMLTodayReport getTodayReportByID(String id)
 {
  if(id!=null){
   return (XMLTodayReport)todayReportsMap.get(id);
  }else{
   return null;
  }
 }
 
 public List getAllValue(){
  if(TodayReportValueList.size()!=0){
   TodayReportValueList.clear();
  }
  Object[] keys = todayReportsMap.keySet().toArray();
        for(int i = 0; i < keys.length; i ++){
         XMLTodayReport todayReport =(XMLTodayReport) todayReportsMap.get(keys[i]);
         TodayReportValueList.add(todayReport);
        }
        return TodayReportValueList;
 }
}

XMLTodayReport.java

/*
 * Created on 2005-12-27
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.ldtec.todayReport.data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.ldtec.newreport.data.XMLCondition;
import com.ldtec.newreport.data.XMLConditions;
import com.ldtec.newreport.data.XMLField;
import com.ldtec.todayReport.data.*;

/**
 * @author JOE
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class XMLTodayReport{
 
 private String id;
 private String name;
 private List TodayReportList = new ArrayList();
 private XMLReport xmlReport;
 private XMLTodayReport xmlTodayReport;
 
 public String getID() {
  return id;
 }
 public void setID(String id) {
  this.id = id;
 }
 
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 
 public List getTodayReportList() {
  return this.TodayReportList;
 }
 public void setTodayReportList( List todayReportList ) {
  this.TodayReportList = todayReportList;
 }
 public void addXMLReport(XMLReport xmlReport){
  TodayReportList.add(xmlReport);
 }
}

XMLBody.java

/*
 * Created on 2005-12-27
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.ldtec.todayReport.data;

import java.io.Serializable;
import com.ldtec.todayReport.data.*;
/**
 * @author JOE
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class XMLBody implements Serializable{
 
 private String BodySQLValue;
 
 public String getBodySQLValue(){
  return BodySQLValue;
 }
 public void setBodySQLValue(String BodySQLValue){
  this.BodySQLValue = BodySQLValue;
 }
 
}

XMLReport .java

/*
 * Created on 2005-12-27
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.ldtec.todayReport.data;

import java.io.Serializable;

import com.ldtec.todayReport.data.*;
/**
 * @author JOE
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class XMLReport implements Serializable{
 
 private String tableName;
 private String description;
 private XMLHead xmlHead;
 private XMLBody xmlBody;
 private XMLStatistical xmlStatistical;
 private XMLForward xmlForward;
 
 public String getTableName() {
  return tableName;
 }
 public void setTableName(String tableName) {
  this.tableName = tableName;
 }
 
 public String getDescription(){
  return description;
 }
 public void setDescription(String description){
  this.description = description;
 }
 
 public XMLHead getXmlHead() {
  return xmlHead;
 }
 public void setXmlHead(XMLHead xmlHead) {
  this.xmlHead = xmlHead;
 }
 
 public XMLBody getXmlBody() {
  return xmlBody;
 }
 public void setXmlBody(XMLBody xmlBody) {
  this.xmlBody = xmlBody;
 }
 
 public XMLStatistical getXmlStatistical() {
  return xmlStatistical;
 }
 public void setXmlStatistical(XMLStatistical xmlStatistical) {
  this.xmlStatistical = xmlStatistical;
 }
 
 public XMLForward getXmlForward() {
  return xmlForward;
 }
 public void setXmlForward(XMLForward xmlForward) {
  this.xmlForward = xmlForward;
 }

}

XMLHead.java

/*
 * Created on 2005-12-27
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.ldtec.todayReport.data;

import java.io.Serializable;
import com.ldtec.todayReport.data.*;
/**
 * @author JOE
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class XMLHead implements Serializable{
 
 private String HeadSQLValue;
 
 public String getHeadSQLValue(){
  return HeadSQLValue;
 }
 public void setHeadSQLValue(String HeadSQLValue){
  this.HeadSQLValue = HeadSQLValue;
 }
 
}

XML文件:

<?xml version="1.0" encoding="GB2312"?>

   <TodayReports>

      <TodayReport id="1" name="人员统计表">

      <Report tableName="DTPEPOLE_REMAIN_DOWN_ROLL" description="井下现有人员统计表">

   <Head>
      <![CDATA[SELECT  "COMMUNITY_NAME" FROM "DTPEPOLE_REMAIN_DOWN_ROLL" WHERE (GETDATE()-A_P_P_CREATED_DATE)>0 AND (GETDATE()-A_P_P_CREATED_DATE)<1]]>
   </Head>
   
   <Body>
      <![CDATA[SELECT  "UNIT","PLACE","TOTAL","REPORT_PEOPLE" FROM "DTPEPOLE_REMAIN_DOWN_ROLL" WHERE (GETDATE()-A_P_P_CREATED_DATE)>0 AND (GETDATE()-A_P_P_CREATED_DATE)<1]]>
   </Body>

   <Statistical>
      <![CDATA[SELECT sum(total) FROM "DTPEPOLE_REMAIN_DOWN_ROLL" WHERE (GETDATE()-A_P_P_CREATED_DATE)>0 AND (GETDATE()-A_P_P_CREATED_DATE)<1]]>
   </Statistical>

   <Forward url="DOWN_ROLL"/>

      </Report>

   </TodayReport>

<TodayReports>

你可能感兴趣的:(JAVA读取XML文件)