需要使用jsp页面显示xml响应。我正在获取xml对象的对象“vehicleOrderResponse”,如果我将在新文件中显示此对象,则xml显示成功,但未在jsp页面中获取xml数据。提供任何代码片段以在jsp页面中显示xml响应?
Jsp页面代码:
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:VehicleOrderType xmlns="urn:ford/interface/VehicleOrder/UnitDetail/v2" xmlns:ns2="urn:ford/VehicleOrder/UnitDetail/v2.0">
<VehicleOrder>
<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>
<BodyDescription>P8J Coupe Shelby GT500</BodyDescription>
<Status>
<ns2:Status>Sold </ns2:Status>
<ns2:StatusDateTime>2013-05-04T04:00:00.000Z</ns2:StatusDateTime>
</Status>
</VehicleOrder>
</ns2:VehicleOrderType>
答案 0 :(得分:0)
您可以通过将预先格式化的XML内容包装在<pre>
标记内来预先指定格式,以防止浏览器将其解释为HTML并按原样显示:
<pre>
<%
// your existing code
%>
</pre>
更好的选择是将JSP scriptlet <% %>
代码移出到Servlet中,该Servlet将XML内容设置为请求属性,然后使用{{}将请求转发给JSP 1}}。
RequestDispatcher
然后,最后在您的JSP中使用JSTL的String responseXML = new String(bytes.toByteArray()).trim();
if (responseXML != null) {
request.setAttribute("responseXML", responseXML.substring(
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>".length()));
}
标记。这将自动为您转义XML。
<c:out>