aspose file tools*
The moose likes Beginning Java and the fly likes xml transform code just not behaving Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "xml transform code just not behaving" Watch "xml transform code just not behaving" New topic
Author

xml transform code just not behaving

joyita raksit
Greenhorn

Joined: Oct 12, 2006
Posts: 17
hi

i just cant seem to get this xml stuff working! i've got the imports, my classpath is correct, i'm using java 1.6, i've check the api's, i think i've decared all i need to etc but i'm still getting these errors that are starting to make me twitch. here's the errant snippet of code...

public static Document transformXML(Document xmlDoc, Document xslDoc) throws XMLHelperException {
try {

Source xmlIn = new StreamSource(xmlDoc);
Source xslIn = new StreamSource(xslDoc);

ByteArrayOutputStream baos = new ByteArrayOutputStream();
StreamResult xmlOut = new StreamResult(baos);

TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(xmlOut);
transformer.transform(xmlIn, xslIn, xmlOut);
baos.close();
String result = baos.toString();

return parseXMLFromString(result);

---------------

& the errors;

C:\>javac XMLHelper.java
XMLHelper.java:172: cannot find symbol
symbol : constructor StreamSource(org.w3c.dom.Document)
location: class javax.xml.transform.stream.StreamSource
Source xmlIn = new StreamSource(xmlDoc);
^
XMLHelper.java:173: cannot find symbol
symbol : constructor StreamSource(org.w3c.dom.Document)
location: class javax.xml.transform.stream.StreamSource
Source xslIn = new StreamSource(xslDoc);
^
XMLHelper.java:179: cannot find symbol
symbol : method newTransformer(javax.xml.transform.stream.StreamResult)
location: class javax.xml.transform.TransformerFactory
Transformer transformer = tFactory.newTransformer(xmlOut);
^
XMLHelper.java:180: transform(javax.xml.transform.Source,javax.xml.transform.Res
ult) in javax.xml.transform.Transformer cannot be applied to (javax.xml.transfor
m.Source,javax.xml.transform.Source,javax.xml.transform.stream.StreamResult)
transformer.transform(xmlIn, xslIn, xmlOut);
^
XMLHelper.java:389: cannot find symbol
symbol : method getDefaultTagTable()
location: class org.w3c.tidy.TagTable
org.w3c.tidy.TagTable tags = org.w3c.tidy.TagTab
le.getDefaultTagTable();


anyone any idea? it's going to be embarrassingly simple, i know it
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Well, you've just been rather creative with your code. For example, StreamSource reads from Readers and Streams, not from Document objects. You want to use a DOMSource to read from a Document. Transformer.newTransformer() can take a Source argument, but not a StreamResult. The code you're looking for is something like this

ByteArrayOutputStream baos = new ByteArrayOutputStream();
StreamResult xmlOut = new StreamResult(baos);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer t = factory.newTransformer(new DOMSource(xslDoc));
t.transform(new DOMSource(xmlDoc, xmlOut);


[Jess in Action][AskingGoodQuestions]
joyita raksit
Greenhorn

Joined: Oct 12, 2006
Posts: 17
dang i'm perplexed, i see where i went wrong, bit overexcited! but now i've amended the code and i'm still getting an error. i googled it and a guy seems to have found a workaround using jdom - here's the link

http://forum.java.sun.com/thread.jspa?threadID=662922&start=15&tstart=0

really though, i first want to figure out why this is not compiling

so, here's my code

DOMSource xmlIn = new DOMSource(xmlDoc);
DOMSource xslIn = new DOMSource(xslDoc);

ByteArrayOutputStream baos = new ByteArrayOutputStream();
StreamResult xmlOut = new StreamResult(baos);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer t = factory.newTransformer(new DOMSource(xslDoc));
t.transform(xmlDoc, xmlOut);

baos.close();
String result = baos.toString();

return parseXMLFromString(result);

------------------------

and the error:

XMLHelper.java:182: transform(javax.xml.transform.Source,javax.xml.transform.Res
ult) in javax.xml.transform.Transformer cannot be applied to (org.w3c.dom.Docume
nt,javax.xml.transform.stream.StreamResult)
t.transform(xmlDoc, xmlOut);
^
XMLHelper.java:392: cannot find symbol
symbol : method getDefaultTagTable()
location: class org.w3c.tidy.TagTable
org.w3c.tidy.TagTable tags = org.w3c.tidy.TagTab
le.getDefaultTagTable();

^


&

if i try
t.transform(new DOMSource(xmlDoc, xmlOut));

then i get

XMLHelper.java:182: cannot find symbol
symbol : constructor DOMSource(org.w3c.dom.Document,javax.xml.transform.stream.
StreamResult)
location: class javax.xml.transform.dom.DOMSource
t.transform(new DOMSource(xmlDoc, xmlOut));
^

any ideas? thanks a lot for previous help, i've gone from no clue to vague clue
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: xml transform code just not behaving
 
Similar Threads
What is the following XSLT program doing ?
problem in XSL Transformation
XSLT Transformation in jav
xml transform just not behaving!
DocumentBuilder.parse not working for String input