Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML page cannot be displayed

Status
Not open for further replies.

mpenny

Programmer
Jan 5, 2002
4
US
I'm two months into learning Java, am creating a website and have hit a wall. I get this xml message:

The XML page cannot be displayed
Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later.


--------------------------------------------------------------------------------

The system cannot locate the object specified. Error processing resource 'My xml code is:
<?xml version=&quot;1.0&quot; encoding=&quot;iso-8859-1&quot;?>

<!DOCTYPE ProductList [
<!ELEMENT ProductList (Product+)>
<!ELEMENT Product (Title, Author+, ISBN, Cost, InStock) >
<!ELEMENT Title (#PCDATA) >
<!ELEMENT Author (#PCDATA) >
<!ELEMENT ISBN (#PCDATA) >
<!ELEMENT Cost (#PCDATA)>
<!ELEMENT InStock (#PCDATA) >
<!ELEMENT OrderEntry (Product*)> ]>

<?xml-stylesheet type=&quot;text/xsl&quot; href=&quot;ProductList.xsl&quot; ?>


<ProductList>
<Product>
<Title></Title>
<Author></Author>
<ISBN></ISBN>
<Cost></Cost>
<InStock></InStock>
</Product>
</ProductList>

My servlet is:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.text.DecimalFormat;

public class ProductLookuptest extends HttpServlet {

private static void loadJDBCDriver () {
System.out.println (&quot;Loading database driver...&quot;);
try {

DriverManager.registerDriver ( new sun.jdbc.odbc.JdbcOdbcDriver() );
System.out.println (&quot;Database driver loaded...&quot;);
}
catch (SQLException e) {
System.err.println (e.getMessage () + e.toString() );
}
}

private static Connection getConnected () {
System.out.println (&quot;getting connection...&quot;);
try {
String jdbc_url = &quot;jdbc:eek:dbc:Book&quot;;
Connection conn = DriverManager.getConnection (jdbc_url);
System.out.println (&quot;connection created ...&quot;);
return conn;
}
catch (SQLException e) {
System.err.println (e.getMessage () + e.toString() );
System.exit(1); //Driver failure
}
return null; // never happen
}

private static Statement makeStatement (Connection conn) {
try {
System.out.println (&quot;Making a Statement...&quot;);
Statement stmt = conn.createStatement ();
System.out.println (&quot;Statement created...&quot;);
return stmt;
}
catch (SQLException e) {
System.err.println (e.getMessage () );
System.exit(2); //Driver failure
}
return null; // never happen
}

private static ResultSet openResultSet (Statement stmt, String query) {
try {
System.out.println (&quot;Creating resultSet...&quot;);
ResultSet rs = stmt.executeQuery (query);
System.out.println (&quot;Resultset created...&quot;);
return rs;
}
catch (SQLException e) {
System.err.println (e.getMessage () + e.toString() );
System.exit(3); //ResultSet error
}
return null; // never happen
}

private static void processResultSet (ServletOutputStream out, ResultSet rs) {
try {
DecimalFormat dollar = new DecimalFormat(&quot;0.00&quot;);
System.out.println (&quot;\n\n+++++ Processing ResultSet +++++\n&quot;);
while (rs.next() ) {
out.println (&quot;<Product>&quot;);
out.println (&quot;<Title>&quot; + rs.getString(1) + &quot;</Title>&quot;);
out.println (&quot;<Author>&quot; + rs.getString(2) + &quot;</Author>&quot;);
out.println (&quot;<ISBN>&quot; + rs.getString(3) + &quot;</ISBN>&quot;);
out.println (&quot;<Cost>&quot; + rs.getString(4) + &quot;<Cost>&quot;);
if (rs.getInt (5) > 0)
out.println (&quot;<InStock>Yes</InStock>&quot;);
else
out.println (&quot;<InStock>No</InStock>&quot;);
out.println (&quot;</Product>&quot;);
}
System.out.println (&quot;\n\n+++++ ResultSet Processed +++++\n&quot;);
}
catch (IOException e) {
System.err.println (e.getMessage ());
System.exit(4); //Output error
}
catch (SQLException e) {
System.err.println (e.getMessage () );
System.exit(5); //Processing error
}
}

private static void closeThingsDown (ResultSet rs, Statement stmt, Connection conn) {
try {
System.out.println (&quot;Closing Things Down...&quot;);
rs.close();
System.out.println (&quot;ResultSet Closed...&quot;);
stmt.close ();
System.out.println (&quot;Statement closed...&quot;);
conn.close();
System.out.println (&quot;Connection closed...&quot;);
}
catch (SQLException e) {
System.err.println (e.getMessage () );
System.exit(5); //Closure failure
}
}

public void doGet (HttpServletRequest request, HttpServletResponse response)
{
try {
loadJDBCDriver ();
Connection conn = getConnected ();
Statement stmt = makeStatement (conn);

response.setContentType (&quot;text/xml&quot;);
ServletOutputStream out = response.getOutputStream();
out.println (&quot;<?xml version=\&quot;1.0\&quot; standalone=\&quot;yes\&quot;?>&quot;); // xml header
out.println (&quot;<?xml-stylesheet type=\&quot;text/xsl\&quot; href=\&quot;/Book/ProductList.xsl\&quot;?>&quot;); // xsl ss
out.println (&quot;<ProductList>&quot;); // root element

// Prepare query based on uploaded user search criteria
String title = request.getParameter (&quot;title&quot;);
String author = request.getParameter (&quot;author&quot;);
String isbn = request.getParameter (&quot;ISBN&quot;);
String query = &quot;SELECT Title, Author, ISBN, QuantityOnHand FROM Products &quot;;


ResultSet rs = openResultSet (stmt, query);
processResultSet (out, rs); // print results as xml
closeThingsDown (rs, stmt, conn); // close db stuff

out.println (&quot;</ProductList>&quot;); // close root
}
catch (IOException e) {
System.err.println (e.getMessage () );
}
}

public void doPost (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet (request, response);
}
}

I don't understand what my problem is. Has anyone had this before? I could definately use your help.
 
MPenny --

Two thoughts that comes to mind... I've been beating my head against the XSL learning curve myself for the last few weeks and have seen that message more times than I care to admit ~smile~

First, I have found that from within my XML and XSL documents, relative URLs are unreliable. More often than not, links to imports, style sheets and images that use relative URLs fail. You might try specifying the full URI for ProductList.xsl

Second, it is way to easy to miss a closing tag and my experience with IE has been that it chokes when you do and the output message is often &quot;unhelpful&quot; in identifying the cause. However, if I load the XSL page (skipping the XML data) directly into IE, it will usually do a fairly good job of displaying exactly what it is whining about. I would suggest you load ProductList.xsl directly and check the IE output to see if it explains where the problem might be.

Good luck.
 
Thanks for your post!
I tried what you suggested, but still no luck. I looked through the XML, but am going to do it again (all the text just blends together after awhile).
 
mpenny --

If you'll e-mail me the XSL stylesheet and a sample XML data set for it, I'll look and see if I can figure out why it is not loading for you.

spiel@cfl.rr.com
 
I really appreciate your help. I blew away all of my directories and reconstructed them, so it loaded (I still can't figure out the problem there!) I then got it to work, but came up with another XML display problem. I'll email you the code, which is a java servlet that contains the xml and the xsl that goes with it. The funny thing is it loads until I add 'cost' to the result set in the servlet. I think you're right about IE being picky with xsl, but I can't see what it's whining about. It gives me this message:

The XML page cannot be displayed
Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later.


------------------------------------------------------------
End tag 'ProductList' does not match the start tag 'Product'. Line 9, Position 3


</ProductList>
--^


 
mpenny --

First, I did put your XSL and XML on my site to test and it worked fine. You can find the test code at -- --
Now, we know at this point that the XML data structure is right and the XSL layout/syntax is right. The only variable remaining is the servlet code -- and honestly that's a topic not really applicable to this thread.

However, I can (I think) give you a quick response to the servlet code problem -- I'll ask others to forgive me straying off topic

There's nothing wrong with the code where you are outputting the XML elements from the SQL search results. However, when you add the <Cost> element it is throwing you an exception for some reason. When you catch that exception in either IOexception or SQLexception you are printing an error to sys.err, but you are not closing the <Product> tag you had opened in the XML output stream.

Add a out.println (&quot;</Product>&quot;) to both of those exception handlers and that will solve the tag mismatch problem you get.

Of course, you're going to have to go back and find out why the rs.getString(4) is throwing an exception. I'm betting that field is empty within the SQL table and therefor throws an exception.

FWIW: You might have to modify your code to call a funciton for each element and do a try/catch in that function so you can close the tag. This will ensure all tags are closed if the rs.getString() throws an exception -- Something like this...

private void printElement(
ServletOutputStream out, ResultSet rs,
int index, String id )
{
out.print( &quot;<&quot; + id + &quot;>&quot; );

try
{
out.print( rs.getString( index ) );
}
catch ( Throwable e)
{
System.err.println (e.getMessage ()
+ e.toString() );
}

out.println( &quot;</&quot; + id + &quot;>&quot; );
}

Then, from within processResultSet...

processResultSet(ServletOutputStream out, ResultSet rs)
{
...
out.println( &quot;<Product&quot;> );
printElement( out, rs, 1, &quot;Title&quot; );
printElement( out, rs, 2, &quot;Author&quot; );
...
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top