需要用 jsp 页面显示 xml 响应。我正在获取 xml 对象“vehicleOrderResponse”的对象,如果我将在新文件中显示此对象,则 xml 显示成功,但未在 jsp 页面中获取 xml 数据。提供任何代码以在 jsp 页面中显示 xml 响应?
编写代码:
try
{
SAMSServiceAdaptor serviceadaptor = new SAMSServiceAdaptor();
VehicleOrderDetailRequestType vehicleOrderDetailRequest = serviceadaptor.createRequest(
vin, bodyCode, dealerCode, ordernumber, modelYear);
vehicleOrderResponse = SAMSServiceLocator.getSAMSServicePort().retrieveVehicleOrderDetail(vehicleOrderDetailRequest);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
JAXBContext context = JAXBContext
.newInstance(VehicleOrderDetailResponseType.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
true);
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
marshaller.marshal(vehicleOrderResponse, bytes);
String sb = new String(bytes.toByteArray());
String responseXML = sb.trim();
if(responseXML!=null){
out.println(responseXML.trim().substring("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>".length()));
}else{
out.println("Response was null, Please check input paramters! ");
}
}
catch(Exception ex)
{
out.println(ex.toString());
}
以格式获取 XML 响应:-
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:VehicleOrderDetailResponseType xmlns="urn:ford/interface/VehicleOrder/UnitDetail/v2" xmlns:ns2="urn:ford/VehicleOrder/UnitDetail/v2.0">
<Orders>
<ModelYear>2014</ModelYear>
<Dealer>13058</Dealer>
<Body>P8J</Body>
<ItemNo>445S </ItemNo>
<Vin>1ZVBP8JZ3E5223527</Vin>
<Division>F </Division>
<GsdbSiteCode>G9W1A</GsdbSiteCode>
<CurrentBuildWeek>2013-04-01Z</CurrentBuildWeek>
<ReceiptDate>2013-03-07Z</ReceiptDate>
<VehicleLineDescription>Mustang</VehicleLineDescription>