In the serialization process of XML and common pojo, there are many methods. The xstream package is still good, and its author is written by the famous thoughtworks. The following is a simple analysis.
1 first is a pojo object, such:
Public class employee {
Private string name;
Private string designation;
Private string department;
..........
2. Change pojo to an XML object.
Xstream = new xstream (New domdriver ());
Xstream. Alias ("employee", employee. Class );
Strxml = xstream. toxml (person );
System. Out. println (strxml );
3. deserialize XML objects into pojo objects
String STR = "..." // XML
Employee Employee = (employee) xstream. fromxml (STR );
System. Out. println ("name:" + employee. getname ());
4. A complicated example is attached:
Public class xmlparser {
Private Static final log = logfactory. getlog (xmlparser. Class );
Private Static final string default_encoding = "UTF-8 ";
/**
* Obtain the query matching of the specified character set in the XML file header encoding.
*/
Private Static pattern encodingpattern = pattern. Compile ("<\\? XML. * \ s + encoding \ s * = \ s * [\ "']? ([\ W \-_] +) [\ "'\ s?> <] ", Pattern. dotall + pattern. case_insensitive );
Private Static customxppdriver driver = new customxppdriver (New customreplacer ());
Private Static Boolean flat;
Private Static Boolean notlist;
/**
* Parse the XML file into map set data and use the character set specified by the XML content for encoding parsing. If no encoding is specified in the XML, the UTF-8 is used for encoding parsing by default.
*/
Public static map parsexmlfile (string filename) throws filenotfoundexception, unsupportedencodingexception {
Return parse (new file (filename ));
}
/**
* Parse the XML file into map set data and use the character set specified by the XML content for encoding parsing. If no encoding is specified in the XML, the UTF-8 is used for encoding parsing by default.
*/
Public static map parse (File xmlfile) throws filenotfoundexception, unsupportedencodingexception {
Reader reader = NULL;
Try {
Reader = getxmlencodingreader (New fileinputstream (xmlfile ));
Return parse (Reader );
} Finally {
If (reader! = NULL ){
Try {
Reader. Close ();
} Catch (exception ignore ){
}
}
}
}
/**
* Parse the XML byte array into map set data and use the character set specified by the XML content for encoding parsing. If no encoding is specified in the XML, the UTF-8 is used for encoding parsing by default.
*/
Public static map parse (byte [] xmlbytes) throws unsupportedencodingexception {
Return parse (getxmlencodingreader (New bytearrayinputstream (xmlbytes )));
}
/**
* Parses the XML byte array into map set data according to the specified character set.
*/
Public static map parse (byte [] xmlbytes, string encoding) throws unsupportedencodingexception {
Hierarchicalstreamreader reader = driver. createreader (New bytearrayinputstream (xmlbytes), encoding );
Return getdata (Reader );
}
/**
* Parses the XML string into map set data.
* @ Param xmlstr string
* @ Return collection data
*/
Public static map parse (string xmlstr ){
Return parse (New stringreader (xmlstr ));
}
/**
* Parses XML byte streams into map set data and uses the character set specified in XML content for encoding and resolution */
Public static map parse (inputstream xmlstream) throws unsupportedencodingexception {
Return parse (getxmlencodingreader (xmlstream ));
}
/**
* Parses the XML byte stream into map set data according to the specified character set.
* @ Param xmlstream byte stream
* @ Param encoding Character Set
* @ Return collection data
* @ Throws unsupportedencodingexception unsupported Character Set exception
*/
Public static map parse (inputstream xmlstream, string encoding) throws unsupportedencodingexception {
Hierarchicalstreamreader reader = driver. createreader (xmlstream, encoding );
Return getdata (Reader );
}
/**
* Parse the XML workflow stream into map set data.
* @ Param xmlreader refers to the stream
* @ Return collection data
*/
Public static map parse (Reader xmlreader ){
Hierarchicalstreamreader reader = driver. createreader (xmlreader );
Return getdata (Reader );
}
/**
* Gets the character set encoding specified by the XML content in the byte stream. If no encoding is specified in the XML, the UTF-8 is returned.
* @ Param stream byte stream. The mark/reset method must be supported.
* @ Return Character Set
*/
Public static string getencoding (inputstream stream ){
String encoding = default_encoding;
If (! Stream. marksupported ()){
Return encoding;
}
Byte [] Buf = new byte [70];
Try {
Stream. Mark (100 );
Int Len = stream. Read (BUF );
If (LEN> 0 ){
String xmlhead = new string (BUF, 0, Len );
Matcher M = encodingpattern. matcher (xmlhead );
If (M. Find ()){
Encoding = M. Group (1). Trim ();
}
}
Stream. Reset ();
} Catch (throwable e ){
Log. Error ("Get XML Encoding Error! ", E );
}
Return encoding;
}
/**
* Converts an XML byte stream to a sequence stream that supports the encoding specified by the XML content. If encoding is not specified in the XML, the UTF-8 is used for encoding resolution by default.
* @ Param stream byte stream
* @ Return response stream
* @ Throws unsupportedencodingexception unsupported Character Set exception
*/
Private Static reader getxmlencodingreader (inputstream stream) throws unsupportedencodingexception {
Stream = new bufferedinputstream (Stream );
Return new inputstreamreader (stream, getencoding (Stream ));
}
/**
* Parse the XML workflow stream into map set data.
* @ Param reader XML response stream read object
* @ Return collection data
*/
Private Static map getdata (hierarchicalstreamreader reader ){
Map Data = new hashmap ();
Filldata (reader, Data, flat );
Return data;
}
/**
* Set whether to use the set method when filling the map set after parsing is complete. The set method is used by default.
* Set: data with the same XML node name is stored using the arraylist set.
* Single: data with the same XML node name can only be stored with the first value.
* @ Param notlist
*/
Public static void setnotlist (Boolean no ){
Notlist = no;
}
Public static Boolean isnotlist (){
Return notlist;
}
/**
* After setting resolution, whether to fill the map set in flat filling mode. If the flat filling mode is not used, the map set uses the same structure as the XML node to store data. Not used by default.
* Flat filling mode: No matter which node data of XML is filled into the root set. If the root set already has node data of this name, the data of the node name is stored in the list object sequence.
* @ Param flattype: whether the flat filling mode is used.
*/
Public static void setflattype (Boolean flattype ){
Flat = flattype;
}
/**
* Do you want to fill the map set in flat filling mode?
* @ Return indicates whether the flat filling mode is used.
*/
Public static Boolean isflattype (){
Return flat;
}
/**
* Parse the XML Parse stream and fill the data in the specified map set.
* @ Param reader XML response stream read object
* @ Param data: set of data to be filled
* @ Param flattype whether flat filling mode is used
*/
Private Static void filldata (hierarchicalstreamreader reader, map data, Boolean flattype ){
Map Childs;
String name = reader. getnodename ();
Object value;
If (! Reader. hasmorechildren ()){
Value = reader. getvalue ();
Childs = NULL;
} Else if (! Flattype ){
Value = Childs = new hashmap ();
} Else {
Value = "";
Childs = data;
}
Object nodecontainer = data. Get (name );
// Directly overwrite duplicate name
If (notlist ){
If (nodecontainer = NULL ){
If (value! = NULL ){
Data. Put (name, value );
}
}
} Else {
// Repeat the generated list to save
If (nodecontainer = NULL ){
If (value! = NULL ){
Data. Put (name, value );
}
} Else if (nodecontainer instanceof arraylist ){
(Arraylist) nodecontainer). Add (value );
} Else {
Arraylist nodelist = new arraylist ();
Nodelist. Add (nodecontainer );
Nodelist. Add (value );
Data. Put (name, nodelist );
}
}
While (reader. hasmorechildren ()){
Reader. movedown ();
Filldata (reader, Childs, flattype );
Reader. moveup ();
}
}
/**
* Custom xpp parsing Driver Class
* @ Author WD
*/
Private Static class customxppdriver extends xppdriver {
Public customxppdriver (){
Super (New customreplacer ());
}
Public customxppdriver (xmlfriendlyreplacer replacer ){
Super (replacer );
}
/**
* Converts an XML byte stream to an XML byte stream that supports the specified character set encoding.
* @ Param stream byte stream
* @ Param encoding Character Set
* @ Return XML response stream read object
* @ Throws unsupportedencodingexception unsupported Character Set exception
*/
Public hierarchicalstreamreader createreader (inputstream stream, string encoding) throws unsupportedencodingexception {
Return createreader (New inputstreamreader (stream, encoding ));
}
}
/**
* Custom xml tag conversion class
* @ Author WD
*/
Private Static class customreplacer extends xmlfriendlyreplacer {
Public String escapename (string name ){
Return name;
}
Public String unescapename (string name ){
Return name;
}
}
Public static void main (string [] ARGs) throws exception {
Java. Io. bytearrayoutputstream output = new java. Io. bytearrayoutputstream ();
Java. Io. fileinputstream input = NULL;
Byte [] DATA = new byte [1024];
Int Len =-1;
Try {
Input = new java. Io. fileinputstream ("C: \ test.txt ");
For (; (LEN = input. Read (data)> = 0 ;){
Output. Write (data, 0, Len );
}
} Catch (exception e ){
Throw E;
} Finally {
If (input! = NULL ){
Try {
Input. Close ();
} Catch (exception e ){
}
}
}
Byte [] xmlbytes = getmsg (output. tostring ());
System. Out. println ("XML content =" + new string (xmlbytes, getencoding (New java. Io. bytearrayinputstream (xmlbytes ))));
Xmlparser. setflattype (true );
Map xml = xmlparser. parse (xmlbytes );
System. Out. println ();
System. Out. println ("XML Object =" + XML );
}