List of usage examples for javax.xml.transform.stream StreamSource StreamSource
public StreamSource(File f)
From source file:Main.java
@SuppressWarnings("rawtypes") public static Object unmarshal(String xmlBody, Class objectClass) { try {//from w w w . j a v a2 s .c om JAXBContext context = JAXBContext.newInstance(objectClass); Unmarshaller u = context.createUnmarshaller(); return u.unmarshal(new StreamSource(new StringReader(xmlBody))); } catch (JAXBException e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static String transformString(String xmlString, String xsltFilePath) throws Exception { StreamResult result = new StreamResult(new StringWriter()); StreamSource s = new StreamSource(new File(xsltFilePath)); StreamSource xml = new StreamSource(new StringReader(xmlString)); 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 void transform(InputStream xslis, InputStream xmlis, OutputStream xmlos) { try {//from w w w .j ava 2s . c o m TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(new StreamSource(xslis)); transformer.transform(new StreamSource(xmlis), new StreamResult(xmlos)); xslis.close(); xmlis.close(); xmlos.close(); } catch (Exception e) { throw new RuntimeException("Fail to do XSLT transformation", e); } }
From source file:Main.java
/** * Formatte un XML/* w ww .jav a 2 s . c om*/ * */ public static String prettyFormat(String input, int indent) { try { 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 (Throwable e) { try { 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)); transformer.transform(xmlInput, xmlOutput); return xmlOutput.getWriter().toString(); } catch (Throwable t) { return input; } } }
From source file:Main.java
public static void validateSChema(String xmlFile, String... xsdFiles) throws SAXException, IOException { Source[] schemas = new Source[xsdFiles.length]; for (int i = 0; i < schemas.length; i++) { schemas[i] = new StreamSource(xsdFiles[i]); }/*from w ww. j a v a 2 s . co m*/ SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); Schema schema = schemaFactory.newSchema(schemas); Validator validator = schema.newValidator(); Source source = new StreamSource(xmlFile); validator.validate(source); }
From source file:Main.java
public static String transform(String xml, File stylesheet) throws TransformerException { StringWriter writer = new StringWriter(); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(new StreamSource(stylesheet)); transformer.transform(new StreamSource(xml), new StreamResult(writer)); return writer.toString(); }
From source file:Main.java
public static synchronized String formatXml(String xml) { ByteArrayOutputStream output = new ByteArrayOutputStream(); try {/*from w ww .j a v a 2 s . co m*/ readableXformer.transform(new StreamSource(new StringReader(xml)), new StreamResult(output)); } catch (TransformerException e) { throw new RuntimeException("Failed to format XML", e); } return output.toString(); }
From source file:Main.java
/** * /* ww w.java 2s.c o m*/ * @param xml * @param indent * @return pretty formatted xml */ public static String prettyFormat(String xml, int indent) { try { Source xmlInput = new StreamSource(new StringReader(xml)); 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); // simple exception handling, please review it } }
From source file:Main.java
public static Object fromXml(@SuppressWarnings("rawtypes") Class clazz, String stringXml) { JAXBContext context = null;/*from w w w.ja v a2s .c o m*/ try { context = JAXBContext.newInstance(clazz); Unmarshaller unmarshaller = context.createUnmarshaller(); return unmarshaller.unmarshal(new StreamSource(new StringReader(stringXml))); } catch (JAXBException e) { e.printStackTrace(); } return null; }
From source file:Main.java
@SuppressWarnings("unchecked") public static <T> T unmarshaller(String xml, Class<?> T) { JAXBContext jc;//from www .j a v a 2 s. c o m Unmarshaller unmarshaller; Object o = null; try { jc = JAXBContext.newInstance(T); unmarshaller = jc.createUnmarshaller(); o = unmarshaller.unmarshal(new StreamSource(new StringReader(xml))); } catch (JAXBException e) { e.printStackTrace(); } return (T) o; }