List of usage examples for javax.xml.transform.stream StreamSource StreamSource
public StreamSource(File f)
From source file:Main.java
/** * <p>Use this to grab things like the Plays title, or the characters in the play.</p> * <p>You will have to specify the XSLT file to do this</p> * * @param xmlFile the name and path of the xml file * @param xslFile the name and path of the xslt file *@param delim the delimiter you wish to split with. * @return A string delimited of the transformed document * @see MimeConstants//from w w w. j av a 2s . c o m */ public static String simpleTransformToStr(String xmlFile, String xslFile) { // -------------------------------------------------------------------- // http://www.oreillynet.com/pub/a/oreilly/java/news/javaxslt_0801.html // -------------------------------------------------------------------- String returnValue = ""; try { // JAXP reads data using the Source interface Source xmlSource = new StreamSource(xmlFile); Source xsltSource = new StreamSource(xslFile); // the factory pattern supports different XSLT processors TransformerFactory transFact = TransformerFactory.newInstance(); Transformer trans; trans = transFact.newTransformer(xsltSource); // ---------------------------------------------------------- // http://forum.java.sun.com/thread.jspa?threadID=636335&messageID=3709470 // Paul, you had alot of trouble figuring out how to pass the output of the transformation, the url above is hwere you got the following chunk // ---------------------------------------------------------- StringWriter output = new StringWriter(); trans.transform(xmlSource, new StreamResult(output)); return output.toString(); } catch (TransformerException ex) { ex.printStackTrace(); } return null; }
From source file:Main.java
public static void OutputXml(File in, String saveFileInPath) throws Exception { TransformerFactory tFactory = TransformerFactory.newInstance(); Source xslDoc = new StreamSource("backup.xslt"); Source xmlDoc = new StreamSource(in.getPath()); System.out.print(in.getName() + "/n"); String outputFileName = in.getName().split("\\.")[0]; System.out.println(outputFileName); OutputStream htmlFile;/*from ww w . j a va 2s .co m*/ htmlFile = new FileOutputStream(saveFileInPath + "//" + outputFileName + ".html"); Transformer transformer = tFactory.newTransformer(xslDoc); transformer.transform(xmlDoc, new StreamResult(htmlFile)); }
From source file:Main.java
public static boolean validateXML(String schemaPath, String xmlPath) { try {// w ww .ja v a 2 s .c o m String schemaLang = "http://www.w3.org/2001/XMLSchema"; SchemaFactory factory = SchemaFactory.newInstance(schemaLang); // create schema by reading it from an XSD file: Schema schema = factory.newSchema(new StreamSource(schemaPath)); Validator validator = schema.newValidator(); // at last perform validation: validator.validate(new StreamSource(xmlPath)); } catch (Exception ex) { return false; } return true; }
From source file:se.inera.intyg.intygstjanst.web.integration.util.SendMessageToCareUtil.java
public static SendMessageToCareType getSendMessageToCareTypeFromFile(String fileName) throws Exception { JAXBContext jaxbContext = JAXBContext.newInstance(SendMessageToCareType.class); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); return unmarshaller.unmarshal(new StreamSource(new ClassPathResource(fileName).getInputStream()), SendMessageToCareType.class).getValue(); }
From source file:Main.java
/** * Applies a stylesheet (that receives parameters) to a given xml document. * The resulting XML document is converted to a string after transformation. * //from www. j av a 2s. com * @param xmlDocument * the xml document to be transformed * @param parameters * the hashtable with the parameters to be passed to the * stylesheet * @param xsltFilename * the filename of the stylesheet * @return the transformed xml document as a string * @throws Exception */ public static String transformDocumentAsString(Document xmlDocument, Map<String, String> parameters, String xsltFilename) throws Exception { // Generate a Transformer. Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(xsltFilename)); // set transformation parameters if (parameters != null) { for (Map.Entry<String, String> param : parameters.entrySet()) { transformer.setParameter(param.getKey(), param.getValue()); } } StringWriter stringWriter = new StringWriter(); StreamResult streamResult = new StreamResult(stringWriter); // Perform the transformation. transformer.transform(new DOMSource(xmlDocument), streamResult); // Now you can get the output Node from the DOMResult. return stringWriter.toString(); }
From source file:Main.java
/** * @param xmlsource/* w w w. ja va2s . c o m*/ * @param xsltfile * @return * @throws javax.xml.transform.TransformerException */ public static String transform(String xmlsource, File xsltfile) throws TransformerException { Source xmlSource = new StreamSource(new StringReader(xmlsource)); Source xsltSource = new StreamSource(xsltfile); StringWriter stringWriter = new StringWriter(); TransformerFactory transFact = TransformerFactory.newInstance(); Templates cachedXSLT = transFact.newTemplates(xsltSource); Transformer trans = cachedXSLT.newTransformer(); trans.transform(xmlSource, new StreamResult(stringWriter)); return stringWriter.toString(); }
From source file:Main.java
/** * Perform a schema validation/*from w w w .jav a 2 s . c o m*/ */ public static void validate(InputStream schema, InputStream document) throws Exception { validate(new StreamSource(schema), new StreamSource(document)); }
From source file:Main.java
/** * Format the provided XML input.// w w w .j a va 2 s .co m * * @param input * XML input to format. * @param indent * Indentation to use on formatted XML. * @return Formatted XML. * @throws TransformerException * if some problem occur while processing the xml * @see #prettyFormat(String) */ public static String prettyFormat(String input, Integer indent) throws TransformerException { if (input != null) { Source xmlInput = new StreamSource(new StringReader(input)); StringWriter stringWriter = new StringWriter(); StreamResult xmlOutput = new StreamResult(stringWriter); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indent == null ? 2 : indent)); transformer.transform(xmlInput, xmlOutput); return xmlOutput.getWriter().toString(); } return input; }
From source file:Main.java
/** * Applies an XSL transformation to <code>xmlString</code> using * <code>xsltStr</code> as the stylesheet and returns the transformed * string./*from w ww . j a v a 2 s . c o m*/ * * @param xmlString * The XML to transform. * @param xsltStr * The stylesheet as an XML string (not a file). * @return Transformed string. * @throws Exception */ public static String applyStylesheetAsString(String xmlString, String xsltStr) throws Exception { // Use the static TransformerFactory.newInstance() method to instantiate // a TransformerFactory. The javax.xml.transform.TransformerFactory // system property setting determines the actual class to instantiate -- // org.apache.xalan.transformer.TransformerImpl. TransformerFactory tFactory = TransformerFactory.newInstance(); // Use the TransformerFactory to instantiate a Transformer that will work with // the stylesheet you specify. This method call also processes the // stylesheet into a compiled Templates object. Transformer transformer = tFactory .newTransformer(new StreamSource(new ByteArrayInputStream(xsltStr.getBytes()))); // Use the Transformer to apply the associated Templates object to an // XML document (foo.xml) and write the output to a file (foo.out). StringWriter swriter = new StringWriter(); StreamResult sresult = new StreamResult(swriter); transformer.transform(new StreamSource(new ByteArrayInputStream(xmlString.getBytes("UTF-8"))), sresult); return swriter.toString(); }
From source file:Main.java
public static boolean transform(InputStream xsltInputStream, InputStream xmlInputStream, OutputStream outputStream, Map<String, Object> parameterMap) { boolean result = false; try {//from w w w. j a v a2 s. co m Source source = new StreamSource(xsltInputStream); Transformer transformer = TransformerFactory.newInstance().newTemplates(source).newTransformer(); for (Entry<String, Object> entry : parameterMap.entrySet()) { transformer.setParameter(entry.getKey(), entry.getValue()); } transformer.transform(new StreamSource(xmlInputStream), new StreamResult(outputStream)); result = true; } catch (TransformerException | TransformerFactoryConfigurationError e) { } return result; }