mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-05-07 14:17:47 +08:00
优化代码
This commit is contained in:
parent
cd85078ddc
commit
ccf57aead5
@ -5,8 +5,6 @@ import java.io.InputStream;
|
|||||||
import java.io.StringReader;
|
import java.io.StringReader;
|
||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import javax.xml.bind.*;
|
import javax.xml.bind.*;
|
||||||
|
|
||||||
@ -17,7 +15,7 @@ import com.sun.xml.bind.marshaller.CharacterEscapeHandler;
|
|||||||
|
|
||||||
public class XmlTransformer {
|
public class XmlTransformer {
|
||||||
|
|
||||||
protected static final JAXBContext jaxbContext = initJAXBContext();
|
protected static final JAXBContext JAXB_CONTEXT = initJAXBContext();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* xml -> pojo
|
* xml -> pojo
|
||||||
@ -29,16 +27,14 @@ public class XmlTransformer {
|
|||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public static <T> T fromXml(Class<T> clazz, String xml) throws JAXBException {
|
public static <T> T fromXml(Class<T> clazz, String xml) throws JAXBException {
|
||||||
JAXBContext context = jaxbContext;
|
Unmarshaller um = JAXB_CONTEXT.createUnmarshaller();
|
||||||
Unmarshaller um = context.createUnmarshaller();
|
|
||||||
T object = (T) um.unmarshal(new StringReader(xml));
|
T object = (T) um.unmarshal(new StringReader(xml));
|
||||||
return object;
|
return object;
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public static <T> T fromXml(Class<T> clazz, InputStream is) throws JAXBException {
|
public static <T> T fromXml(Class<T> clazz, InputStream is) throws JAXBException {
|
||||||
JAXBContext context = jaxbContext;
|
Unmarshaller um = JAXB_CONTEXT.createUnmarshaller();
|
||||||
Unmarshaller um = context.createUnmarshaller();
|
|
||||||
InputSource inputSource = new InputSource(is);
|
InputSource inputSource = new InputSource(is);
|
||||||
inputSource.setEncoding("utf-8");
|
inputSource.setEncoding("utf-8");
|
||||||
T object = (T) um.unmarshal(inputSource);
|
T object = (T) um.unmarshal(inputSource);
|
||||||
@ -60,14 +56,13 @@ public class XmlTransformer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static <T> void toXml(Class<T> clazz, T object, Writer writer) throws JAXBException {
|
public static <T> void toXml(Class<T> clazz, T object, Writer writer) throws JAXBException {
|
||||||
JAXBContext context = jaxbContext;
|
Marshaller m = JAXB_CONTEXT.createMarshaller();
|
||||||
Marshaller m = context.createMarshaller();
|
|
||||||
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
|
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
|
||||||
m.setProperty(CharacterEscapeHandler.class.getName(), characterUnescapeHandler);
|
m.setProperty(CharacterEscapeHandler.class.getName(), CHAR_ESCAPE_HANDLER);
|
||||||
m.marshal(object, writer);
|
m.marshal(object, writer);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static CharacterEscapeHandler characterUnescapeHandler = new CharacterUnescapeHandler();
|
protected static final CharacterEscapeHandler CHAR_ESCAPE_HANDLER = new CharacterUnescapeHandler();
|
||||||
|
|
||||||
protected static class CharacterUnescapeHandler implements CharacterEscapeHandler {
|
protected static class CharacterUnescapeHandler implements CharacterEscapeHandler {
|
||||||
public void escape(char[] ac, int i, int j, boolean flag, Writer writer) throws IOException {
|
public void escape(char[] ac, int i, int j, boolean flag, Writer writer) throws IOException {
|
||||||
|
Loading…
Reference in New Issue
Block a user