Dom4j解析Xml与Dom解析Xml

二种方式比较:Dom4j易用强大,Dom易懂繁杂;

下面贴出二中解析方式的代码:





     smtp.qq.com 
     25  
     smtp   
     UserName  
     123456789  
     [email protected] 



 
    
         com.fix.ciss.quartz.job.BillChnageJobBillChnageJobBillChnageGroup
         BillChnageTriggerBillChnageGroup0 0 23 * * ?
    
    
         com.fix.ciss.quartz.job.BrithJobBrithJobBrithGroup
         BrithTriggerBrithGroup0 30 0 1 * ?
    
    
         com.fix.ciss.quartz.job.RepaymentJobRepaymentJobRepaymentGroup
         RepaymentTriggerRepaymentGroup0 30 23 * * ?
    
    
         com.fix.ciss.quartz.job.CalendarJobCalendarJobJobCalendarJobGroup
         CalendarTriggerCalendarGroup0 0 1 1 * ?
    
    
         com.fix.ciss.quartz.job.TaskToSendJobTaskToSendJobTaskToSendGroup
         TaskToSendTriggerTaskToSendGroup0 40 0 * * ?
    
    
         com.fix.ciss.quartz.job.FtpJobFtpJobFtpGroup
         FtpTriggerFptGroup0 50 0 * * ?
    
  


第一种:Dom解析:

package com.fix.ciss.mail;

import java.io.FileInputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 * 解析MailSendInfo.xml文件
 * @author pengpan
 * 修改日期:2012/05/23
 */
public class MailXmlHelp {
     
	
	private Document getDocument() throws Exception {
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = dbf.newDocumentBuilder();
		FileInputStream fis = new FileInputStream(MailXmlHelp.class.getResource("").getPath().replaceAll("%20", " ")+"MailSendInfo.xml"); //注:MailSendInfo.xml与当前Java文件在同一目录
		Document doc = db.parse(fis);
		return doc;
	}

    /**
     * 解析
     * @return
     */
	public MailSenderInfo getMailSenderInfo() {
		MailSenderInfo mailSendObj = new MailSenderInfo();
		try {
			Document doc = getDocument();
			NodeList nodeList = doc.getElementsByTagName("MailInfo");
			for(int i=0;i

            
 
  

第二种:Dom4j 解析:

package com.fix.ciss.mail;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.fix.ciss.obj.CronTriggerObj;
import com.fix.ciss.obj.JobDetailObj;
import com.fix.ciss.obj.SchdeulerObj;

public class SchedulersXmlHelp {
     private Document getDocument(String path){
    	 SAXReader saxReader = new SAXReader();
    	 Document doc = null;
    	 try {
			doc = saxReader.read(new File(path));
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			System.out.println("读取文档异常");
			e.printStackTrace();
		}
		return doc;
     }
     
     /**
      * 解析Schedulers
      * @return
      */
     public  List getSchedulers(){
    	 List list =new ArrayList();
    	 SchdeulerObj schObj = null;
    	 JobDetailObj detailObj = null;
    	 CronTriggerObj cronTrig = null;
    	 Document doc = getDocument(SchedulersXmlHelp.class.getResource("").getPath().replaceAll("%20", " ")+"MailSendInfo.xml");
    	 Iterator root = doc.getRootElement().element("Schedulers").elementIterator();
    	 while(root.hasNext()){
    		 Element element = (Element)root.next();
    		 schObj = new SchdeulerObj();
    		 Iterator iterJob = element.elementIterator("JobDetail"); //子节点JobDetail集合

    		 while(iterJob.hasNext()){
    			 Element eleJob=(Element)iterJob.next();
    			 detailObj =new JobDetailObj();
    			 detailObj.setClassName(eleJob.elementTextTrim("ClassName"));
    			 detailObj.setGroup(eleJob.elementTextTrim("Group"));
    			 detailObj.setName(eleJob.elementTextTrim("Name"));
    		 }
    		 
    		 Iterator iterTrigger = element.elementIterator("CronTrigger");
    		 while(iterTrigger.hasNext()){
    			 cronTrig = new CronTriggerObj();
    			 Element eleTrigger=(Element)iterTrigger.next();
    			 cronTrig.setName(eleTrigger.elementTextTrim("Name"));
    			 cronTrig.setGroup(eleTrigger.elementTextTrim("Group"));
    			 cronTrig.setCronExpression(eleTrigger.elementTextTrim("CronExpression"));
    		 }
    		 schObj.setJob(detailObj);
    		 schObj.setCron(cronTrig);
    		 list.add(schObj);
    	 }
    	 return list;
     }
}

            

你可能感兴趣的:(J2EE)