List of usage examples for javax.xml.transform.stream StreamSource StreamSource
public StreamSource(File f)
From source file:Main.java
/** * Creates a Source from an InputStream. * @param is InputStream of the Source.// w w w . j a va2s . c o m * @return the source. */ public static Source createSource(InputStream is) { return new StreamSource(is); }
From source file:Main.java
public static String transform(String xmlString, String stylesheetPathname) throws Exception { try {/*from w w w. j a v a 2 s .com*/ TransformerFactory factory = TransformerFactory.newInstance(); Source stylesheetSource = new StreamSource(new File(stylesheetPathname).getAbsoluteFile()); Transformer transformer = factory.newTransformer(stylesheetSource); Source inputSource = new StreamSource(new StringReader(xmlString)); Writer outputWriter = new StringWriter(); Result outputResult = new StreamResult(outputWriter); transformer.transform(inputSource, outputResult); return outputWriter.toString(); } catch (TransformerConfigurationException tce) { throw new Exception(tce.getMessageAndLocation()); } catch (TransformerException te) { throw new Exception(te.getMessageAndLocation()); } }
From source file:Main.java
public static String prettyFormat(String input, int indent, boolean isOmitXmlDeclaration) { try {/* ww w . j av a 2 s .c o m*/ Source xmlInput = new StreamSource(new StringReader(input)); StringWriter stringWriter = new StringWriter(); StreamResult xmlOutput = new StreamResult(stringWriter); TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", indent); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); if (isOmitXmlDeclaration) { transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); } transformer.transform(xmlInput, xmlOutput); return xmlOutput.getWriter().toString(); } catch (Exception e) { throw new RuntimeException(e); // simple exception handling, please review it } }
From source file:Main.java
public static String getXmlFileToString(String filePath) throws TransformerException { File xmlFile = new File(filePath); StringWriter writer = new StringWriter(); TransformerFactory fac = TransformerFactory.newInstance(); Transformer x = fac.newTransformer(); x.transform(new StreamSource(xmlFile), new StreamResult(writer)); return writer.toString(); }
From source file:Main.java
public static String transform(String xmlFilePath, String xsltFilePath) throws Exception { StreamResult result = new StreamResult(new StringWriter()); StreamSource s = new StreamSource(new File(xsltFilePath)); StreamSource xml = new StreamSource(new File(xmlFilePath)); Transformer transformer = TransformerFactory.newInstance().newTransformer(s); transformer.transform(xml, result);//from w ww .j a v a 2 s .c o m String response = result.getWriter().toString(); return response; }
From source file:Main.java
public static String format(String input, int indent) { try {/*from w ww. j ava2s . c o m*/ Source xmlInput = new StreamSource(new StringReader(input)); StringWriter stringWriter = new StringWriter(); StreamResult xmlOutput = new StreamResult(stringWriter); TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", indent); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(xmlInput, xmlOutput); return xmlOutput.getWriter().toString(); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
public static Object parseXmlFileToBeanByJAXB(String path, Class clase) throws Exception { JAXBContext context = JAXBContext.newInstance(clase); Unmarshaller unmarshaller = context.createUnmarshaller(); JAXBElement elm = unmarshaller.unmarshal(new StreamSource(new File(path)), clase); return elm.getValue(); }
From source file:Main.java
public static <T> T unMarshal(String content, Class<T> clazz) throws JAXBException { JAXBContext jaxbContext = JAXBContext.newInstance(clazz); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); return unmarshaller.unmarshal(new StreamSource(new StringReader(content)), clazz).getValue(); }
From source file:Main.java
static boolean validateAgainstXSD(InputStream xml, InputStream xsd) { try {/*from w w w . java 2 s. c om*/ SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = factory.newSchema(new StreamSource(xsd)); Validator validator = schema.newValidator(); validator.validate(new StreamSource(xml)); return true; } catch (Exception ex) { return false; } }
From source file:Main.java
public static Schema getSchema(InputStream... inputs) throws SAXException { StreamSource[] streamSource = new StreamSource[inputs.length]; for (int i = 0; i < inputs.length; i++) { streamSource[i] = new StreamSource(inputs[i]); }/*from w ww.ja v a 2 s .co m*/ SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = sf.newSchema(streamSource); return schema; }