Let's say the first three ways:
Dom Way : The individual understands similar to. NET's XmlDocument, the parsing time is not high, occupies the memory, is not suitable for the big XML parsing;
Sax Method : event-based parsing, when parsing to a part of XML, triggers a specific event that can be defined in a custom parsing class to do when an event is triggered; a personal feeling is a very different way, I don't know. NET system is there a similar way?
Stax Way : The individual understands similar to. NET XmlReader way, the efficiency is high, occupy memory is little, apply large XML parsing;
However, the Sax method was used before, this article mainly introduces JAXB, here only the main code is posted:
Copy Code code as follows:
Import java.util.ArrayList;
Import java.util.List;
Import org.xml.sax.Attributes;
Import org.xml.sax.SAXException;
Import Org.xml.sax.helpers.DefaultHandler;
public class Configparser extends DefaultHandler {
Private String currentconfigsection;
Public Sysconfigitem Sysconfig;
Public list<interfaceconfigitem> interfaceconfiglist;
Public list<ftpconfigitem> ftpconfiglist;
Public list<adapterconfigitem> adapterconfiglist;
public void Startdocument () throws Saxexception {
Sysconfig = new Sysconfigitem ();
Interfaceconfiglist = new arraylist<interfaceconfigitem> ();
Ftpconfiglist = new arraylist<ftpconfigitem> ();
Adapterconfiglist = new arraylist<adapterconfigitem> ();
}
public void Enddocument () throws Saxexception {
}
public void Startelement (string uri, String localname, String qName, Attributes Attributes) throws Saxexception {
if (Qname.equalsignorecase ("Item") && attributes.getlength () > 0) {
if (Currentconfigsection.equalsignorecase ("Sysconfigitem")) {
Sysconfig = new Sysconfigitem (attributes);
else if (currentconfigsection.equalsignorecase ("Interfaceconfigitems")) {
Interfaceconfiglist.add (new Interfaceconfigitem (attributes));
else if (currentconfigsection.equalsignorecase ("Ftpconfigitems")) {
Ftpconfiglist.add (new Ftpconfigitem (attributes));
else if (currentconfigsection.equalsignorecase ("Adapterconfigitems")) {
Adapterconfiglist.add (new Adapterconfigitem (attributes));
}
} else {
Currentconfigsection = QName;
}
}
public void EndElement (string uri, String localname, String qName) throws Saxexception {
}
public void characters (char ch[], int start, int length) throws Saxexception {
}
}
Copy Code code as follows:
Import Java.lang.reflect.Field;
Import Java.text.DateFormat;
Import java.text.ParseException;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
Import org.xml.sax.Attributes;
public class Configitembase {
private static DateFormat DateFormat = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
Public Configitembase () {
}
/**
* Currently support only a few common types if you need to support other types, please modify the code here
*
* @param attributes
*/
Public Configitembase (Attributes Attributes) {
Class<?> cls = This.getclass ();
field[] fields = Cls.getdeclaredfields ();
for (Field field:fields) {
String FieldType = Field.gettype (). Getsimplename ();
for (int i = 0; i < attributes.getlength (); i++) {
if (Attributes.getqname (i). Equalsignorecase (Field.getname ())) {
Field.setaccessible (TRUE);
try {
if (Fieldtype.equalsignorecase ("String")) {
Field.set (This, Attributes.getvalue (Attributes.getqname (i)));
else if (fieldtype.equalsignorecase ("Integer")) {
Field.set (This, integer.valueof (Attributes.getvalue (Attributes.getqname (i))));
else if (fieldtype.equalsignorecase ("Double")) {
Field.set (This, double.valueof (Attributes.getvalue (Attributes.getqname (i))));
else if (fieldtype.equalsignorecase ("Date")) {
Field.set (This, GetDate (Attributes.getvalue (Attributes.getqname (i))));
} else {
System.out.println ("Warning:unhandler Field (" + field.getname () + "-" + FieldType + "));
}
catch (IllegalArgumentException e) {
E.printstacktrace ();
catch (Illegalaccessexception e) {
E.printstacktrace ();
}
Break
}
}
}
}
Public String toString () {
String result = "";
Class<?> cls = This.getclass ();
String classnamestring = Cls.getname ();
Result + = classnamestring.substring (Classnamestring.lastindexof ('. ') + 1, classnamestring.length ()) + ":";
field[] fields = Cls.getdeclaredfields ();
for (Field field:fields) {
try {
result = = Field.getname () + "=" + Field.get (this) + ";";
catch (IllegalArgumentException e) {
E.printstacktrace ();
catch (Illegalaccessexception e) {
E.printstacktrace ();
}
}
return result;
}
/**
* Processing Time type attributes (time format requirements: YYYY-MM-DD HH:MM:SS)
*
* @param datestring
* @return
*/
private static Date GetDate (String datestring) {
Date date = null;
try {
Date = Dateformat.parse (datestring);
catch (ParseException e) {
E.printstacktrace ();
}
return date;
}
}
The most convenient: JAXB (Java architecture for XML Binding) is highlighted below
Here, using the more complex mobile Batchsyncorderrelationreq interface XML as an example (feel able to solve this everyone is basically enough), the message format is as follows (the CDATA content in Svccont is a style, it's disgusting):
Copy Code code as follows:
<?xml version= "1.0" encoding= "UTF-8" standalone= "yes"?>
<InterBOSS>
<Version>0100</Version>
<TestFlag>0</TestFlag>
<BIPType>
<BIPCode>BIP2B518</BIPCode>
<ActivityCode>T2101518</ActivityCode>
<ActionCode>0</ActionCode>
</BIPType>
<RoutingInfo>
<OrigDomain>BOSS</OrigDomain>
<RouteType>routeType</RouteType>
<Routing>
<HomeDomain>XXXX</HomeDomain>
<RouteValue>routeValue</RouteValue>
</Routing>
</RoutingInfo>
<TransInfo>
<SessionID>2013041017222313925676</SessionID>
<TransIDO>2013041017222313925676</TransIDO>
<TransIDOTime>20130410172223</TransIDOTime>
<TransIDH></TransIDH>
<TransIDHTime></TransIDHTime>
</TransInfo>
<SNReserve>
<TransIDC></TransIDC>
<ConvID></ConvID>
<CutOffDay></CutOffDay>
<OSNTime></OSNTime>
<OSNDUNS></OSNDUNS>
<HSNDUNS></HSNDUNS>
<MsgSender></MsgSender>
<MsgReceiver></MsgReceiver>
<Priority></Priority>
<ServiceLevel></ServiceLevel>
<SvcContType></SvcContType>
</SNReserve>
<Response>
<RspType>rspType</RspType>
<RspCode>rspCode</RspCode>
<RspDesc>rspDesc</RspDesc>
</Response>
<svccont><! [Cdata[<?xml version= "1.0" encoding= "UTF-8" standalone= "yes"?>
<batchSyncOrderRelationReq>
<msgTransactionID>210001BIP2B518130410172223651627</msgTransactionID>
<reqNum>2</reqNum>
<reqBody>
<oprNumb>210001BIP2B518130410172224341871</oprNumb>
<subscriptionInfo>
<oprTime>oprTime1</oprTime>
<actionID>actionId1</actionID>
<brand>brand1</brand>
<effTime>effTime1</effTime>
<expireTime>expireTime1</expireTime>
<feeUser_ID>feeUserId1</feeUser_ID>
<destUser_ID>destUserId1</destUser_ID>
<actionReasonID>actionId1</actionReasonID>
<servType>servType1</servType>
<subServType>subServType1</subServType>
<SPID>spId1</SPID>
<SPServID>spServId1</SPServID>
<accessMode>accessMode1</accessMode>
<servParamInfo>
<para_num>0</para_num>
<para_info>
<para_name></para_name>
<para_value></para_value>
</para_info>
</servParamInfo>
<feeType>feeType1</feeType>
</subscriptionInfo>
</reqBody>
<reqBody>
<oprNumb>210001BIP2B518130410172224420909</oprNumb>
<subscriptionInfo>
<oprTime>oprTime2</oprTime>
<actionID>actionId2</actionID>
<brand>brand2</brand>
<effTime>effTime2</effTime>
<expireTime>expireTime2</expireTime>
<feeUser_ID>feeUserId2</feeUser_ID>
<destUser_ID>destUserId2</destUser_ID>
<actionReasonID>actionId2</actionReasonID>
<servType>servType2</servType>
<subServType>subServType2</subServType>
<SPID>spId2</SPID>
<SPServID>spServId2</SPServID>
<accessMode>accessMode2</accessMode>
<servParamInfo>
<para_num>0</para_num>
<para_info>
<para_name></para_name>
<para_value></para_value>
</para_info>
</servParamInfo>
<feeType>feeType2</feeType>
</subscriptionInfo>
</reqBody>
</batchSyncOrderRelationReq>]]></SvcCont>
</InterBOSS>
The decoding code is as follows:
Copy Code code as follows:
@XmlRootElement (name = "Batchsyncorderrelationreq")
@XmlAccessorType (Xmlaccesstype.field)
public class Batchsyncorderrelationreq extends Bossmessage<batchsyncorderrelationreq> {
@XmlElement (name = "Msgtransactionid")
Private String Msgtransactionid = "";
@XmlElement (name = "Reqnum")
Private String Reqnum = "";
@XmlElement (name = "Reqbody")
Private list<batchsyncorderrelationreqbody> reqbodylist;
Public Batchsyncorderrelationreq () {
}
Public String Getmsgtransactionid () {
return This.msgtransactionid;
}
public void Setmsgtransactionid (String msgtransactionid) {
This.msgtransactionid = Msgtransactionid;
}
Public String Getreqnum () {
return this.reqnum;
}
public void Setreqnum (String reqnum) {
This.reqnum = Reqnum;
}
Public list<batchsyncorderrelationreqbody> getreqbodylist () {
return this.reqbodylist;
}
public void Setreqbodylist (list<batchsyncorderrelationreqbody> reqbodylist) {
This.reqbodylist = reqbodylist;
}
@Override
Public Batchsyncorderrelationreq deserialized (String interbossxmlcontent) throws Businessexception {
try {
Deserialized for head
Jaxbcontext Jaxbcxt4head = jaxbcontext.newinstance (Messagehead.class);
Unmarshaller unmarshaller4head = Jaxbcxt4head.createunmarshaller ();
Messagehead head = (messagehead) Unmarshaller4head.unmarshal (new StringReader (interbossxmlcontent));
Deserialized for syncorderrelationreq body
Jaxbcontext jaxbcxt4body = jaxbcontext.newinstance (Batchsyncorderrelationreq.class);
Unmarshaller unmarshaller4body = Jaxbcxt4body.createunmarshaller ();
Batchsyncorderrelationreq batchsyncorderrelationreq = (batchsyncorderrelationreq) Unmarshaller4Body.unmarshal (new StringReader (Head.getsvccont (). Trim ());
Batchsyncorderrelationreq.sethead (head);
return batchsyncorderrelationreq;
catch (Jaxbexception e) {
throw new Businessexception ("Syncorderrelationreq.deserialized () error! (" + interbossxmlcontent + ")", e);
}
}
}
@XmlAccessorType (Xmlaccesstype.field)
Copy Code code as follows:
public class Batchsyncorderrelationreqbody {
@XmlElement (name = "Oprnumb")
Private String Oprnumb = "";
@XmlElement (name = "Subscriptioninfo")
Private Subscriptioninfo Subscriptioninfo;
Public Batchsyncorderrelationreqbody () {
}
Public Batchsyncorderrelationreqbody (String oprnumb, Subscriptioninfo subscriptioninfo) {
This.oprnumb = Oprnumb;
This.subscriptioninfo = Subscriptioninfo;
}
Public String Getoprnumb () {
return this.oprnumb;
}
public void Setoprnumb (String oprnumb) {
This.oprnumb = Oprnumb;
}
Public Subscriptioninfo Getsubscriptioninfo () {
return this.subscriptioninfo;
}
public void Setsubscriptioninfo (Subscriptioninfo subscriptioninfo) {
This.subscriptioninfo = Subscriptioninfo;
}
}
@XmlAccessorType (Xmlaccesstype.field)
Copy Code code as follows:
public class Subscriptioninfo {
@XmlElement (name = "Oprtime")
Private String Oprtime = "";
@XmlElement (name = "ActionId")
Private String ActionId = "";
@XmlElement (name = "Brand")
Private String brand = "";
@XmlElement (name = "Efftime")
Private String Efftime = "";
@XmlElement (name = "Expiretime")
Private String Expiretime = "";
@XmlElement (name = "feeuser_id")
Private String Feeuserid = "";
@XmlElement (name = "destuser_id")
Private String Destuserid = "";
@XmlElement (name = "Actionreasonid")
Private String Actionreasonid = "";
@XmlElement (name = "Servtype")
Private String Servtype = "";
@XmlElement (name = "Subservtype")
Private String Subservtype = "";
@XmlElement (name = "SPID")
Private String spId = "";
@XmlElement (name = "Spservid")
Private String Spservid = "";
@XmlElement (name = "AccessMode")
Private String AccessMode = "";
@XmlElement (name = "Feetype")
Private String Feetype = "";
Public Subscriptioninfo () {
}
Public Subscriptioninfo (
String Oprtime,
String ActionId,
String brand,
String Efftime,
String Expiretime,
String Feeuserid,
String Destuserid,
String Actionreasonid,
String Servtype,
String Subservtype,
String SpId,
String Spservid,
String AccessMode,
String Feetype) {
This.oprtime = Oprtime;
This.actionid = ActionId;
This.brand = brand;
This.efftime = Efftime;
This.expiretime = Expiretime;
This.feeuserid = Feeuserid;
This.destuserid = Destuserid;
This.actionreasonid = Actionreasonid;
This.servtype = Servtype;
This.subservtype = Subservtype;
This.spid = spId;
This.spservid = Spservid;
This.accessmode = AccessMode;
This.feetype = Feetype;
}
Public String Getoprtime () {
return this.oprtime;
}
public void Setoprtime (String oprtime) {
This.oprtime = Oprtime;
}
Public String Getactionid () {
return This.actionid;
}
public void Setactionid (String actionid) {
This.actionid = ActionId;
}
Public String Getbrand () {
return this.brand;
}
public void Setbrand (String brand) {
This.brand = brand;
}
Public String Getefftime () {
return this.efftime;
}
public void Setefftime (String efftime) {
This.efftime = Efftime;
}
Public String Getexpiretime () {
return this.expiretime;
}
public void Setexpiretime (String expiretime) {
This.expiretime = Expiretime;
}
Public String Getfeeuserid () {
return This.feeuserid;
}
public void Setfeeuserid (String feeuserid) {
This.feeuserid = Feeuserid;
}
Public String Getdestuserid () {
return This.destuserid;
}
public void Setdestuserid (String destuserid) {
This.destuserid = Destuserid;
}
Public String Getactionreasonid () {
return This.actionreasonid;
}
public void Setactionreasonid (String actionreasonid) {
This.actionreasonid = Actionreasonid;
}
Public String Getservtype () {
return this.servtype;
}
public void Setservtype (String servtype) {
This.servtype = Servtype;
}
Public String Getsubservtype () {
return this.subservtype;
}
public void Setsubservtype (String subservtype) {
This.subservtype = Subservtype;
}
Public String Getspid () {
return this.spid;
}
public void Setspid (String spId) {
This.spid = spId;
}
Public String Getspservid () {
return this.spservid;
}
public void Setspservid (String spservid) {
This.spservid = Spservid;
}
Public String Getaccessmode () {
return this.accessmode;
}
public void Setaccessmode (String accessmode) {
This.accessmode = AccessMode;
}
Public String Getfeetype () {
return this.feetype;
}
public void Setfeetype (String feetype) {
This.feetype = Feetype;
}
}