XML and Bean Mutual transfer maven Dependency package
<properties> <json-version>2.4.2</json-version> </Properties> <dependency> <groupId>Com.fasterxml.jackson.core</groupId> <artifactid>Jackson-core</artifactid> <version>${json-version}</version> </Dependency> <dependency> <groupId>Com.fasterxml.jackson.core</groupId> <artifactid>Jackson-annotations</artifactid> <version>${json-version}</version> </Dependency> <dependency> <groupId>Com.fasterxml.jackson.core</groupId> <artifactid>Jackson-databind</artifactid> <version>${json-version}</version> </Dependency> <dependency> <groupId>Com.fasterxml.jackson.dataformat</groupId> <artifactid>Jackson-dataformat-xml</artifactid> <version>${json-version}</version> </Dependency>
Jackson Xmlmapper Object
PackageCom.tqmall.utils;ImportCom.fasterxml.jackson.databind.ObjectMapper;ImportCom.fasterxml.jackson.dataformat.xml.XmlMapper;/** * * @author Yibo.liu * * Public class jacksonmapper { /** * Can reuse, share globally * * Private Static FinalObjectmapper Object =NewObjectmapper ();/** * Can reuse, share globally * * Private Static FinalXmlmapper XML =NewXmlmapper ();/** * Private constructor * * Private Jacksonmapper() { }/** * Return a objectmapper that's singleton * @return */ Public StaticObjectmapperGetobjectmapper() {returnObject }/** * Return a xmlmapper that's singleton * @return */ Public StaticXmlmapperGetxmlmapper() {returnxml }}
Conversion Tool Class
PackageCom.tqmall.elasticsearch.nindex.writer.slaveproxy;ImportCom.fasterxml.jackson.core.JsonProcessingException;ImportCom.fasterxml.jackson.dataformat.xml.XmlMapper;ImportJava.io.File;ImportJava.io.IOException;ImportJava.io.InputStream;/** * Generic method for converting XML to bean, or bean to XML * @author Yibo.liu */ Public class jacksonutils { Public Static void Main(string[] args)throwsIOException {NewSlaveindexxmlparser (). Parse (); }/** * XML to Object * * @param xmlpath * @param cls * @param <T> * @return * @throws ioexception * * Public Static<T> TXmltobean(String Xmlpath, class<t> CLS)throwsIOException {xmlmapper xml = Jacksonmapper.getxmlmapper (); T obj = Xml.readvalue (NewFile (Xmlpath), CLS);returnObj }/** * @param xmlfile * @param cls * @param <T> * @return * @throws ioexception * * Public Static<T> TXmltobean(File xmlfile, class<t> CLS)throwsIOException {xmlmapper xml = Jacksonmapper.getxmlmapper (); T obj = Xml.readvalue (xmlfile, CLS);returnObj }/** * @param xmlinputstream * @param cls * @param <T> * @return
* @throws ioexception * * Public Static<T> TXmltobean(InputStream Xmlinputstream, class<t> CLS)throwsIOException {xmlmapper xml = Jacksonmapper.getxmlmapper (); T obj = Xml.readvalue (Xmlinputstream, CLS);returnObj } Public Static<T> StringBeantoxml(T Bean)throwsjsonprocessingexception {xmlmapper xml = Jacksonmapper.getxmlmapper (); String string = Xml.writevalueasstring (bean);returnString }}
XML document Structure definition:
This part is not the point. Just a brief description.
1. Files that are extended to. xsd are XML Schemas definition, so we need to define this file first. (In the project, you can apply the definition of package like spring maven and change it to your own)
2. xmlns:xs= "Http://www.w3.org/2001/XMLSchema" in the xmlns is the XML namespace, the namespace,: XS is an alias definition, if there are multiple references, to distinguish between different namespaces, avoid command conflicts. Inside the quotation marks is the XML definition document. This is the definition of the Internet Consortium (World Wide Web Consortium, the World Network), which defines the XSD document.
3. Targetnamespace= "http://www.tqmall.com/search/slave/0.0.1" xmlns= "http://www.tqmall.com/search/slave/0.0.1" In both of these, targetnamespace is the namespace that defines the document, which is the document you define, and the latter is the direct reference to yourself, what to do with, not clear.
4. Then, in XML that uses the XSD defined above
-Xmlns= "http://www.tqmall.com/search/slave/0.0.1"
-Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
-Xsi:schemalocation= "http://www.tqmall.com/search/slave/0.0.1
-Http://www.tqmall.com/search/slave/slave-0.0.1.xsd "
-Xsi:schemalocation is required to import an XSD definition file that needs to be used.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java uses Jackson for XML bean Mutual transfer