开发者

Display xml file in the browser using java

开发者 https://www.devze.com 2023-02-26 08:51 出处:网络
I have an xml file in my computer and i want to display this xml in the browser using java. i have a jsp page and when entering this page i want to display xml file in the browser.

I have an xml file in my computer and i want to display this xml in the browser using java. i have a jsp page and when entering this page i want to display xml file in the browser. how can i do this in jsp page with java code. for example, my x开发者_开发百科ml path; C:\xmlexamlpes\sample.xml

how can i display this xml in browser through jsp


Try setting the contentType to text/xml and then write out the xml file. 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).


You could read the xml and render it in the 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.


You can also Try DOM and SAX Parsers.

Parsers in Java

0

精彩评论

暂无评论...
验证码 换一张
取 消