0

需要用 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>
4

1 回答 1

0

您可以通过将 XML 内容包装在标签中来指定预先格式化的 XML 内容,<pre>以防止浏览器尝试将其解释为 HTML 并按原样显示:

<pre>
    <%
      // your existing code
    %>
</pre>

更好的选择是将您的 JSP scriptlet<% %>代码移到一个 Servlet 中,该 Servlet 将 XML 内容设置为请求属性,然后使用RequestDispatcher.

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()));
}

然后,最后在您的 JSP 中使用 JSTL 的<c:out>标记。这会自动为您转义 XML。

<p>
    <c:out value="${responseXML}" />
</p>
于 2013-08-22T17:50:11.823 回答