javax.xml.transform.TransformerException durin XSL Transformation in Java

Hi,

Below is my piece of code where i access a web service that returns a xml as a string. I apply a xsl tranformation on it and try to store the result as a string. I get this error message

javax.xml.transform.TransformerException: Result object passed to''{0}'' is invalid.

at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.getOutputHandler(Unknown Source)

at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown Source)

at NewService.main(NewService.java:52)

My Code:

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InputStream;

import java.io.StringReader;

import java.net.MalformedURLException;

import java.net.URL;

import java.rmi.RemoteException;

import javax.xml.namespace.QName;

import javax.xml.rpc.ServiceException;

import javax.xml.transform.Result;

import javax.xml.transform.Source;

import javax.xml.transform.Transformer;

import javax.xml.transform.TransformerConfigurationException;

import javax.xml.transform.TransformerException;

import javax.xml.transform.TransformerFactory;

import javax.xml.transform.stream.StreamResult;

import javax.xml.transform.stream.StreamSource;

import org.apache.axis.client.Call;

import org.apache.axis.client.Service;

publicclass NewService{

/**

* @param args

*/

publicstaticvoid main(String[] args){

// TODO Auto-generated method stub

String endPoint ="http://localhost:8080/SampleDynamicWebProj/services/SampleClient";

Service service =new Service();

Call callOne;

try{

callOne = (Call) service.createCall();

callOne.setTargetEndpointAddress(new URL(endPoint));

callOne.setOperationName(new QName("http://DefaultNamespace",

"getXMLString"));

String concated = (String) callOne.invoke(new Object[]{"s"});

InputStream xsltFile =new FileInputStream("xslpackage/empTran.xsl");

Source xmlSource =new StreamSource(new StringReader(concated));

Source xsltSource =new StreamSource(xsltFile);

TransformerFactory transFact =

TransformerFactory.newInstance();

Transformer trans = transFact.newTransformer(xsltSource);

Result result =new StreamResult();

trans.transform(xmlSource, result);

System.out.println(result.toString());

}catch (ServiceException e){

e.printStackTrace();

}catch (MalformedURLException e){

e.printStackTrace();

}catch (RemoteException e){

e.printStackTrace();

}catch (FileNotFoundException e){

e.printStackTrace();

}catch (TransformerConfigurationException e){

e.printStackTrace();

}catch (TransformerException e){

e.printStackTrace();

}

}

}

I get the transformed XML into a Result object, but when i do a toString() oon it, i get the above exception.

any help wil be appreciated,

Dilip

[5461 byte] By [dilip_jsfa] at [2007-11-27 5:40:58]
# 1

> I get the transformed XML into a Result object, but

> when i do a toString() oon it, i get the above

> exception.

Well, no. Look at the stack trace you posted. It clearly says the error occurs in the transform() method. So the transformation fails.

And it fails because the StreamResult you provided doesn't contain any information about where to send the output. Use one of the other constructors.

DrClapa at 2007-7-12 15:17:58 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

Thanks,

But the problem was with the result giving an error. Its fixed now.

I have another question, i need to pass an XML data inside a xml tag for e.g.

<Employees>

<employee>

<name>Nick</name>

<addredd>[!CDATA[<streetname>2nd Street</streetname>]]</address>

</employee>

</Employees>

will this type of coding work?

thanks,

Dilip

dilip_jsfa at 2007-7-12 15:17:58 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3
Depends what you mean by "work". Your start and end tags don't match, but that's probably just a typing error. And it's not normal to put XML in an address, but maybe you just chose a bad example. But that is a correct way to put XML into a document as text rather than as markup.
DrClapa at 2007-7-12 15:17:58 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4

Oh well, yes it was a typo in address tag...ok agreed that its a bad example, check this out then,

i have a XML data that i convert to a html format using xsl transformation, now this converted html has to be shown in a html page(i use the out.write option).

so my initial xml looks like this ::

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

<Results>

<ColumnCount>6</ColumnCount>

<Columns>

<column>UID</column>

<column>UserName</column>

<column>Password</column>

<column>LastName</column>

<column>FirstName</column>

<column>EmailAddress</column>

</Columns>

<Rows>

<Row>

<value>1</value>

<value>userone</value>

<value>password-1</value>

<value>Anant</value>

<value>Dilip</value>

<value>dilip.a@email.com</value>

</Row>

<Row>

<value>2</value>

<value>usertwo</value>

<value>password-2</value>

<value>Palli</value>

<value>Gilli</value>

<value>Gilli.palli@email.com</value>

</Row>

</Rows>

I apply XSL transformation on this to get a HTML which i will be writing into my output screen hoping that the user will see it in a tabular format!

<[!CDATA["

<?xml version="1.0" encoding="UTF-8"?>

<table border="1">

<tr bgcolor="#9acd32">

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

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

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

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

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

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

</tr>

<tr>

<td>1</td>

<td>userone</td>

<td>password-1</td>

<td>Anant</td>

<td>Dilip</td>

<td>dilip.a@email.com</td>

</tr>

<tr>

<td>2</td>

<td>usertwo</td>

<td>password-2</td>

<td>Palli</td>

<td>Gilli</td>

<td>Gilli.palli@email.com</td>

</tr>

</table>

"]]>

The entire data is passed to a XML parser . I want the transformed xml data (which will be inside a <status></status> tag to be untouched by this parser. As you see i have put the transformed xml in a CDATA tag, but this aint helping me...

need urgent help,

Dilip

dilip_jsfa at 2007-7-12 15:17:58 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5
Do you want that data to be treated as text or as markup? What you did there says "I want this data to be treated as text" because that's what CDATA says. But since you're producing HTML, it's quite possible you want it to be treated as markup.
DrClapa at 2007-7-12 15:17:58 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 6
Sorry there, yeah i want it as markup.
dilip_jsfa at 2007-7-12 15:17:58 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 7
Then don't put it in a CDATA section.
DrClapa at 2007-7-12 15:17:58 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 8

The entire html will come under a <status></status> tag. If i dont put it inside the CDATA section, the parser recognises it as markup and i lose the html structure and cannot display the content.

What i did do was use a URLEncoder.encode to encode the html and put it inside the <status/> tag and while receiving i did a URLDecode.decode. Works fine...

Is there a better way?

Also, has anyone worked on IBM's Workplace Dashboard? A product of Bowstreet.

thanks,

Dilip

dilip_jsfa at 2007-7-12 15:17:58 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 9

> The entire html will come under a <status></status>

> tag. If i dont put it inside the CDATA section, the

> parser recognises it as markup and i lose the html

> structure and cannot display the content.

Of course it's markup, it's HTML. And you don't "lose" the HTML structure. I don't know why you can't display the HTML -- did you try the xsl:copy or xsl:copy-of elements?

DrClapa at 2007-7-12 15:17:58 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 10

No..havent tried that...

Let me explain the entire scenario, i pass an xml string on a web service call. This string obtained and parsed using a method named DataConverter.toIXML(String params);

Now this is specific to a tool of IBM called the Workplace Dashboard. This code will remove the markup and provide me with the values within the tags. So if i were to send a CDATA section within a tag i get the CDATA section as it is and if i write it onto the user screen using a out.write it doesnt get displayed as a markup but as junk with all that CDATA tag visible.

dilip_jsfa at 2007-7-12 15:17:58 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 11
Then don't put it in a CDATA section.
DrClapa at 2007-7-12 15:17:58 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...