Hi

I have a small issue with displaying xml document when tried retriving in jsp page.

This is the complete code:

<%@ page import="java.io.*" %>
<%@ page import=" org.xmldb.api.base.*" %>
<%@ page import=" org.xmldb.api.modules.*" %>
<%@ page import=" org.xmldb.api.*" %>
<%@ page import=" javax.xml.transform.OutputKeys" %>
<%@ page import=" javax.servlet.jsp.*" %>
<%@ page import=" javax.servlet.jsp.tagext.*" %>
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
Collection col = null;
XMLResource res = null;
try {
     Class cl = Class.forName("org.exist.xmldb.DatabaseImpl");
     Database database = (Database) cl.newInstance();
     DatabaseManager.registerDatabase(database);
     col = DatabaseManager.getCollection("xmldb:exist://localhost:8080/exist/xmlrpc/db/NeuroML/Level1");
 col.setProperty(OutputKeys.INDENT, "no");
 res = (XMLResource)col.getResource("rest.xml");
     out.println(res.getContent());              
}
 catch (Exception e)  
   {   out.println("hererere");
out.println(e);
e.printStackTrace();
    }   finally { }  
%>
</body>
</html>

It gives me content on the file, in my case contents of "rest.xml", but what i need is to display the original xml tree structure in the browser, not just the contents present between tags.
What i am missing in the code to make it happen??

Thanks
Sidharth

This seems to be a problem with content type of your servlet response. You need to set the content type to text/xml instead of text/html for the browser to render your response in a "tree" format.

That doesnt changed any thing. Result is the same.
Any other suggestion?

The "tree" structure of XML is shown only if you render an XML file, not when you a try to render a XML file within an HTML. If your requirement is the latter i.e. to mix and match HTML and XML you'd need to use a Javascript library to emulate the same. For e.g. the following code works and shows a tree structured XML file in IE and FF:

<%@ page language="java" contentType="text/xml; charset=UTF-8"
	pageEncoding="UTF-8"%>
<html>
 <head>
  <title>
    Insert title here
  </title>
 </head>
 <body>HI</body>
</html>

Thanks a lot, my code worked fine now. Thanks again.

Hey i have one more question.
I am using eXist native XMl database, and want the jsp code to retrieve the stored xml file from db and show up in the browser.
How should i go about it. Right now i now how to retrieve a file present in the db, by registering the db inside the jsp code and giving file path and name.

My final aim is to have a jsp page showing up collections stored in the db, and the user can access the collection and see xml files in them, just read accessibility.

Hey i have one more question.
I am using eXist native XMl database, and want the jsp code to retrieve the stored xml file from db and show up in the browser.
How should i go about it. Right now i now how to retrieve a file present in the db, by registering the db inside the jsp code and giving file path and name.

My final aim is to have a jsp page showing up collections stored in the db, and the user can access the collection and see xml files in them, just read accessibility.

A better way would be to move the entire XML loading logic to a servlet. Also, register/load the required classes/resources only once instead of doing it for every request. Create your own context listener which would be invoked after the servlet context has been initialized (once for every web application as per servlet specification) and do the common loading and registration there. Your servlet should be smart enough to know the intent of the user. If the request is for something like "http://localhost:8080/myapp/all" then display the links to list of all the XML files you have in your database. If the URL is something like "http://localhost:8080/myapp/view/XXX", then show the XML file XXX to the user and so on. I hope you catch the drift..

Also, I've never worked with the native XMLDB product you mention so I won't know what would be an efficient way of initializing/loading the driver/database. Look into the products documentation for more details.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.