Reputation: 73
I need to display an XML file in jsp. The code should go to a particular location and read xml and then show it in a jsp.
I have done the file reading part but when I try to show that in jsp I am getting error. I tried different ways, like escaping char, setting content type, but every time I get a different error.
One of them is -
XML Parsing Error: no element found Location:
http://localhost:8080/viewXmlFile
Line Number 8, Column 12:
Is there any way I can show the XML file in jsp? I am using 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;
}
This is the JSP file-
<%@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>
This is the XML, I am trying to show-
<?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>
Upvotes: 1
Views: 1652
Reputation: 73
After changing the jsp file I am able to view the XML on jsp.
This is the code I have in jsp now-
<%@page contentType="text/xml" pageEncoding="UTF-8"%>${xmlData}
Upvotes: 2