cancel
Showing results for 
Search instead for 
Did you mean: 

Error in transforming XML using Java

Former Member
0 Kudos
390

Hi,

I am trying to transform a simple xml file using java.

Following is the code i am using

TransformerFactory factory = TransformerFactory.newInstance();

Transformer transformer = null;

Templates template = null;

StreamSource xmlSource = null;

StreamSource xslSource = null;

StreamResult result = null;

String msg = new String();

boolean support = false;

try {

xmlSource = new StreamSource("c:/abc.xml");

xslSource = new StreamSource("c:/abc.xsl");

File f = new File("c:/log/copy.xml");

result = new StreamResult(f);

template = factory.newTemplates(xmlSource);

transformer = template.newTransformer();

transformer.transform(xmlSource, result);

} catch (Exception ex) {

msg = ex.getMessage();

}

But it returns "Exceptionclass javax.xml.transform.TransformerConfigurationExceptionoccurred

Could not load stylesheet. javax.xml.transform.TransformerException: Unsupported Result object."

XML file i am using is as follows

<?xml version="1.0" encoding="ISO-8859-1"?>

<?xml-stylesheet type="text/xsl" href="c:\abc.xsl"?>

<catalog>

<cd>

<title>Empire Burlesque</title>

<artist>Bob Dylan</artist>

<country>USA</country>

<company>Columbia</company>

<price>10.90</price>

<year>1985</year>

</cd>

</catalog>

XSLT is

<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:transform version="1.0"

xmlns: xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">

<html>

<body>

<h2>My CD Collection</h2>

<table border="1">

<tr bgcolor="#9acd32">

<th align="left">Title</th>

<th align="left">Artist</th>

</tr>

<xsl:for-each select="catalog/cd">

<tr>

<td><xsl:value-of select="title"/></td>

<td><xsl:value-of select="artist"/></td>

</tr>

</xsl:for-each>

</table>

</body>

</html>

</xsl:template>

</xsl:transform>

Can anyone tell me what could be the reason of this error.

Thanks,

Komal.

Accepted Solutions (0)

Answers (1)

Answers (1)

Benny
Product and Topic Expert
Product and Topic Expert
0 Kudos

Could you tell me which product you are using and what system this happens in?

Regards,

Benny

Former Member
0 Kudos

Hi Benny,

Thanks for the quick response.I am working on SAP Netweaver studio Sneak preview 6.3. I am using JDK 1.3.

Komal.

Benny
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi Komal,

the version and especially xml stuff was in pretty early stage for sneak preview I last year. Maybe downloading the new version and trying witht his one will help.

Regards,

Benny

zubev
Advisor
Advisor
0 Kudos

Hi Komal,

You have:

template = factory.newTemplates(xmlSource);

but it should be

template = factory.newTemplates(xslSource);

I think the exception you got is not thrown by the sapxmltoolkit. Could you please print the whole stack trace of the exception that occurs?

Best regards,

Alexander

Former Member
0 Kudos

Hi Alexander,

I am using xslSource but it is not working. When i am calling newTemplate(xslSource) or newTransformer(xslSource) it comes in catch block where when i pring exception class it returns TransformationConfigurationException and when i print message using ex.getMessage() it returns the message i have posted above. when i comment this statement no exception is generated and control does not go to catch block. i don't know whether there is any error in my java code or in XSLT program.

thanks,

Komal.

zubev
Advisor
Advisor
0 Kudos

Hi Komal,

I need the full stack trace of the exception in order to help you more. Could you please post it?

Best regards,

Alexander

Former Member
0 Kudos

Hi Alexander,

Following is the stack trace i am receiving. It is saying "Unsupported Result object".

I tried "factory.getFeature(StreamResult.FEATURE);" which returns "true".

javax.xml.transform.TransformerConfigurationException: Could not load stylesheet. javax.xml.transform.TransformerException: Unsupported Result object. at com.sap.engine.lib.jaxp.TemplatesImpl.(TemplatesImpl.java:129) at com.sap.engine.lib.jaxp.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:42) at NewServlet.doPost(NewServlet.java:59) at NewServlet.doGet(NewServlet.java:31) at javax.servlet.http.HttpServlet.service(HttpServlet.java:740) at javax.servlet.http.HttpServlet.service(HttpServlet.java:853) at com.sap.engine.services.servlets_jsp.server.servlet.InvokerServlet.service(InvokerServlet.java:129) at javax.servlet.http.HttpServlet.service(HttpServlet.java:853) at com.sap.engine.services.servlets_jsp.server.RunServlet.runSerlvet(RunServlet.java:171) at com.sap.engine.services.servlets_jsp.server.ServletsAndJspImpl.startServlet(ServletsAndJspImpl.java:387) at com.sap.engine.services.httpserver.server.RequestAnalizer.startServlet(RequestAnalizer.java:375) at com.sap.engine.services.httpserver.server.RequestAnalizer.startServlet(RequestAnalizer.java:355) at com.sap.engine.services.httpserver.server.RequestAnalizer.invokeWebContainer(RequestAnalizer.java:721) at com.sap.engine.services.httpserver.server.RequestAnalizer.checkFilename(RequestAnalizer.java:418) at com.sap.engine.services.httpserver.server.RequestAnalizer.handle(RequestAnalizer.java:223) at com.sap.engine.services.httpserver.server.Client.handle(Client.java:79) at com.sap.engine.services.httpserver.server.Processor.request(Processor.java:152) at com.sap.engine.core.service630.context.cluster.session.ApplicationSessionMessageListener.process(ApplicationSessionMessageListener.java:37) at com.sap.engine.core.cluster.impl6.session.UnorderedChannel$MessageRunner.run(UnorderedChannel.java:60) at com.sap.engine.core.thread.impl3.ActionObject.run(ActionObject.java:55) at java.security.AccessController.doPrivileged(Native Method) at com.sap.engine.core.thread.impl3.SingleThread.run(SingleThread.java:115) -


