Recent project development encountered a problem, the provider no longer through the URL address request way, but
Send XML-formatted messages through the socket to a designated server for unified authentication of information.
So it is necessary to assemble the XML-formatted message string and parse the XML-formatted characters returned by the server to obtain the required data.
The key to solving the problem. Before that, I thought it would be a bit difficult ... After it is done, then the egg, it is so ...
LZ mainly used in the XStream class. This class perfectly solves the transformation between an XML document and a Java object.
Because just touching this class ... For many of the features provided in the inside has not been carefully excavated. It's just a simple way to achieve
I want to implement the feature: Wait for the following time to eat slowly, carefully taste ...
Entrance class: The main provider runs the portal ...
The class contains a static generatehtml () method, a string used to generate the XML format
PackageCom.linjm.xml; Public classEntrance { Public Static voidMain (string[] args) {String HTML=generatehtml (); System.out.println ("############################"); Tagentity tagentity= Xmlutil.fromxml (HTML, tagentity.class); System.out.println ("Parsing HTML:"); System.out.println ("Title:" +Tagentity.gethead (). GetTitle ()); System.out.println ("Output class Information:"); System.out.println (Tagentity.tostring ()); } Public StaticString generatehtml () {tagentity tagentity=Newtagentity (); Head Head=Init.inithead (); Body Body=Init.initbody (); Tagentity.sethead (head); Tagentity.setbody (body); String HTML=Xmlutil.toxml (tagentity); String Top= "<! DOCTYPE HTML public \ "-//W3C//DTD HTML 4.01 transitional//en\" >\n "; System.out.println ("Generate html:\n" + top +html); returnhtml; } }View Code
First of all, look at the results of the operation of the program ... After reading we look at the following specific code structure ...
Generate HTML:<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >########################### #解析HTML: Title:title Output class information: Head:[title:title,meta:meta,js:javascript,css:css], Body:[div:div,input:input,font:front,span:span]
View Code
Init class: primarily used to initialize the contents of the child tags within the head and body tags in the XML ...
PackageCom.linjm.xml; Public classInit { Public StaticHead Inithead () {head head=NewHead (); Head.settitle ("Title"); Head.setmeta ("Meta"); Head.setjs ("JavaScript"); Head.setcss ("CSS"); returnHead; } Public StaticBody Initbody () {Body Body=NewBody (); Body.setdiv ("Div"); Body.setfont ("Front"); Body.setinput ("Input"); Body.setspan ("Span"); returnbody; }}View Code
tagentity class: mainly contains two fields: Head and body.
Also the head tag and body tag presented in the final XML ...
Define theXML display in the label by means of the @ Xstreamalias annotation ...
You can define different labels according to your own needs ...
Head and body tags can vary according to requirements ...
PackageCom.linjm.xml;ImportCom.thoughtworks.xstream.annotations.XStreamAlias; @XStreamAlias ("HTML") Public classtagentity {@XStreamAlias ("Head") Privatehead Head; @XStreamAlias ("Body") Privatebody Body; PublicHead GetHead () {returnHead; } Public voidSethead (head head) { This. Head =Head; } PublicBody GetBody () {returnbody; } Public voidSetbody (body body) { This. BODY =body; } Publicstring toString () {string str= "head:[" + "title:" + head.gettitle () + ", Meta:" + head.getmeta () + ", JS:" + head.getjs () + ", CSS:" + head.getcss () + "]," + "body:[" + "div:" + body.getdiv () + ", Input:" + body.getinput () + ", font:" + body.getfont () + ", Span:" + Body.getspan () + "]"; returnstr; }}View Code
Head class: the child tag attribute field in the head tag is defined in the entity class ...
The sub-tags in the head tag can be based on the actual situation
PackageCom.linjm.xml;ImportCom.thoughtworks.xstream.annotations.XStreamAlias; Public classHead {@XStreamAlias ("Title") PrivateString title; @XStreamAlias ("Meta") PrivateString Meta; @XStreamAlias ("JS") PrivateString JS; @XStreamAlias ("CSS") PrivateString CSS; PublicString GetTitle () {returntitle; } Public voidSettitle (String title) { This. title =title; } PublicString Getmeta () {returnMeta; } Public voidSetmeta (String meta) { This. Meta =Meta; } PublicString Getjs () {returnJS; } Public voidSetjs (String js) { This, J1 =JS; } PublicString getcss () {returnCSS; } Public voidsetcss (String css) { This. CSS =CSS; }}View Code
Body class: The child tag attribute field in the body tag is defined in the entity class ...
The sub-tags in the body tag can be determined according to the actual situation ...
PackageCom.linjm.xml;ImportCom.thoughtworks.xstream.annotations.XStreamAlias; Public classBody {@XStreamAlias ("Div") PrivateString Div; @XStreamAlias ("Span") PrivateString span; @XStreamAlias ("Font") PrivateString font; @XStreamAlias ("Input") PrivateString input; PublicString Getdiv () {returnDiv; } Public voidSetdiv (String div) { This. div =Div; } PublicString Getspan () {returnspan; } Public voidSetspan (String span) { This. span =span; } PublicString GetFont () {returnfont; } Public voidsetFont (String font) { This. Font =font; } PublicString GetInput () {returninput; } Public voidsetinput (String input) { This. Input =input; }}View Code
Xmlutil class: This class defines xstream in the assembly of XML strings and parsing XML string encapsulation methods ...
The ToXml () method is used to convert a Java object to an XML string;
The FromXml () method is used to parse an XML string into a Java object.
PackageCom.linjm.xml;ImportCom.thoughtworks.xstream.XStream;ImportCom.thoughtworks.xstream.io.xml.DomDriver; Public classXmlutil {/*** @desc To convert Java objects to XML string *@authorjimly * @date 20160423 **/ Public StaticString toXml (Object obj) {XStream xs=NewXStream (Newdomdriver ()); Xs.processannotations (Obj.getclass ()); returnxs.toxml (obj); } /*** @desc Convert XML string to Java Object *@authorjimly * @date 20160423 **/@SuppressWarnings ("Unchecked") Public Static<T> T FromXml (String xmlstr, class<t>CLS) {XStream xs=NewXStream (Newdomdriver ()); Xs.processannotations (CLS); T obj=(T) xs.fromxml (XMLSTR); returnobj; }}View Code
The code implementation is simple and not very complex ... Basically you can read it ...
In order not to make the code too long, I still write in multiple classes of files, not all in a class file.
The design of the program structure is a bit unreasonable, a bit verbose. Currently only shows the function of the program ...
The programming needs to be optimized ...
Jar Package Needed:Xstream-1.4.7.jar
Java objects and XML documents, the original