cancel
Showing results for 
Search instead for 
Did you mean: 

Transformer in JAVA DOM

Former Member
0 Kudos

Hi,

I am doing hands on using Java Dom.

I have to pick up the xml file from source directory (local pc )and convert into target xml file and store in local pc using java dom

(not using in XI)

In NWDS, I picked up the file and parsed properly finally built the target DOm(domTarget)

Finally i need to transform it and put into folder as xml file.

I ended up as follows, then really dont know what to do.

TransformerFactory tFactory = TransformerFactory.newInstance();

Transformer transformer = tFactory.newTransformer();

Source req_Input = new DOMSource(docTarget);

Result req_Output = new StreamResult();

transformer.transform(req_Input,req_Output);

can any one provide the few lines of final code

thanks in advance

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Neha

For this you have to use file input/output stream for this

the snippet of the code is given below. This might help you.

TransformerFactory transformerfactory =

TransformerFactory.newInstance();

Transformer transformer = transformerfactory.newTransformer

(new StreamSource(new File

(application.getRealPath("/") + "ch09_02.xsl")));

transformer.transform(new StreamSource(new File(application.getRealPath

("/") + "ch09_01.xml")),

new StreamResult(new File(application.getRealPath("/") +

"result.html")));

}

catch(Exception e) {}

FileReader filereader = new FileReader(application.getRealPath("/") +

"result.html");

BufferedReader bufferedreader = new BufferedReader(filereader);

String textString;

while((textString = bufferedreader.readLine()) != null) {

}

filereader.close();

}

Thanks

Abhishek Mahajan

*Please reward points in case helpful*

Former Member
0 Kudos

Hi Amit,

From your code, i could get

source : ch09_02.xsl

target :result.html

what about the The target DOM,

i my case

Document docTarget = dbBuilder.newDocument();

finally i have added all the elements to the docTarget

where am i using it ?

I think i should use it, but where?

Regards

former_member185845
Active Participant
0 Kudos

Dear Neeha,

prob try with this,

File fout = new File("target.xml");

StreamResult ss = new StreamResult(fout);

Source source = new DOMSource(docTarget);

TransformerFactory tFactory = TransformerFactory.newInstance();

Transformer transformer = tFactory.newTransformer();

transformer.transform(source,ss);

FileReader filereader = new FileReader(fout);

BufferedReader bufferedreader = new BufferedReader(filereader);

String textString;

while((textString = bufferedreader.readLine()) != null)

{}

filereader.close();

Hope fully u should see the target doc.

Regards

chandra.dasari@yash.com

Answers (0)