[英]Display xml file in the browser using java
I have an xml file in my computer and i want to display this xml in the browser using java. 我的计算机中有一个xml文件,我想使用Java在浏览器中显示此xml。 i have a jsp page and when entering this page i want to display xml file in the browser.
我有一个jsp页面,进入此页面时,我想在浏览器中显示xml文件。 how can i do this in jsp page with java code.
我如何用Java代码在jsp页面中做到这一点。 for example, my xml path;
例如,我的xml路径; C:\\xmlexamlpes\\sample.xml
C:\\ xmlexamlpes \\ sample.xml
how can i display this xml in browser through jsp 我如何通过jsp在浏览器中显示此xml
Try setting the contentType to text/xml
and then write out the xml file. 尝试将contentType设置为
text/xml
,然后写出xml文件。 Sample code: 样例代码:
<%@ page contentType="text/xml" %>
<%@ page import="java.io.*" %>
<%
//dump out the file
BufferedReader in = new BufferedReader(new FileReader("path/to/file.xml"));
String line;
while((line = in.readLine())!=null){
out.print(line);
}
in.close();
%>
Alternatively, just redirect your jsp to the xml file or provide a link to it (provided that the file is public). 或者,只需将jsp重定向到xml文件或提供指向它的链接(前提是该文件是公共的)。
You could read the xml and render it in the jsp. 您可以阅读xml并将其呈现在jsp中。 Note that you should escape special characters like
<
and >
using xml entities or add <br>
for line breaks (string replace) in order to make the browser display it correctly and not try to interpret it. 请注意,您应使用xml实体转义
<
和>
类的特殊字符,或在换行符(字符串替换)中添加<br>
,以使浏览器正确显示它,而不是试图对其进行解释。
You can also Try DOM and SAX Parsers. 您也可以尝试DOM和SAX解析器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.