In java, XStream is used to convert an object to an xml string or convert an xml string to an object =
We often have requirements, such as calling others' interfaces, and others will return data in json or xml format to you. Then we need to convert the data into corresponding objects for our own use;
Sometimes someone else calls our interface to provide data in xml or json format. If json is not mentioned here, it is easy to process, springMvc is easy to support.
The following section only describes the conversion between xml and objects.
First we need to introduce the jar package: xstream-1.4.8.jar
Paste the code below. The code is written in the springMvc environment.
@ Controllerpublic class NavigationAppDataController extends BaseController {@ SuppressWarnings ("unused") private static final Logger log = LoggerFactory. getLogger (NavigationAppDataController. class); @ Autowired private StoNavigationAppManager stoNavigationAppManager; @ RequestMapping (value = "/ITE/login") public void getNavigationXmlData (HttpServletRequest request, HttpServletResponse response) {List
FindStoreNavigationAppToXml = stoNavigationAppManager. findStoreNavigationAppData (); XStreamConfig xStreamConfig = new XStreamConfig (); // What does this mean? I will introduce it in the following text, which is probably the xStreamConfig that renames nodes in xml. getAliasMap (). put ("topcategories", List. class); xStreamConfig. getAliasMap (). put ("category", NavigationAppCommand. class); String xml = toXML (findStoreNavigationAppToXml, xStreamConfig); // you can specify the response encoding format for the returned data. setCharacterEncoding (CharsetType. UTF8); // sets the content type to determine the type of data that the browser will read from the returned data to display response. setContentType ("application/xml"); // write data to response and return it to the requester. getWriter (). write (xml );} /*** returns the ** @ param bean * Object * @ param xStreamConfig * configuration * @ return */public static String toXML (Object bean, XStreamConfig xStreamConfig) {XStream xstream = constructXStream (xStreamConfig); return xstream. toXML (bean);}/*** convert the xml string into the corresponding object ** @ param xml * @ return */public static
T fromXML (String xml) {return fromXML (xml, null);} public static
T fromXML (String xml, XStreamConfig xStreamConfig) {XStream xstream = constructXStream (xStreamConfig); return (T) xstream. fromXML (xml);} public static XStream constructXStream (XStreamConfig xStreamConfig) {XStream xstream = new XStream (); if (Validator. isNotNullOrEmpty (xStreamConfig )) {// ******************** alias ***************** * *************************** Map
> AliasMap = xStreamConfig. getAliasMap (); if (Validator. isNotNullOrEmpty (aliasMap) {for (Map. Entry
> Entry: aliasMap. entrySet () {String key = entry. getKey (); Class
Value = entry. getValue (); // rename the xstream class. alias (key, value );}} // ******************** implicitCollectionMap ****************** * ************************** Map
> ImplicitCollectionMap = xStreamConfig. getImplicitCollectionMap (); if (Validator. isNotNullOrEmpty (implicitCollectionMap) {for (Map. Entry
> Entry: implicitCollectionMap. entrySet () {String key = entry. getKey (); Class
Value = entry. getValue (); xstream. addImplicitCollection (value, key );}}}}}
/*** XStream related configuration. **/public final class XStreamConfig {/** alias. */private Map
> AliasMap = new HashMap
> ();/** Hide and hide the implicit set. For example, there is a list below. The second Class parameter in the generic type is ownerType. */private Map.
> ImplicitCollectionMap = new HashMap
> ();/*** Get the alias. ** @ return the aliasMap */public Map
> GetAliasMap () {return aliasMap;}/*** sets the alias. ** @ param aliasMap * the aliasMap to set */public void setAliasMap (Map
> AliasMap) {this. aliasMap = aliasMap;}/*** obtain the implicit set hiding and hiding. For example, there is a list below, and the second Class parameter in the generic type is ownerType. ** @ return the implicitCollectionMap */public Map
> GetImplicitCollectionMap () {return implicitCollectionMap;}/*** sets implicit set hiding and hiding. For example, there is a list below. The second Class parameter in the generic type is ownerType. ** @ param implicitCollectionMap * the implicitCollectionMap to set */public void setImplicitCollectionMap (Map
> ImplicitCollectionMap) {this. implicitCollectionMap = implicitCollectionMap ;}}
The implementation method is very simple. In addition, let's talk about the meaning of several response parameter settings.
Response. setCharacterEncoding (CharsetType. UTF8 );
Set the encoding format of the returned data. If this is not set, the data returned to the client request may be garbled. The encoding format must be unified.
Response. setContentType ("application/xml ");
Set the content type to determine the type of data that the browser will read from the returned data. If this is not set, the browser displays a large string in xml format, with no effect, after the settings, you can click expand and close as I did above, and the data and node color are significantly different for easy viewing.