[英]How to get xml data with jsp page
Need to show xml response with jsp page. 需要显示带有jsp页面的xml响应。 I am getting Object of xml object"vehicleOrderResponse", If I will show this object in new file then xml is showing successfully, but not getting xml data in jsp page. 我正在获取xml对象“ vehicleOrderResponse”的对象,如果我将在新文件中显示此对象,则xml显示成功,但在jsp页面中未获取xml数据。 Provide any piece of code to show xml response in jsp page? 提供任何代码来在jsp页面中显示xml响应?
Jsp page code: 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 response in file:- 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>
You can specify your XML content pre-formatted by wrapping it within <pre>
tags to prevent the browser from trying to interpret it as HTML and display it as is: 您可以通过将XML内容包装在<pre>
标记中来指定预格式化的XML内容,以防止浏览器尝试将其解释为HTML并按原样显示:
<pre>
<%
// your existing code
%>
</pre>
A better option would be to move out your JSP scriptlet <% %>
code into a Servlet that sets the XML content as a request attribute before forwarding the request to the JSP using a RequestDispatcher
. 更好的选择是将JSP scriptlet <% %>
代码移到Servlet中,该Servlet将XML内容设置为请求属性,然后再使用RequestDispatcher
将请求转发到JSP。
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()));
}
Then, finally within your JSP use JSTL's <c:out>
tag. 然后,最后在您的JSP中使用JSTL的<c:out>
标记。 This would escape the XML automatically for you. 这将自动为您转义XML。
<p>
<c:out value="${responseXML}" />
</p>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.