java对象转soap_使⽤jaxb实现对象与xml之间的转换,并且是
soap报⽂与对象转换
使⽤jaxb 实现对象与xml之间的转换,并且是 soap报⽂与对象转换
1.测试⽤例
public class TestJaxb {
@Test
public void testToObject(){
JaxbBinder jaxbBinder = new JaxbBinder(SoapEnvelope.class);
URL url = Class().getClassLoader().getResource("l");
String xml = null;
try {
xml = adFileToString(new Path())) ;
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
SoapEnvelope envelope =jaxbBinder.fromXml(xml);
System.out.println("getOrderNo="+Body().getReceB2COrderResponseList().get(0).getOrderNo());
System.out.println("\ndata="+ (envelope).toString());
}
@Test
public void testToXml(){
ReceB2COrderRequest re = new ReceB2COrderRequest();
re.setBuyerContact("test111123123");
re.setBuyerId("asdfsadfasdf");
re.setBuyerName("aaaaaaaaaaa");
SoapBody body = new SoapBody();
body.setReceB2COrderRequestList(Arrays.asList(re));
SoapEnvelope envelope = new SoapEnvelope(null,body);
JaxbBinder jaxbBinder = new JaxbBinder(SoapEnvelope.class); String xml =Xml(envelope,"utf-8");
System.out.println("\nxml="+ xml);
}
2.转换⼯具
l.bind.*;
l.namespace.QName;
import java.io.File;
import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Collection;
/**
* 使⽤Jaxb2.0实现XMLJava Object的Binder.
* 特别⽀持Root对象是List的情形.
*/
public class JaxbBinder {
//多线程安全的Context.
private JAXBContext jaxbContext;
/**
* @param types 所有需要序列化的Root对象的类型.
*/
public JaxbBinder(Class>... types) {
try {
jaxbContext = wInstance(types);
} catch (JAXBException e) {
throw new RuntimeException(e);
}namespace是干嘛的
}
/**
* Java Object->Xml.
*/
public String toXml(Object root, String encoding) {
try {
StringWriter writer = new StringWriter();
createMarshaller(encoding).marshal(root, writer);
String();
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
/**
* Java Object->Xml, 特别⽀持对Root Element是Collection的情形.
*/
@SuppressWarnings("unchecked")
public String toXml(Collection root, String rootName, String encoding) {
try {
CollectionWrapper wrapper = new CollectionWrapper();
JAXBElement wrapperElement = new JAXBElement(new QName(rootName), CollectionWrapper.class, wrapper);
StringWriter writer = new StringWriter();
createMarshaller(encoding).marshal(wrapperElement, writer);
String();
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
/**
* Xml->Java Object.
*/
@SuppressWarnings("unchecked")
public T fromXml(String xml) {
try {
StringReader reader = new StringReader(xml);
return (T) createUnmarshaller().unmarshal(reader);
} catch (JAXBException e) {
throw new RuntimeException(e);
}
/**
* 创建Marshaller, 设定encoding(可为Null).
*/
public Marshaller createMarshaller(String encoding) {
try {
Marshaller marshaller = ateMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); if (StringUtils.isNotBlank(encoding)) {
marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
}
return marshaller;
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
/**
* 创建UnMarshaller.
*/
public Unmarshaller createUnmarshaller() {
try {
ateUnmarshaller();
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
/**
* 封装Root Element 是 Collection的情况.
*/
public static class CollectionWrapper {
@SuppressWarnings("unchecked")
@XmlAnyElement
protected Collection collection;
@SuppressWarnings("unchecked")
public T fromXML(String fileName) {
return (T)fromXML(new File(fileName));
}
@SuppressWarnings("unchecked")
public T fromXML(File file) {
try {
Unmarshaller unmarshaller = createUnmarshaller();
return (T) unmarshaller.unmarshal(file);
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
@SuppressWarnings("unchecked")
public T fromXML(InputStream stream) {
try {
Unmarshaller unmarshaller = createUnmarshaller();
return (T) unmarshaller.unmarshal(stream);
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
3.XML报⽂实例(l)
爱奇艺
332396
30.00
2014081809451210442
OT001
72041119-5d40-42dc-afec-980cbf0122a4
4A8A8F14C3BED9151F51C0096319A664AAFD91E5B2982EAB21DDE8C557FC5A52B887E8CBF9AE3B19 C20140818094536462081
00
20140818094518

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。