【问题标题】:Display XML in jsp file using Spring MVC使用 Spring MVC 在 jsp 文件中显示 XML
【发布时间】:2017-10-25 18:54:02
【问题描述】:

我需要在 jsp 中显示一个 XML 文件。代码应该转到特定位置并读取 xml,然后将其显示在 jsp 中。

我已经完成了文件读取部分,但是当我尝试在 jsp 中显示时出现错误。我尝试了不同的方法,比如转义字符、设置内容类型,但每次都得到不同的错误。

其中一个是 -

XML 解析错误:找不到元素位置:

http://localhost:8080/viewXmlFile

第 8 行,第 12 列:

有什么方法可以在 jsp 中显示 XML 文件吗?我正在使用 Spring MVC。

@RequestMapping(value = "/viewXmlFile", method = RequestMethod.GET)
public ModelAndView viewXmlFile(HttpServletRequest request, HttpServletResponse response) {
    String path = //sec15/folder/myXmlFile.xml;

    StringBuffer data = new StringBuffer();
    try(BufferedReader reader = Files.newBufferedReader(Paths.get(path), StandardCharsets.UTF_8);) {
        List<String> collect = reader.lines().collect(Collectors.toList());

        for (String line : collect) {
            data.append(line + CommonConstants.FILE_NEXT_LINE);
        }
    } catch (Exception e) {
    }

    ModelAndView model = new ModelAndView("viewXmlFile");
    model.addObject("xmlData", data.toString());
    return model;
}

这是JSP文件-

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<body>
<pre>
    <c:out value="${xmlData}" />
</pre>
</body>
</html>

这是 XML,我正在尝试显示-

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns14:Appearance xmlns:ns2="http://www.example.com/ObjectMetadata/9.0" xmlns:ns3="http://www.example.com/TransactionMetadata/9.0" xmlns:ns14="http://www.example.com/Appearance/2.56">
    <ns3:TransactionMetadata>
        <SourceSystem>Alpha</SourceSystem>
        <TransactionType>Appearance</TransactionType>
        <UniqueTransactionId>8d797d156487</UniqueTransactionId>
        <TransactionDateTime>2017-05-23T03:04:48.025+02:00</TransactionDateTime>
    </ns3:TransactionMetadata>
    <Appearance>
        <ns2:ObjectMetadata>
            <ActionType>Update</ActionType>
            <BusinessObjectName>Appearance</BusinessObjectName>
        </ns2:ObjectMetadata>
        <AppearanceUID>A500003410</AppearanceUID>
        <AppearanceNumber>001</AppearanceNumber>
        <AppearanceName>1495501</AppearanceName>
        <CreationDate>2017-05-23T03:04:37+02:00</CreationDate>
        <ModifiedDate>2017-05-23T03:04:38+02:00</ModifiedDate>
    </Appearance>
</ns14:Appearance>

【问题讨论】:

标签: java xml spring jsp spring-mvc


【解决方案1】:

更改 jsp 文件后,我可以在 jsp 上查看 XML。

这是我现在在jsp中的代码-

<%@page contentType="text/xml" pageEncoding="UTF-8"%>${xmlData}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-20
    • 1970-01-01
    • 1970-01-01
    • 2012-05-17
    • 2016-08-21
    • 1970-01-01
    • 2017-11-20
    • 1970-01-01
    相关资源
    最近更新 更多