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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML, XSLT in Jsp 1

Status
Not open for further replies.

sparc20

Programmer
Mar 13, 2002
56
GB
Hello everyone,

i want to open a stream of xml, maybe a file and also
use an .xsl file for transforming it.

i Have so far :

response.setContentType("text/xml");
PrintWriter out = response.getWriter();

File xmlFile = new File("item.xml");
File xsltFile = new File("item.xsl");

Source xmlSource = new StreamSource(xmlFile);
Source xsltSource = new StreamSource(xsltFile);
TransformerFactory transFact =
TransformerFactory.newInstance();
Transformer trans = transFact.newTransformer(xsltSource);

trans.transform(xmlSource, new StreamResult(out));


out.close();
}



but i will not show anything in the page,
IEplorer returns this error when is called

"XML document must have a top level element. Error processing resource '
By themselfs, the xml is displayed just fine using xsl

Also, is there a default location where .xml,.xsl files sohuld be ? The servlet doing the processing is in a package folder "newportal" within "WEB-INF classes".

thanx
 
What happens when you transform to an file - does the file look valid xml ?

--------------------------------------------------
Free Database Connection Pooling Software
 
item.xml is displayed correctly using item.xsl using a web browser, yes.

If you agree with the the code above it might be a problem with the files location. As this is my first attempt i may misplace them in the directory structure. Would u know where should they be ?

They look like this:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<?xml-stylesheet type="text/xsl" href="item.xsl"?>

<article>
<headline>headline</headline>
<byLine>byline</byLine>
<body>news body</body>
<reporter>John</reporter>
<author>John</author>
<dateCreated>20-10-2004</dateCreated>
<datePublished>20-10-2004</datePublished>
<imageURI></article>


---------------- and ---------------

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="<xsl:template match="/">
<html>
<body>
<h2>News Item</h2>
<table border="1">
<tr><td>Headtitle:</td><td><xsl:value-of select="article/headline"/></td></tr>
<tr><td>Byline:</td><td><xsl:value-of select="article/byLine"/></td></tr>
<tr><td>Body:</td><td><xsl:value-of select="article/body"/></td></tr>
<tr><td>Reporter:</td><td><xsl:value-of select="article/reporter"/></td></tr>
<tr><td>Author:</td><td><xsl:value-of select="article/author"/></td></tr>
<tr><td>Created on:</td><td><xsl:value-of select="article/dateCreated"/></td></tr>
<tr><td>Published on:</td><td><xsl:value-of select="article/datePublished"/></td></tr>
<tr><td></td><td><img src="{article/imageURI}"/></td></tr>
</table>
</body>
</html>
</xsl:template></xsl:stylesheet>
 
Put your xsl & xml files in the root of you webapp (ie in the newsportal directory), and run this code. Also stream the transform to some file on your system so you know what the xml looks like are translation.

Code:
        response.setContentType("text/xml");
        PrintWriter out = response.getWriter();
     
     	String webappRoot = getServletContext().getRealPath("/");
     
        File xmlFile = new File(webappRoot +"/item.xml");
        File xsltFile = new File(webappRoot +"/"item.xsl");
        
        if (!xmlFile.exists() || !xsltFile.exists()) {
        	throw new IOException("Cannot find input resources !!! " +xmlFile.exists() +" " +xsltFile.exists());
        }

        Source xmlSource = new StreamSource(xmlFile);
        Source xsltSource = new StreamSource(xsltFile);
        TransformerFactory transFact = TransformerFactory.newInstance();
        Transformer trans = transFact.newTransformer(xsltSource);

	trans.transform(xmlSource, new StreamResult(new FileOutputStream("some file on your system, like C:/ or /tmp")));

        trans.transform(xmlSource, new StreamResult(out));
 
        
        out.close();

--------------------------------------------------
Free Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top