<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>
package com.tqmall.utils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
/**
*
* @author yibo.liu
*/
public class JacksonMapper {
/**
* can reuse, share globally
*/
private static final ObjectMapper object = new ObjectMapper();
/**
* can reuse, share globally
*/
private static final XmlMapper xml = new XmlMapper();
/**
* private constructor
*/
private JacksonMapper() {
}
/**
* return a ObjectMapper that is singleton
* @return
*/
public static ObjectMapper getObjectMapper() {
return object;
}
/**
* return a XmlMapper that is singleton
* @return
*/
public static XmlMapper getXmlMapper() {
return xml;
}
}
package com.tqmall.elasticsearch.nindex.writer.slaveproxy;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
/**
* 通用方法,用于转换xml到bean,或者bean 到xml
* @author yibo.liu
*/
public class JacksonUtils {
public static void main(String[] args) throws IOException {
new SlaveIndexXmlParser().parse();
}
/**
* XML To Object
*
* @param xmlPath
* @param cls
* @param <T>
* @return
* @throws IOException
*/
public static <T> T xmlToBean(String xmlPath, Class<T> cls) throws IOException {
XmlMapper xml = JacksonMapper.getXmlMapper();
T obj = xml.readValue(new File(xmlPath), cls);
return obj;
}
/**
* @param xmlFile
* @param cls
* @param <T>
* @return
* @throws IOException
*/
public static <T> T xmlToBean(File xmlFile, Class<T> cls) throws IOException {
XmlMapper xml = JacksonMapper.getXmlMapper();
T obj = xml.readValue(xmlFile, cls);
return obj;
}
/**
* @param xmlInputStream
* @param cls
* @param <T>
* @return
* @throws IOException
*/
public static <T> T xmlToBean(InputStream xmlInputStream, Class<T> cls) throws IOException {
XmlMapper xml = JacksonMapper.getXmlMapper();
T obj = xml.readValue(xmlInputStream, cls);
return obj;
}
public static <T> String beanToXml(T bean) throws JsonProcessingException {
XmlMapper xml = JacksonMapper.getXmlMapper();
String string = xml.writeValueAsString(bean);
return string;
}
}
这一部分不是重点。只是简单描述。
1. 扩展为 .xsd 的文件为 XML Schemas Definition ,所以我们需要先定义这个文件。(在项目中可以直接套用spring maven 等包的定义,改成自己的)
2. xmlns:xs=”http://www.w3.org/2001/XMLSchema” 中 xmlns 是 xml namespace ,即命名空间, :xs 是别名定义,如果有引用多个,用于区分不同的命名空间,避免命令冲突。而引号里面的,就是xml定义文档了。这个是网际网路联盟(World Wide Web Consortium,简称W3C)的定义,也就是定义 xsd 文档 的定义。
3. targetNamespace=”http://www.tqmall.com/search/slave/0.0.1” xmlns=”http://www.tqmall.com/search/slave/0.0.1” 这两个中,targetNamespace是定义这个文档的命名空间,也就是你定义的这个文档,而后面那个就是直接引用了自己,干什么用的,不清楚。
4. 然后,在使用上面定义的xsd的xml里面
- 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 是用来导入需要用到的 xsd 定义文件,必需。
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/zhanlanmg/article/details/48027833