dom4j解析xml

借鉴地址:https://www.cnblogs.com/black-spike/p/9776180.html 

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class PaseXml {
	private static final Log logger = LogFactory.getLog(PaseXml.class);

	private SendResult paseResut(String xml){
		// 模拟数据
		xml=""+ 					
				""+					
						""+
						""+
                "";
		
		Document doc = null;
		SendResult result = new SendResult();
		// 将字符串转为XML
        try {
			doc = DocumentHelper.parseText(xml);
	        // 获取根节点
	        Element rootElt = doc.getRootElement();
	        //获取type
	        String type = rootElt.attributeValue("type");
	        result.setType(type);
	        //获取Message节点
	        Iterator message = rootElt.elementIterator("Message");
	        List msgs = new ArrayList();
	        result.setMesgs(msgs);
	        Message msg ;
            while (message.hasNext()) {
            	msg = new Message();
	           	Element itemAtr = (Element) message.next();
	            //获取需要的数据
                String smsID = itemAtr.attributeValue("SmsID");
                String recvNumber = itemAtr.attributeValue("RecvNumber");
                String code = itemAtr.attributeValue("Code");
                msg.setSmsID(smsID);
                msg.setRecvNumber(recvNumber);
                msg.setCode(code);
                msgs.add(msg);
           }
		} catch (DocumentException e) {
			logger.error("请求返回信息解析失败,xml="+xml,e);
		}
		return result;

	}
	
	
	/**
	 * SMS
	 * @author Administrator
	 *
	 */
	class SendResult{
		private String type;
		List mesgs;
		public String getType() {
			return type;
		}
		public void setType(String type) {
			this.type = type;
		}
		public List getMesgs() {
			return mesgs;
		}
		public void setMesgs(List mesgs) {
			this.mesgs = mesgs;
		}
	}
	
	/**
	 * Message
	 * @author Administrator
	 *
	 */
	class Message{
		private String smsID;
		private String recvNumber;
		private String code;
		
		public String getSmsID() {
			return smsID;
		}
		public void setSmsID(String smsID) {
			this.smsID = smsID;
		}
		public String getRecvNumber() {
			return recvNumber;
		}
		public void setRecvNumber(String recvNumber) {
			this.recvNumber = recvNumber;
		}
		public String getCode() {
			return code;
		}
		public void setCode(String code) {
			this.code = code;
		}
		
	}
	public static void main(String[] args) {
		PaseXml p = new PaseXml();
		SendResult paseResut = p.paseResut("");
		String type = paseResut.getType();
		System.out.println("type:"+type);
		List mesgs = paseResut.getMesgs();
		for (Message message : mesgs) {
			String code = message.getCode();
			String smsID = message.getSmsID();
			String recvNumber = message.getRecvNumber();
			System.out.println("code="+code+"smsID="+smsID+"recvNumber="+recvNumber);
		}

	}

}

 

你可能感兴趣的:(工具类,dom4j,xml)