• Post Reply Bookmark Topic Watch Topic
  • New Topic

"problem of TransformerFactory "  RSS feed

 
arul jothi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI java ranch, I am very upset , because i am not able to get the newInstance for this javax.xml.transform.TransformerFactory .
Can any one plese help me to resolve this problem.Description About my Project:My project deals with xmls. Simply saying i need to xml file in java code by using the TransformerFactory . I am using tomcat Server. From the java documentation of TransformerFactory i come to know the terms called lib/jaxp.properties , and it's required to have the implementation class .Can any one tell me steps i need to follow:I am just using the the following code:
Thanks in advance
 
Makarand Parab
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Arul
Try this way out
TransformerFactory tansfact = TransformerFactory.newInstance();
Transformer transformer = tansfact.newTransformer();
Checkout if ur getting transformer as null before u move ahead from this point

Are you trying to work with XML and XSL.

Let me know

Regards
Makarand Parab
 
arul jothi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI Makarand Parab ,
Thanks for ur reply. the TransformerFactory object is null. So i am not able to get the Transformer from this factory. About ur second question, i am just creating a Document object then i am trying to create a xml file. For this i need a Transformer instance.

Thanks once again. Waiting for ur reply.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have a library that's capable of creating transformers in your classpath? That would be either Xalan (from Jakarta) or Saxon (on SourceForge). Those two also come with instructions on how set them up, that is to say, what you need to put into the jaxp.properties file.
 
Makarand Parab
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arul
To get the Document object u can code it this way rather than going for TransformerFactory.

Code
********
DocumentBuilderFactory dbf = DocumentBuilderFactory .newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc= db.newDocument();

this way u will get document object. Morever u need to have the parser jar file [one ur using xalan or xerces2] in the classpath. Transformer factory is mostly used when you are working with XML and XSL. For simple creation of document object to work with XML using dom u can use the above code.

Let me know

Regards
Makarand Parab
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Transformer factory is mostly used when you are working with XML and XSL.


Which is what the poster is doing. That is clear from the original post.

For simple creation of document object to work with XML using dom u can use the above code.


Which is what the poster is not doing. That also is clear from the original post. Your point is?
 
Jignesh Patel
Ranch Hand
Posts: 626
Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Transformer transformer = TransformerFactory.newInstance
().newTransformer();

It seems you are missing of adding xslt file.
It is supposed to be like this.

Transformer transformer = TransformerFactory.newInstance().newTransformer(inputXSL);

Also check whether the xlan processor is in your path.It comes by default with j2sdk1.4.

Instead of tomcat try following standalon class and see if it works.

import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.Source;
import java.io.ByteArrayOutputStream;
public class Trans{
public static void main(String args[]){
try{


Source inputXML= new StreamSource(Thread.currentThread().getContextClassLoader().getResourceAsStream("source.xml"));

Source inputXSL = new StreamSource(Thread.currentThread().getContextClassLoader().getResourceAsStream("abc.xslt"));



Result result = new StreamResult(System.out);

TransformerFactory tFact = TransformerFactory.newInstance();
Transformer tf = tFact.newTransformer(inputXSL);

tf.setOutputProperty("indent", "yes");//

tf.transform(inputXML,result);



}catch(Exception e){
e.printStackTrace();
}


}
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!