XStream Utils 类:
/**
* XStream 仅支持not auto-detected下线程安全,所以每次调用都需要 new XStream()
* <em>Note, that this only applies if annotations are not auto-detected on-the-fly.</em>
*
* Created by brett on 2015/2/4.
*/
public class XStreamUtil {
/**
* java对象转xml
* @param root
* @return
*/
public static String toXml(UMSFX root){
XStream xstream = new XStream();
xstream.autodetectAnnotations(true);
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + xstream.toXML(root);
}
/**
* xml 转 java对象
* @param xml
* @return
*/
public static UMSFX toObject(String xml){
XStream xstream = new XStream();
xstream.autodetectAnnotations(true);
xstream.alias("UMSFX", UMSFX.class);
return (UMSFX) xstream.fromXML(xml);
}
/**
* xml 转 java对象
* @param in InputStream
* @return
*/
public static UMSFX toObject(InputStream in){
XStream xstream = new XStream();
xstream.autodetectAnnotations(true);
xstream.alias("UMSFX", UMSFX.class);
return (UMSFX) xstream.fromXML(in);
}
}
convert 实现自动识别java类型,并转换成java value-bean
/**
* Created by brett on 2015/2/3.
*/
public class RequestConvert implements Converter {
// public RequestConvert(Mapper mapper) {
// super(mapper);
// }
@Override
public boolean canConvert(Class type) {
return type.equals(UMSFX.class);
}
/**
* 输出
* <UMSFX xmlns="http://www.chinaums.com/UMSFX/1.0">
* <RequestName>A101AcctBalInqRq</RequestName>
* <A101AcctBalInqRq>
* <MsgHeader>
* <Ver>10001</Ver>
* <TransCode>201811</TransCode>
* <MerId>10001000000030</MerId>
* <SrcReqDate>20150203</SrcReqDate>
* <SrcReqId>123456754321345</SrcReqId>
* <ChannelId>10001000000030</ChannelId>
* <RespCode></RespCode>
* <RespMsg></RespMsg>
* </MsgHeader>
* <UserId>Testuserid009890</UserId>
* <Reserved></Reserved>
* </A101AcctBalInqRq>
* </UMSFX>
* @param source
* @param writer
* @param context
*/
@Override
public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
UMSFX umsfx = (UMSFX) source;
writer.addAttribute("xmlns", "http://www.chinaums.com/UMSFX/1.0");
// add RequestName tag
writer.startNode("RequestName");
context.convertAnother(umsfx.getRequestName());
writer.endNode();
// add Request tag
writer.startNode( umsfx.getMsgContent().getClass().getSimpleName() );
context.convertAnother(umsfx.getMsgContent());
writer.endNode();
}
/**
* xml to java object
* @param reader
* @param context
* @return
*/
@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
if( reader.getNodeName().equals("UMSFX") ) {
UMSFX umsfx = new UMSFX();
// reader.hasMoreChildren();
reader.moveDown();
String requestName = (String) context.convertAnother(reader, String.class);
umsfx.setRequestName(requestName);
// moveUp() and moveUp() 类似于 iterator.next()
reader.moveUp();
// reader.hasMoreChildren();
reader.moveDown();
// Class type = HierarchicalStreams.readClassType(reader, context.getRequiredType());
try {
Class type = Class.forName("com.xxx.vo." + reader.getNodeName());// vo都在同一个包下,NodeName名称即为Class类名称
umsfx.setMsgContent((MsgContent) context.convertAnother(reader, type));
return umsfx;
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
return null;
}
}