XStream
The company gives tools to do an interface test, the tool returned to US file format for XML, our platform using Java development, for future data persistence and easy to view the results, you should convert XML to object, so contact with the XStream this class library, although small, but the function is really powerful.
There is an article on the web on the XStream to introduce http://www.cnblogs.com/hoojo/archive/2011/04/22/2025197.html, thank bloggers.
In addition, there are some additions to it, mainly in parsing XML, encountered some inconsistent with the normal XML format, which requires a number of different conventional processing methods to solve:
First, the use of converter interface
There's a response file that needs to be parsed.
<result value=\ "Success\" >
<EngineName>webxxx</EngineName>
<CPU>50</CPU>
<MEM>30</MEM>
<DISK>40</DISK>
<Funcs>
<func name=\ "Trojan scan \" >OK</Func>
<func name=\ "Storage \" >NO</Func>
</Funcs>
</Result> ";
Depending on the format of the XML, it can be parsed into a result object that contains the Enginename,cpu,mem,disk,list<func> property, where the Func object corresponds to the <Func/> node. In the normal way, you cannot assign the text value of the <Func/> node to the Func object.
Then converter came in handy: first, define all kinds of
public class Getstateresult {
private String value;
Private String enginename;
Private String CPU;
Private String MEM;
Private String DISK;
Private list<com.time.dbapp.vo.getstatefunc> Funcs;
public class getstatefuc{
Private String value;//corresponds to ok/no
private String name;
Converter Implementation Class
Class Requestcoverter implements converter{
public void Marshal (Object arg0, hierarchicalstreamwriter writer,marshallingcontext context) {
}
Public Object Unmarshal (hierarchicalstreamreader reader,unmarshallingcontext context) {
Com.time.dbapp.vo.GetStateFunc result = new Com.time.dbapp.vo.GetStateFunc ();
Result.setname (Reader.getattribute ("name")); Get order about must get name before you can get value, otherwise the error
Result.setvalue (Reader.getvalue ());
return result;
}
public boolean Canconvert (Class clazz) {
Return Clazz.equals (Com.time.dbapp.vo.GetStateFunc.class);
}}
The function is to perform the Unmarshal method to resolve the corresponding node when parsing to the Set mapping node (Canconvert returns True).
Next, make the XStream configuration:
XStream XStream = new XStream ();
Xstream.registerconverter (New Requestcoverter ()); Registering converters
Xstream.alias ("result", getstateresult.class);
Xstream.alias ("func", Com.time.dbapp.vo.GetStateFunc.class);
Xstream.useattributefor (Com.time.dbapp.vo.GetStateFunc.class, "name");
This will allow for parsing.
Second, Xppdriver, Prettyprintwriter, Xppreader class
I understand that a parse input-output driver, the class inherits Abstractxppdriver, implements the Hierarchicalstreamdriver interface, (you can view the API document) First look at the class method, you probably know what to do:
Xppdriver () {
Public Hierarchicalstreamwriter Createwriter (Writer out) {}
Public Hierarchicalstreamreader Createreader (Reader reader) {}
Presumably, we can do something with parsing (Reader) and output (Writer). This is a need for this in the above time. The problem is that the test tool returns all of the node names in our document structure (such as above) to uppercase, but according to the JavaBean naming convention, all Java attributes should be in lowercase letters. When in accordance with the principle of xstream processing, they instantiate the object, and the process of assigning values is not through the Get/set method, direct manipulation of attributes, so there will be a problem, a case of uppercase and lowercase is not complete mapping. In order for the code to conform to the specification and parse the XML file correctly. So Xppdriver debut.
The XStream (Hierarchicalstreamdriver hierarchicalstreamdriver) in the API sees that the instantiated XStream object can be processed by passing hierarchicalstreamdriver objects.
So I simply encapsulated XStream
Class Requestxstream {
Requestxstream () {
Super (new Xppdriver () {
Public Hierarchicalstreamwriter Createwriter (Writer out) {
Return to New Prettyprintwriter (out) {
public void Startnode (String name) {//convert the corresponding property name to the first capital letter
Super.startnode (Stringutil.upperfirstchar (name));
}
};
}
Public Hierarchicalstreamreader Createreader (reader reader) {
return new Xppreader (reader) {
Public String Getnodename () {
Return Stringutil.lowerfirstchar (Super.getnodename ()); {//Convert the corresponding property name to the first letter lowercase
}
};
}
});
}
}
This is a good way to deal with the problem of the initial case of the knot, where Stringutil.upperfirstchar writes the first letter of the string to capitalize on its own simple processing. Of course, for different problems can be implemented xppdriver specific different methods, which can be viewed in the API, to solve, in short XStream is a good class library, provides a rich interface to meet different file format parsing and conversion.