使用Dom4j来解析xml格式字符串

jsp页面输入的xml格式如下所示:


         
		
			20151103000000101
			ICBC
			
			工商银行
			工商银行北京市朝阳支行
			0.01
			张三
			9558820200001234567
			110000
			110000
			SOURCE
			
			
			0
			
			
			
		
		
			20151103000000102
			ICBC
			 
			工商银行
			工商银行北京市朝阳支行
			0.01
			李四
			9558820200001234568
			110000
			110000
			SOURCE
			
			
          		0
			
			
			
		
	

对上述代码进行读取:


		request.setCharacterEncoding("UTF-8");
		String xml = formatString(request.getParameter("xml"));
		Map xmlMap = new HashMap();
		//第一步:将请求的数据和商户自己的密钥拼成一个字符串,
		Document document = null;
		try {
			document=DocumentHelper.parseText(xml);
		} catch (DocumentException e) {
		} 
		Element rootEle = document.getRootElement();
		System.out.println("根节点名称:"+rootEle.getName());//获取根节点
		int i=0;
		Iterator iter = rootEle.elementIterator("item"); // 获取根节点下的子节点item
		while(iter.hasNext()){
			XMLBean bean = new XMLBean();
			Element recordEle = (Element) iter.next();
			bean.setOrorder_Id(recordEle.elementTextTrim("order_Id"));
			bean.setBank_Code(recordEle.elementTextTrim("bank_Code"));
			bean.setBank_Name(recordEle.elementTextTrim("bank_Name"));
			bean.setAmount(recordEle.elementTextTrim("amount"));
			bean.setAccount_Name(recordEle.elementTextTrim("account_Name"));
			bean.setAccount_Number(recordEle.elementTextTrim("account_Number"));
			bean.setFee_Type(recordEle.elementTextTrim("fee_Type"));
			bean.setUrgency(recordEle.elementTextTrim("urgency"));
			xmlMap.put(i, bean);
			i++;
		}
		System.out.print(xmlMap.size());

其中设置了和xml标签对应的类:

public class XMLBean {

	String ororder_Id;
	String bank_Code;
	String cnaps;
	String bank_Name;
	String branch_Bank_Name;
	String amount;
	String account_Name;
	String account_Number;
	String account_Type;
	String province;
	String city;
	String fee_Type;
	String payee_Email;
	String urgency;
	String payee_Mobile;
	String leave_Word;
	String abstractInfo;
	String remarksInfo;
	public String getOrorder_Id() {
		return ororder_Id;
	}
	public void setOrorder_Id(String ororder_Id) {
		this.ororder_Id = ororder_Id;
	}
	public String getBank_Code() {
		return bank_Code;
	}
	public void setBank_Code(String bank_Code) {
		this.bank_Code = bank_Code;
	}
	public String getCnaps() {
		return cnaps;
	}
	public void setCnaps(String cnaps) {
		this.cnaps = cnaps;
	}
	public String getBank_Name() {
		return bank_Name;
	}
	public void setBank_Name(String bank_Name) {
		this.bank_Name = bank_Name;
	}
	public String getBranch_Bank_Name() {
		return branch_Bank_Name;
	}
	public void setBranch_Bank_Name(String branch_Bank_Name) {
		this.branch_Bank_Name = branch_Bank_Name;
	}
	public String getAmount() {
		return amount;
	}
	public void setAmount(String amount) {
		this.amount = amount;
	}
	public String getAccount_Name() {
		return account_Name;
	}
	public void setAccount_Name(String account_Name) {
		this.account_Name = account_Name;
	}
	public String getAccount_Number() {
		return account_Number;
	}
	public void setAccount_Number(String account_Number) {
		this.account_Number = account_Number;
	}
	public String getAccount_Type() {
		return account_Type;
	}
	public void setAccount_Type(String account_Type) {
		this.account_Type = account_Type;
	}
	public String getProvince() {
		return province;
	}
	public void setProvince(String province) {
		this.province = province;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getFee_Type() {
		return fee_Type;
	}
	public void setFee_Type(String fee_Type) {
		this.fee_Type = fee_Type;
	}
	public String getPayee_Email() {
		return payee_Email;
	}
	public void setPayee_Email(String payee_Email) {
		this.payee_Email = payee_Email;
	}
	public String getUrgency() {
		return urgency;
	}
	public void setUrgency(String urgency) {
		this.urgency = urgency;
	}
	public String getPayee_Mobile() {
		return payee_Mobile;
	}
	public void setPayee_Mobile(String payee_Mobile) {
		this.payee_Mobile = payee_Mobile;
	}
	public String getLeave_Word() {
		return leave_Word;
	}
	public void setLeave_Word(String leave_Word) {
		this.leave_Word = leave_Word;
	}
	public String getAbstractInfo() {
		return abstractInfo;
	}
	public void setAbstractInfo(String abstractInfo) {
		this.abstractInfo = abstractInfo;
	}
	public String getRemarksInfo() {
		return remarksInfo;
	}
	public void setRemarksInfo(String remarksInfo) {
		this.remarksInfo = remarksInfo;
	}
	
}












你可能感兴趣的:(java,技术)