Java XML Transform - Strange Issue -
i having strange issue cannot seem figure out. until now, transform methods seemed have worked flawlessly, tool building causing me major headaches.
here methods:
this 1 works without error , produces proper xml
public static void transform(string filename, string filepath, string stylesheetpath, string outputto, boolean prettyprint, boolean excludedeclaration) throws transformerexception, ioexception { if (!new file(outputto).exists()) new file(outputto).mkdir(); transformerfactory factory = transformerfactory.newinstance(); source xsl = new streamsource(new file(stylesheetpath)); templates template = factory.newtemplates(xsl); transformer transformer = template.newtransformer(); if (!prettyprint) { transformer.setoutputproperty(outputkeys.indent, "no"); } else { transformer.setoutputproperty(outputkeys.indent, "yes"); transformer.setoutputproperty("{http://xml.apache.org/xslt}indent-amount", "4"); } if (excludedeclaration) transformer.setoutputproperty(outputkeys.omit_xml_declaration, "yes"); source xml = new streamsource(new file(filepath + filename)); outputstream outputstream = new fileoutputstream(outputto + filename); transformer.transform(xml, new streamresult(outputstream)); outputstream.close(); }
using same xslt, following produces xml (or xml) contains text nodes (no elements, attributes, etc)
public static document transforminmemory(document xmldoc, string stylesheetpath) throws transformerexception, parserconfigurationexception, saxexception, ioexception { transformerfactory factory = transformerfactory.newinstance(); source xsl = new streamsource(new file(stylesheetpath)); templates template = factory.newtemplates(xsl); transformer transformer = template.newtransformer(); transformer.setoutputproperty(outputkeys.indent, "no"); transformer.setoutputproperty("{http://xml.apache.org/xslt}indent-amount", "0"); domsource source = new domsource(xmldoc); bytearrayoutputstream baos = new bytearrayoutputstream(); transformer.transform(source, new streamresult(baos)); system.out.println(baos.tostring()); // load documentbuilder documentbuilderfactory domfactory = documentbuilderfactory.newinstance(); documentbuilder builder = domfactory.newdocumentbuilder(); inputsource = new inputsource(new bytearrayinputstream(baos.tobytearray())); return builder.parse(is); }
from searching have been doing, doesn't appear doing incorrectly in second method, sure producing weird results.
sample result (unfortunately, cannot post actual data, replaced text other data)
<?xml version="1.0" encoding="utf-8"?> text here a. other text here b. more text here c. , more text here d. more text here 1
i purposely left result formatted see seeing. above result produced system.out.println(baos.tostring());
. if highlight text in console (eclipse), indentions there, elements, etc not showing up.
so, question: can tell me possibly going on? why first 1 work without problems, second cause result above?
edit:
after playing around method, figured out workaround seems work. instead of using domsource
, converted xmldoc inputstream
, seems bit hacky. thoughts why domsource
causing problem?
public static document transforminmemory(document xmldoc, string stylesheetpath) throws transformerexception, parserconfigurationexception, saxexception, ioexception { transformerfactory factory = transformerfactory.newinstance(); source xsl = new streamsource(new file(stylesheetpath)); templates template = factory.newtemplates(xsl); transformer transformer = template.newtransformer(); transformer.setoutputproperty(outputkeys.indent, "no"); transformer.setoutputproperty("{http://xml.apache.org/xslt}indent-amount", "0"); // convert xmldoc inputstream bytearrayoutputstream xmloutstream = new bytearrayoutputstream(); source domsource = new domsource(xmldoc); result result = new streamresult(xmloutstream); transformerfactory.newinstance().newtransformer().transform(domsource, result); inputstream in = new bytearrayinputstream(xmloutstream.tobytearray()); //domsource source = new domsource(xmldoc); bytearrayoutputstream baos = new bytearrayoutputstream(); source source = new streamsource(in); transformer.transform(source, new streamresult(baos)); system.out.println("baos -> " + baos.tostring()); // load documentbuilder documentbuilderfactory domfactory = documentbuilderfactory.newinstance(); documentbuilder builder = domfactory.newdocumentbuilder(); inputsource = new inputsource(new bytearrayinputstream(baos.tobytearray())); return builder.parse(is); }
documentbuilder default isn't namespace-aware. need setnamespaceaware(true). without namespace awareness, template rules in stylesheet won't match, default template rules kick in, , these output text nodes.
note creating dom merely provide input xslt bad idea; using xslt processor's native tree representation work better.
Comments
Post a Comment