xml 代码
用Digester将XML文件读取到java中。
要导入的XML文件:
xml 代码
- xml version="1.0" encoding="GB2312"?>
- <PACKET type="RESPONSE" version="1.0" >
- <HEAD>
- <REQUEST_TYPE>50REQUEST_TYPE>
- <TYPE_CODE>1TYPE_CODE>
- <ERROR_MESSAGE>ERROR_MESSAGE>
- HEAD>
- <BODY>
- <BASE_PART>
- <CLAIM_CODE>000A00004108000002RICLAIM_CODE>
- BASE_PART>
- <CLAIM_LIST>
- <CLAIM_DATA>
- <CLAIM_COMPANY>A000CLAIM_COMPANY>
- <REPORT_NO>0001REPORT_NO>
- <CLAIM_NO>2565485CLAIM_NO>
- <POLICY_NO>C131031158978POLICY_NO>
- <ACCIDENT_TIME>2004012212ACCIDENT_TIME>
- <REPORT_TIME>2005061208REPORT_TIME>
- <ENDCASE_DATE>20050712ENDCASE_DATE>
- <UNCLAIM_AMOUNT>200.0UNCLAIM_AMOUNT>
- <CLAIM_AMOUNT>111.2CLAIM_AMOUNT>
- <STATUS>12333STATUS>
- <ACCIDENT_PLACE>AAAACCIDENT_PLACE>
- <ACCIDENT_DESCRIPTION>ADFACCIDENT_DESCRIPTION>
- <LIABILITY_AMOUNT>3LIABILITY_AMOUNT>
- CLAIM_DATA>
- <CLAIM_DATA>
- <CLAIM_COMPANY>A001CLAIM_COMPANY>
- <REPORT_NO>0002REPORT_NO>
- <CLAIM_NO>中文CLAIM_NO>
- <POLICY_NO>是不会POLICY_NO>
- <ACCIDENT_TIME>有ACCIDENT_TIME>
- <REPORT_TIME>乱REPORT_TIME>
- <ENDCASE_DATE>码ENDCASE_DATE>
- <UNCLAIM_AMOUNT>的UNCLAIM_AMOUNT>
- <CLAIM_AMOUNT>222.2CLAIM_AMOUNT>
- <STATUS>211STATUS>
- <ACCIDENT_PLACE>12ACCIDENT_PLACE>
- <ACCIDENT_DESCRIPTION>ADFACCIDENT_DESCRIPTION>
- <LIABILITY_AMOUNT>3LIABILITY_AMOUNT>
- CLAIM_DATA>
- CLAIM_LIST>
- BODY>
- PACKET>
下面是对应的Digester文件:
xml 代码
- xml version="1.0"?>
- PUBLIC "-//Jakarta Apache //DTD digester-rules XML V1.0//EN"
- "http://jakarta.apache.org/commons/digester/dtds/digester-rules.dtd">
- <digester-rules>
- <pattern value="PACKET">
- <set-properties-rule/>
- <pattern value="HEAD">
- <object-create-rule classname="com.xxxx.claim.dto.Head"/>
- <bean-property-setter-rule pattern="REQUEST_TYPE" propertyname="requestType"/>
- <bean-property-setter-rule pattern="TYPE_CODE" propertyname="typeCode"/>
- <bean-property-setter-rule pattern="ERROR_MESSAGE" propertyname="errorMessage"/>
- <set-next-rule methodname="setHead"/>
- pattern>
- <pattern value="BODY">
- <object-create-rule classname="com.xxxx.claim.dto.Body"/>
- <pattern value="BASE_PART">
- <object-create-rule classname="com.xxxx.claim.dto.BasePart"/>
- <bean-property-setter-rule pattern="CLAIM_CODE" propertyname="claimCode"/>
- <set-next-rule methodname="setBasePart"/>
- pattern>
- <pattern value="CLAIM_LIST/CLAIM_DATA">
- <object-create-rule classname="com.xxxx.claim.dto.ClaimData"/>
- <bean-property-setter-rule pattern="CLAIM_COMPANY" propertyname="claimCompany"/>
- <bean-property-setter-rule pattern="REPORT_NO" propertyname="reportNo"/>
- <bean-property-setter-rule pattern="CLAIM_NO" propertyname="claimNo"/>
- <bean-property-setter-rule pattern="POLICY_NO" propertyname="policyNo"/>
- <bean-property-setter-rule pattern="ACCIDENT_TIME" propertyname="accidentTime"/>
- <bean-property-setter-rule pattern="REPORT_TIME" propertyname="reportTime"/>
- <bean-property-setter-rule pattern="ENDCASE_DATE" propertyname="endcaseDate"/>
- <bean-property-setter-rule pattern="UNCLAIM_AMOUNT" propertyname="unclaimAmount"/>
- <bean-property-setter-rule pattern="CLAIM_AMOUNT" propertyname="claimAmount"/>
- <bean-property-setter-rule pattern="STATUS" propertyname="status"/>
- <bean-property-setter-rule pattern="ACCIDENT_PLACE" propertyname="accidentPlace"/>
- <bean-property-setter-rule pattern="ACCIDENT_DESCRIPTION" propertyname="accidentDescription"/>
- <bean-property-setter-rule pattern="LIABILITY_AMOUNT" propertyname="liabilityAmount"/>
- <set-next-rule methodname="addClaimData"/>
- pattern>
- <set-next-rule methodname="setBody"/>
- pattern>
- pattern>
- digester-rules>
java调用文件:
java 代码
- package com.xxxx.claim.util;
- import java.io.InputStream;
- import java.net.URL;
- import junit.framework.TestCase;
- import org.apache.commons.digester.Digester;
- import org.apache.commons.digester.xmlrules.DigesterLoader;
- import com.xxxx.claim.dto.PlatformPacket;
- /**
- * @author wanwei
- * @since 2007-5-28
- */
- public class DigesterTest extends TestCase
- {
- private static boolean testXmlRule = true;
- public void testXmlRule()
- {
- if( !testXmlRule ) return;
- URL rulesURL = ClassLoader.getSystemResource("report_back_rule.xml");
- Digester d = DigesterLoader.createDigester(rulesURL);
- PlatformPacket pp = new PlatformPacket();
- d.push(pp);
- try
- {
- InputStream xmlFile = ClassLoader.getSystemResourceAsStream("report_back.xml");
- //xmlFile = CommonUtils.inputStream2String(xmlFile);
- d.parse(xmlFile);
- } catch( java.io.IOException ioe )
- {
- System.out.println("Error reading input file:" + ioe.getMessage());
- System.exit(-1);
- } catch( org.xml.sax.SAXException se )
- {
- System.out.println("Error parsing input file:" + se.getMessage());
- System.exit(-1);
- }
- System.out.println(pp.getVersion());
- }
- }
剩下的就是对应的java dto了。
java 代码
- package com.xxxx.claim.dto;
- /**
- * @author wanwei
- * @since 2007-5-28
- */
- public class PlatformPacket
- {
- private String type = "";
- private String version = "";
- private Head head;
- private Body body;
- public String getType()
- {
- return type;
- }
- public void setType(String type)
- {
- this.type = type;
- }
- public String getVersion()
- {
- return version;
- }
- public void setVersion(String version)
- {
- this.version = version;
- }
- public void setHead(Head head)
- {
- this.head = head;
- }
- public Head getHead()
- {
- return head;
- }
- public Body getBody()
- {
- return body;
- }
- public void setBody(Body body)
- {
- this.body = body;
- }
- }
java 代码
- package com.xxxx.claim.dto;
- /**
- * @author wanwei
- * @since 2007-5-28
- */
- public class Head
- {
- private String requestType ;
- private String typeCode ;
- private String errorMessage ;
- public String getErrorMessage()
- {
- return errorMessage;
- }
- public void setErrorMessage(String errorMessage)
- {
- this.errorMessage = errorMessage;
- }
- public String getRequestType()
- {
- return requestType;
- }
- public void setRequestType(String requestType)
- {
- this.requestType = requestType;
- }
- public String getTypeCode()
- {
- return typeCode;
- }
- public void setTypeCode(String typeCode)
- {
- this.typeCode = typeCode;
- }
- }
java 代码
- package com.xxxx.claim.dto;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * @author wanwei
- * @since 2007-5-28
- */
- public class Body
- {
- private BasePart basePart;
- private List claimList = new ArrayList();
- public void addClaimData(ClaimData badyData)
- {
- claimList.add(badyData);
- }
- public BasePart getBasePart()
- {
- return basePart;
- }
- public void setBasePart(BasePart basePart)
- {
- this.basePart = basePart;
- }
- public List getClaimList()
- {
- return claimList;
- }
- public void setClaimList(List claimList)
- {
- this.claimList = claimList;
- }
- }
java 代码
- package com.xxxx.claim.dto;
- /**
- * claimData
- * @author wanwei
- * @since 2007-6-5
- */
- public class ClaimData
- {
- private String claimCompany;
- private String reportNo;
- private String claimNo;
- private String policyNo;
- private String accidentTime;
- private String reportTime;
- private String endcaseDate;
- private Double unclaimAmount;
- private Double claimAmount;
- private String status;
- private String accidentPlace;
- private String accidentDescription;
- private Double liabilityAmount;
- public String getAccidentDescription()
- {
- return accidentDescription;
- }
- public void setAccidentDescription(String accidentDescription)
- {
- this.accidentDescription = accidentDescription;
- }
- public String getAccidentPlace()
- {
- return accidentPlace;
- }
- public void setAccidentPlace(String accidentPlace)
- {
- this.accidentPlace = accidentPlace;
- }
- public String getAccidentTime()
- {
- return accidentTime;
- }
- public void setAccidentTime(String accidentTime)
- {
- this.accidentTime = accidentTime;
- }
- public Double getClaimAmount()
- {
- return claimAmount;
- }
- public void setClaimAmount(Double claimAmount)
- {
- this.claimAmount = claimAmount;
- }
- public String getClaimCompany()
- {
- return claimCompany;
- }
- public void setClaimCompany(String claimCompany)
- {
- this.claimCompany = claimCompany;
- }
- public String getEndcaseDate()
- {
- return endcaseDate;
- }
- public void setEndcaseDate(String endcaseDate)
- {
- this.endcaseDate = endcaseDate;
- }
- public Double getLiabilityAmount()
- {
- return liabilityAmount;
- }
- public void setLiabilityAmount(Double liabilityAmount)
- {
- this.liabilityAmount = liabilityAmount;
- }
- public String getPolicyNo()
- {
- return policyNo;
- }
- public void setPolicyNo(String policyNo)
- {
- this.policyNo = policyNo;
- }
- public String getReportNo()
- {
- return reportNo;
- }
- public void setReportNo(String reportNo)
- {
- this.reportNo = reportNo;
- }
- public String getReportTime()
- {
- return reportTime;
- }
- public void setReportTime(String reportTime)
- {
- this.reportTime = reportTime;