javax.xml.transform.TransformerException: Unsupported Result object. at com.sap.engine.lib.jaxp.TemplatesImpl.(TemplatesImpl.java:122) at com.sap.engine.lib.jaxp.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:42) at NewServlet.doPost(NewServlet.java:59) at NewServlet.doGet(NewServlet.java:31) at javax.servlet.http.HttpServlet.service(HttpServlet.java:740) at javax.servlet.http.HttpServlet.service(HttpServlet.java:853) at com.sap.engine.services.servlets_jsp.server.servlet.InvokerServlet.service(InvokerServlet.java:129) at javax.servlet.http.HttpServlet.service(HttpServlet.java:853) at com.sap.engine.services.servlets_jsp.server.RunServlet.runSerlvet(RunServlet.java:171) at com.sap.engine.services.servlets_jsp.server.ServletsAndJspImpl.startServlet(ServletsAndJspImpl.java:387) at com.sap.engine.services.httpserver.server.RequestAnalizer.startServlet(RequestAnalizer.java:375) at com.sap.engine.services.httpserver.server.RequestAnalizer.startServlet(RequestAnalizer.java:355) at com.sap.engine.services.httpserver.server.RequestAnalizer.invokeWebContainer(RequestAnalizer.java:721) at com.sap.engine.services.httpserver.server.RequestAnalizer.checkFilename(RequestAnalizer.java:418) at com.sap.engine.services.httpserver.server.RequestAnalizer.handle(RequestAnalizer.java:223) at com.sap.engine.services.httpserver.server.Client.handle(Client.java:79) at com.sap.engine.services.httpserver.server.Processor.request(Processor.java:152) at com.sap.engine.core.service630.context.cluster.session.ApplicationSessionMessageListener.process(ApplicationSessionMessageListener.java:37) at com.sap.engine.core.cluster.impl6.session.UnorderedChannel$MessageRunner.run(UnorderedChannel.java:60) at com.sap.engine.core.thread.impl3.ActionObject.run(ActionObject.java:55) at java.security.AccessController.doPrivileged(Native Method) at com.sap.engine.core.thread.impl3.SingleThread.run(SingleThread.java:115)

Thanks,

Komal.

Former Member
0 Kudos

Hi Benny,

i downloaded Sneak preview II but my code still returns same exception.

Thanks,

Komal.

Former Member
0 Kudos

What if you remove the line

<?xml-stylesheet type="text/xsl" href="c:\abc.xsl"?>

This is implicitly done by your code anyway

.

Former Member
0 Kudos

Hi,

i removed

<?xml-stylesheet type="text/xsl" href="c:\abc.xsl"?>

from my xml file but it is still not working.

thanks,

komal.

zubev
Advisor
Advisor
0 Kudos

Hi Komal,

There is a check if the passed source object to the tempalates is an instance of StreamSource, DOMSource or SAXSource and if not then this exception is thrown.

It is really strange that this check fails due to from the source code you have provided, I see you pass a StreamSource. Could you please check if the StreamSource is an instance of javax.xml.transform.stream.StreamSource?

The other problem could be a classloader problem.

Have you registered any references from your servlet application to any J2EE libraries? Are there any jars in your EAR/WAR file? What JDK are you using and on what OS? Is it Sun's JDK?

Best regards,

Alexander

Former Member
0 Kudos

Hi Alexander,

StreamAOurce is an instance of javax.xml.transform.stream.StreamSource. There are no jar files. i am using JDK 1.3 on windows 2000 server. i am importing following libraries in my application.

import javax.xml.transform.*;

import javax.xml.transform.stream.StreamSource;

import javax.xml.transform.stream.StreamResult;

import javax.xml.transform.dom.*;

import java.io.*;

import org.w3c.dom.*;

Thanks,

Komal.

zubev
Advisor
Advisor
0 Kudos

Hi Komal,

It seems like a classloader problem. Make sure you do not have another xml parser or a jar containing javax.xml.* interfaces inside your EAR file (or with a reference).

If this is not your case, please create a CSS message

Best regards,

Alexander