Xstream + xpp combination of powerful gold

Source: Internet
Author: User
No need to generate DTD, useless configuration, no need to generate helper classes, fast. This is the combination of xstream and xpp.
As we all know about xstream, XML pull parser is a high-speed method for parsing XML files, which is much faster than the traditional method (it is found that pull-based Parsing is now popular ). The following is an example of multiple usage methods.

1. The simplest way to use
This is too simple, so I will take the following example from blog http://moogle.javaeye.com/blog/34661of moogle.
1. Public static void write (){
2. xstream Sm = new xstream ();
3. mytest T = new mytest ();
4. T. setname ("moogle ");
5. T. setxb ("male ");
6. Try {
7. fileoutputstream Ops = new fileoutputstream (new file ("C: // 111.xml "));
8. Sm. toxml (T, OPS );
9. Ops. Close ();
10.} catch (exception e ){
11. E. printstacktrace ();
12 .}
13 .}
14. Public static void read (){
15. xstream Sm = new xstream (New domdriver ());
16. Try {
17. fileinputstream Ops = new fileinputstream (new file ("C: // 111.xml "));
18. mytest t = (mytest) Sm. fromxml (OPS );
19. system. Out. println (T. getname ());
20. Ops. Close ();
21.} catch (exception e ){
22. E. printstacktrace ();
23 .}
24 .}

Generating XML is
# <Mytest>
# <Name> ASD </Name>
# <XB> male </XB>

2. Moderate method (this function is available only after version 1.2 or later)
Xstream stream = new xstream ();
Stream. Alias ("schema", schemaobject. Class );
Stream. useattributefor ("url", String. Class );
Stream. useattributefor ("jdbcdriverclass", String. Class );
Stream. useattributefor ("user", String. Class );
Stream. useattributefor ("password", String. Class );
Fileoutputstream S = new fileoutputstream (File );
Stream. toxml (theobject, S );
S. Close ();
Alias and useattributefor are used to modify <com. hongsoft. model. schemaobject> to <schema>

3. Advanced methods
Xstream stream = new xstream ();
Stream. registerconverter (New schemaxmlconvertor ());
Stream. Alias ("schema", schemaobject. Class );
Fileinputstream S = new fileinputstream (File );
Object = (schemaobject) stream. fromxml (s );
S. Close ();
Registerconverter can convert the XML and objects of any schema into one another. This actually denies what many friends have said.
"Xstream + xpp" is not powerful. Example of schemaxmlconvertor:
Public void Marshal (Object arg0, hierarchicalstreamwriter writer,
Marshallingcontext arg2 ){
Schemaobject schema = (schemaobject) arg0;
Writer. startnode (schemaobject. Table );
Writer. addattribute (schemaobject. table_name, itable. getname ());
// Line
List <schemaobject. tableobject. lineobject> lines = itable. getlines ();
For (Int J = 0; j <lines. Size (); j ++)
{
Schemaobject. tableobject. lineobject jline = lines. Get (j );
Writer. startnode (schemaobject. line );
// Column
Map <string, string> Columns = jline. getcolumns ();
Iterator ite = columns. keyset (). iterator ();
While (ITE. hasnext ())
{
String columnname = ite. Next (). tostring ();
Writer. addattribute (columnname, columns. Get (columnname ));
}
Writer. endnode ();
}
Writer. endnode ();
Writer. underlyingwriter ();
}

Public object unmarshal (hierarchicalstreamreader reader,
Unmarshallingcontext arg1 ){
Schemaobject schema = new schemaobject ();
Schema. setjdbcdriverclass (reader. getattribute (schemaobject. jdbc_driver_class ));
Schema. seturl (reader. getattribute (schemaobject. url ));
Schema. setuser (reader. getattribute (schemaobject. User ));
Schema. setpassword (reader. getattribute (schemaobject. Password ));
List <tableobject> tables = new arraylist <tableobject> ();
While (reader. hasmorechildren ()){
Reader. movedown ();
Schemaobject. tableobject table = new schemaobject (). New tableobject ();
Table. setname (reader. getattribute (schemaobject. table_name ));
Tables. Add (table );
Reader. moveup ();
}
Schema. settables (tables );
Return Schema;
}
Public Boolean canconvert (class arg0 ){
Return arg0.equals (schemaobject. Class );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.