One of the issues we're going to explore today is how to convert XML-formatted strings to objects and objects to XML strings. Brief Introduction
Today, although most of the data is transmitted in JSON format, XML will have some old projects in use as a versatile programmer. How can we not use it? The normal old-fashioned way is to do a series of operations by getting the nodes, and the individual feels too complex and cumbersome. Today we recommend a simple set of APIs. XStream class. Good nonsense not much to say, directly on the code. (in order to tell more clearly.) We don't use any annotations in the whole process, just call the API to achieve the effect. If you understand the principle, look at the syntax of the annotation and then use the object to go to XML
Let's start with the simple object to XML as an example, because the XML object will be relatively complex, we are from shallow to deep. The first step: Import jar bag, I Project to Gradle build. The jar package download reference is as follows (common items from the Internet to find a jar package placed in the Lib folder can be)
Compile ("com.thoughtworks.xstream:xstream:1.4.10") Step Two: Create object (create user and customer, don't be misled by name ...) It doesn't matter.)
1. Create the User object (use Lombok to create the Getset method, do not understand the classmate directly created by hand)
Package com.kingboy.springboot.domain.dto;
Import Lombok. Data;
Import java.util.List;
/**
* Created by Beyondli on 2017/6/14.
* *
@Data public
class User {
private String name;
Private Integer age;
Private list<customer> Customer;
2. Create the Customer object (IBID.)
Package com.kingboy.springboot.domain.dto;
Import Lombok. Data;
/**
* Created by Beyondli on 2017/6/14.
*
/@Data public
class Customer {
private String commodity
}
Step three: Test class
Create the user object and the Customer object and assign the
user user = new user ();
Customer customer1 = new Customer ();
Customer Customer2 = new Customer ();
Customer1.setcommodity ("Product 1");
Customer2.setcommodity ("Product 2");
list<customer> list = new arraylist<> ();
List.add (customer1);
List.add (customer2);
User.setname ("Beyondli");
User.setage (a);
User.setcustomer (list);
Create XStream Object
xstream XStream = new XStream ();
Call ToXML to turn the object into a string
s = xstream.toxml (user);
System.out.println (s);
Fourth step: Output Results
Fifth step: Optimize
The above we see the result, but seemingly and we want is not the same, how some are full path name. The workaround here is simple. Mainly talk about ideas. Because it is related to the problem of whether or not to encapsulate errors when XML is turned. By default, when an object has no name, such as the first object and the type of the generic in the collection type in the object, we have no way of naming him, by default it is a full path name. So if we don't handle it, the XML that comes in when we turn the XML directly is the name, and if we call the API package directly, we get an error that the encapsulation fails because the name is inconsistent. So we're going to alias the full path name class ( note. It is best not to alias all objects without brain, although it can also solve the problem. But we'd better understand, just alias the class that is needed , and the code is as follows
Create the user object and the Customer object and assign the
user user = new user ();
Customer customer1 = new Customer ();
Customer Customer2 = new Customer ();
Customer1.setcommodity ("Product 1");
Customer2.setcommodity ("Product 2");
list<customer> list = new arraylist<> ();
List.add (customer1);
List.add (customer2);
User.setname ("Beyondli");
User.setage (a);
User.setcustomer (list);
Create XStream Object
xstream XStream = new XStream ();
Alias Xstream.alias for the specified class
("User", user.class);
Xstream.alias ("Customer", customer.class);
Call ToXML to turn the object into a string
s = xstream.toxml (user);
System.out.println (s);
Results:
Students who want to read here will understand the alias above, when to alias, when not to alias, so that when the XML to turn objects without any deviations and errors. XML Spin Object First step: Import the jar package. The second step: Create user and Customer objects. Part III: Test class
First of all, we show that the above hint does not have an alias error problem, I simulated an XML type of string, but not alias
Simulate an XML format string
XML = "<user>\n" +
" <name>beyondli</name>\n" +
" < age>23</age>\n "+
" <customer>\n "+
" <customer>\n "+
" < Commodity> commodity 1</commodity>\n "+
" </customer>\n "+
" <customer>\n "+
" <commodity> merchandise 2</commodity>\n" +
" </customer>\n" +
" </ customer>\n "+
" </user> ";
Create XStream Object
xstream XStream = new XStream ();
Alias, do not write first, let it error
User user2 = (user) xstream.fromxml (XML);
System.out.println (User2);
Results
And obviously, although we object user and customer name are not wrong. But the error is not found. This error is combined with the result of the above object's conversion to XML. We can clearly see where the problem is.
The correct code is as follows
Simulate an XML format string
XML = "<user>\n" +
" <name>beyondli</name>\n" +
" < age>23</age>\n "+
" <customer>\n "+
" <customer>\n "+
" < Commodity> commodity 1</commodity>\n "+
" </customer>\n "+
" <customer>\n "+
" <commodity> merchandise 2</commodity>\n" +
" </customer>\n" +
" </ customer>\n "+
" </user> ";
Create XStream Object
xstream XStream = new XStream ();
The alias should be
xstream.alias ("user", User.class) relative to the XML name;
Xstream.alias ("Customer", customer.class);
User User2 = (user) xstream.fromxml (XML);
System.out.println (User2);
Results:
Conversion successful
In fact, the conversion between the XML and the object is not difficult, but often error and unsuccessful in fact, because of the problem in the alias. That cannot be matched. I hope this article can help more students to solve the problem.
The above view is only a personal understanding. If there is a mistake or imperfect, also hope that the common growth