Java XML Transform - Strange Issue

Tosmomiaccils

New Member
I am having this strange issue that I cannot seem to figure out. Up until now, my transform methods seemed to have worked almost flawlessly, but the tool I am currently building is causing me some major headaches.Here are my methods:This one works without error and produces the proper XML\[code\] 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(); }\[/code\]Using the same XSLT, the following produces XML (or something from XML) that contains only Text nodes (no elements, attributes, etc)\[code\] 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 into DocumentBuilder DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = domFactory.newDocumentBuilder(); InputSource is = new InputSource(new ByteArrayInputStream(baos.toByteArray())); return builder.parse(is); }\[/code\]From all the searching I have been doing, it doesn't appear that I am doing anything incorrectly in the second method, but it sure is producing some weird results.Sample result (unfortunately, I cannot post the actual data, so I just replaced the text with other data)\[code\] <?xml version="1.0" encoding="UTF-8"?> Some Text Here A. Some other text here B. Some more text here C. And more text here D. Even more text here A 1\[/code\]I purposely left result formatted so you could see exactly what I am seeing. The above result is what is produced by the \[code\]System.out.println(baos.toString());\[/code\]. If I highlight the text in my console (Eclipse), the indentions are all there, but all elements, etc are not showing up.So, my question: Can anyone tell me what could possibly be going on? Why does the first one work without any problems, but the second cause the result above?
 
Back
Top