【发布时间】: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>
【问题讨论】:
-
请create a Minimal, Complete, and Verifiable example。编辑您的问题并提供 java 代码和
JSP。 -
@RubioRic 我也试过了。 "
-
正如@beckyang 所建议的,发布一个示例:您的 xml 是什么,您的读取代码是什么,控制器,视图(jsp 页面),......没有必要发布您的所有代码,只是与这个问题相关的部分。
-
@RubioRic 我也添加了代码。
标签: java xml spring jsp spring-mvc