Dom4j解析Xml与Dom解析Xml

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

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


<?xml version="1.0" encoding="UTF-8"?>
<Root>
<MailInfo>
     <ServierHost>smtp.qq.com</ServierHost> <!-- 发送邮件的服务器IP -->
     <ServerPost>25</ServerPost>  <!-- 发送邮件的服务器端口 -->
     <Protocol>smtp</Protocol>   <!-- 协议 -->
     <UserName>UserName</UserName>  <!-- 发送用户名 -->
     <Password>123456789</Password>  <!-- 发送邮件密码 -->
     <SendMailAddress>[email protected]</SendMailAddress> <!-- 发送邮箱地址 -->
</MailInfo>

<!-- 任务加载配置 -->
 <Schedulers>
    <schdeuler>
         <JobDetail><ClassName>com.fix.ciss.quartz.job.BillChnageJob</ClassName><Name>BillChnageJob</Name><Group>BillChnageGroup</Group></JobDetail>
         <CronTrigger><Name>BillChnageTrigger</Name><Group>BillChnageGroup</Group><CronExpression>0 0 23 * * ?</CronExpression></CronTrigger>
    </schdeuler>
    <schdeuler>
         <JobDetail><ClassName>com.fix.ciss.quartz.job.BrithJob</ClassName><Name>BrithJob</Name><Group>BrithGroup</Group></JobDetail>
         <CronTrigger><Name>BrithTrigger</Name><Group>BrithGroup</Group><CronExpression>0 30 0 1 * ?</CronExpression></CronTrigger>
    </schdeuler>
    <schdeuler>
         <JobDetail><ClassName>com.fix.ciss.quartz.job.RepaymentJob</ClassName><Name>RepaymentJob</Name><Group>RepaymentGroup</Group></JobDetail>
         <CronTrigger><Name>RepaymentTrigger</Name><Group>RepaymentGroup</Group><CronExpression>0 30 23 * * ?</CronExpression></CronTrigger>
    </schdeuler>
    <schdeuler>
         <JobDetail><ClassName>com.fix.ciss.quartz.job.CalendarJob</ClassName><Name>CalendarJobJob</Name><Group>CalendarJobGroup</Group></JobDetail>
         <CronTrigger><Name>CalendarTrigger</Name><Group>CalendarGroup</Group><CronExpression>0 0 1 1 * ?</CronExpression></CronTrigger>
    </schdeuler>
    <schdeuler>
         <JobDetail><ClassName>com.fix.ciss.quartz.job.TaskToSendJob</ClassName><Name>TaskToSendJob</Name><Group>TaskToSendGroup</Group></JobDetail>
         <CronTrigger><Name>TaskToSendTrigger</Name><Group>TaskToSendGroup</Group><CronExpression>0 40 0 * * ?</CronExpression></CronTrigger>
    </schdeuler>
    <schdeuler>
         <JobDetail><ClassName>com.fix.ciss.quartz.job.FtpJob</ClassName><Name>FtpJob</Name><Group>FtpGroup</Group></JobDetail>
         <CronTrigger><Name>FtpTrigger</Name><Group>FptGroup</Group><CronExpression>0 50 0 * * ?</CronExpression></CronTrigger>
    </schdeuler>
  </Schedulers>
</Root>


第一种: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;
	}

    /**
     * 解析<MailInfo>
     * @return
     */
	public MailSenderInfo getMailSenderInfo() {
		MailSenderInfo mailSendObj = new MailSenderInfo();
		try {
			Document doc = getDocument();
			NodeList nodeList = doc.getElementsByTagName("MailInfo");
			for(int i=0;i<nodeList.getLength();i++){
				Element ele = ((Element)nodeList.item(i));
				String servierHost = ele.getElementsByTagName("ServierHost").item(0).getTextContent();  
				String serverPost = ele.getElementsByTagName("ServerPost").item(0).getTextContent();
				String protocol = ele.getElementsByTagName("Protocol").item(0).getTextContent();
				String userName = ele.getElementsByTagName("UserName").item(0).getTextContent();
				String password = ele.getElementsByTagName("Password").item(0).getTextContent();
				String sendMailAddress = ele.getElementsByTagName("SendMailAddress").item(0).getTextContent();
				mailSendObj.setMailServerHost(servierHost);
				mailSendObj.setMailServerPost(Integer.parseInt(serverPost));
				mailSendObj.setProtocol(protocol);
				mailSendObj.setUserName(userName);
				mailSendObj.setPassword(password);
				mailSendObj.setFromAddress(sendMailAddress);
			}
		} catch (Exception e) {
            e.printStackTrace();
		}
		return mailSendObj;
	}
}

            

第二种: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<SchdeulerObj> getSchedulers(){
    	 List<SchdeulerObj> list =new ArrayList<SchdeulerObj>();
    	 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;
     }
}

            

你可能感兴趣的:(xml,exception,String,null,iterator,encoding)