File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

xml transform code just not behaving

 
joyita raksit
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
 
joyita raksit
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic