NewBee Developer NewBee Developer - 1 month ago 6
Java Question

Escape XML tags in textarea

I am working on spring boot with jsp in which I have a requirement to show an XML string in a jsp textarea. I ran into a strange problem while displaying the XML. The first tag just got ripped off and an extra

">
added at the end

Note.xml

<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>


greetings.jsp

<!DOCTYPE html>

<html>
<head>
<meta charset="UTF-8">
<title>Hello</title>
</head>
<body>
<form action="/smartxml/openxml" method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="Open" name="btnOpen" /><br/><br/>
<textarea name="txtXml" rows="45" cols="223" value="${fileContent}"></textarea>
</form>
</body>
</html>


IndexController.java

@Controller
public class IndexController {
@RequestMapping(params = "btnOpen", method = RequestMethod.POST)
public String uploadFile(@RequestParam("file") MultipartFile file, Model model) {
try {
InputStream is = file.getInputStream();
StringWriter writer = new StringWriter();
IOUtils.copy(is, writer, StandardCharsets.UTF_8);
String fileContent = writer.toString();
model.addAttribute("fileContent", xmlOperation.readXml(is));
} catch (IOException e) {
System.out.println(e.getMessage());
}

return "greeting";
}
}


Output

<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>">


Any help would be appreciated..

Answer
<textarea name="txtXml" rows="45" cols="223">${fileContent}</textarea>