XMLDocumentBuilderFactory factory = XMLDocumentFactory.getInstance(); Document document = factory.parse(root);
XMLRoot
with the content of a File:URL xmlURL = XMLDocumentFactoryBaseTest.class.getResource("xmlPrint2.xml"); XMLNodeUtilities2 utils = new XMLNodeUtilities2(); XMLRoot root = utils.getRootNode(xmlURL);Now we can convert to a
Document
:XMLDocumentBuilderFactory factory = XMLDocumentBuilderFactory.getInstance(); Document document = factory.parse(root);The result can be used for an xsl transform[1]
TransformerFactory factory = TransformerFactory.newInstance(); URL xslURL = XMLDocumentFactoryBaseTest.class.getResource("empty.xsl"); File xslFile = new File(xslURL.toURI().getPath()); StreamSource streamSource = new StreamSource(xslFile); Transformer transformer = factory.newTransformer(streamSource); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); DOMSource xslDomSource = new DOMSource(document); outputFile = File.createTempFile("mdiutils", ".xml"); try ( FileOutputStream outputStream = new FileOutputStream(outputFile)) { transformer.transform(xslDomSource, new StreamResult(outputStream)); }
Copyright 2006-2024 Herve Girod. All Rights Reserved. Documentation and source under the LGPL v2 and Apache 2.0 licences