XML与对象之间的互相转换
程序员文章站
2024-03-15 08:22:05
...
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import org.apache.axis.utils.ByteArrayOutputStream;
import org.apache.log4j.Logger;
import org.dom4j.DocumentException;
import com.dhl.eplatform.bns.easypay.pojo.ShipmentCustomersGetResponse;
public class Test {
private static Logger log=Logger.getLogger(Test.class);
public static void main(String[] args) throws DocumentException, JAXBException {
test();
}
public static void test() throws DocumentException, JAXBException{
String resultXml="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><ns2:Response xmlns:ns2=\"http://www.cn.dhl.com\" xmlns:ns4=\"http://www.dhl.com/Express/CM/CM_BOStatusRsp/v3\" xmlns:ns3=\"http://www.dhl.com/Express/CM/CM_BkgMsg/v3\" xmlns:ns9=\"Booking.SOAP.BookingCancel\" xmlns:ns12=\"http://www.cn.dhl.com/WsService_datatypes\" xmlns:ns5=\"Booking.SOAP.BookingStatusGet\" xmlns:ns13=\"http://www.dhl.com/Express/CM/CM_GenericRequest/v3\" xmlns:ns6=\"Booking.SOAP.BookingEventQuery\" xmlns:ns10=\"http://www.dhl.com/Express/CM/CM_BOStatusRqst/v3\" xmlns:ns7=\"Booking.SOAP.BookingCreate_v2\" xmlns:ns11=\"http://www.dhl.com/Express/CM/CM_GenRqstMsg/v3\" xmlns:ns8=\"Booking.SOAP.BookingUpdate\"><ResponseHeader><SendTime>2018-11-20 10:16:20</SendTime><SerialNo>151106134833</SerialNo><ChannelName>adc</ChannelName><Message>查询信息成功!</Message><MessageCode>Success</MessageCode></ResponseHeader><ShipmentCustomersInvceGroup><InvoiceNumber>D10734663</InvoiceNumber><InvoiceDate>2018-10-16</InvoiceDate><InvoiceTime>02:16:58</InvoiceTime><InvoiceZone>+00:00</InvoiceZone><AccountNo>DUTYCNJQN</AccountNo><AccountCtry>CN</AccountCtry><PinCode>EYT78V</PinCode><InvoiceGrossAmount>467.96</InvoiceGrossAmount><InvoiceAdjustedAmount>467.96</InvoiceAdjustedAmount><InvoiceNetAmount>467.96</InvoiceNetAmount><ShipmentCustomersGroup><ShipId>219882</ShipId><AwbNo>4166482515</AwbNo><ItemNo>0</ItemNo><OrignServiceArea>VIF</OrignServiceArea><OrignCode>RTK</OrignCode><DestServiceArea>SHA</DestServiceArea><DestCode>HQW</DestCode><Email>[email protected]</Email><TelNum>+64279254100</TelNum><ContactName>KIDS COLLECTION</ContactName><ShipmentExtraCharge><Secc>DT</Secc><Sect>D</Sect><Secd>IMPORT EXPORT DUTIES_N</Secd><Secq>1</Secq><Secs>0</Secs><Scqn>1</Scqn></ShipmentExtraCharge><ShipmentExtraCharge><Secc>L1</Secc><Sect>D</Sect><Secd>IMPORT VAT_N</Secd><Secq>1</Secq><Secs>0</Secs><Scqn>2</Scqn></ShipmentExtraCharge><ShipmentExtraCharge><Secc>AV</Secc><Sect>D</Sect><Secd>DHL-ADVANCE PAYMENTE_Y</Secd><Secq>1</Secq><Secs>0</Secs><Scqn>3</Scqn></ShipmentExtraCharge><Errs>100000</Errs><Errt>Service operation execution finished successfully</Errt><Erri>Service operation completed successfully</Erri></ShipmentCustomersGroup></ShipmentCustomersInvceGroup></ns2:Response>";
log.info("获得已付金额resultXml=" + resultXml);
JAXBContext context = JAXBContext.newInstance(ShipmentCustomersGetResponse.class); // 获取上下文对象
//XML转对象
Unmarshaller unmarshaller=context.createUnmarshaller();
InputStream inputStream=new ByteArrayInputStream(resultXml.getBytes());
ShipmentCustomersGetResponse customersGetResponse=(ShipmentCustomersGetResponse) unmarshaller.unmarshal(inputStream);
log.info(customersGetResponse.getResponseHeader().getChannelName());
//对象转XML
OutputStream outputStream=new ByteArrayOutputStream();
Marshaller marshaller = context.createMarshaller();
marshaller.marshal(customersGetResponse, outputStream);
log.info(outputStream.toString());
}
}
注意点:所有实体类必须加入注解
推荐阅读
-
java 中 十进制与二进制的互相转换
-
XML与对象之间的互相转换
-
利用反射实现MapStr、MapObj与bean之间的转换
-
GSON实现Java对象与JSON格式对象相互转换的完全教程
-
explode,implode逗号与数组之间的转换
-
GSON实现Java对象与JSON格式对象相互转换的完全教程
-
Java 和 Javascript 的 Date 与 .Net 的 DateTime 之间的相互转换
-
Java中字符串与byte数组之间的相互转换
-
Java 和 Javascript 的 Date 与 .Net 的 DateTime 之间的相互转换
-
JSON的String字符串与Java的List列表对象的相互转换