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 URI./*from w w w . j a va 2 s . co m*/ * @param uri Uri for the source. * @return the source. */ public static Source createSource(String uri) { return new StreamSource(uri); }
From source file:Main.java
public static String transform(String xslt, String xml, String charsetName) throws TransformerException { return transform(new StreamSource(new StringReader(xslt)), new StreamSource(new StringReader(xml)), charsetName);// ww w.j a va 2s . co m }
From source file:Main.java
/** * @param xml/*from w w w . ja va 2 s . c om*/ * @return pretty xml */ public static String prettyXml(String xml) { TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer serializer; try { Source source = new StreamSource(new StringReader(xml)); StringWriter writer = new StringWriter(); serializer = tfactory.newTransformer(); // Setup indenting to "pretty print" serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); serializer.transform(source, new StreamResult(writer)); return writer.toString(); } catch (Exception e) { return xml; } }
From source file:Main.java
/** * Formats a xml string// w w w . j av a 2 s. c om * @param input * @param indent * @return */ public static String prettyFormatXml(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 (Exception e) { e.printStackTrace(); } return input; }
From source file:Main.java
public static Templates getTemplatesByName(File xslF) { Templates templates = null;/*from w ww. ja va 2 s . c o m*/ TransformerFactory tfactory = TransformerFactory.newInstance(); tfactory.setAttribute("http://xml.apache.org/xalan/features/incremental", java.lang.Boolean.TRUE); InputStream is = null; try { StreamSource ss = new StreamSource(xslF); is = ss.getInputStream(); templates = tfactory.newTemplates(ss); if (is != null) { is.close(); is = null; } } catch (Exception e) { e.printStackTrace(); } finally { try { if (is != null) { is.close(); is = null; } } catch (Throwable t) { System.out.println(t); } } return templates; }
From source file:Main.java
public static boolean validateXMLString(String xsdPath, String xml) { try {/* ww w .j av a 2 s . c om*/ SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = factory.newSchema(new File(xsdPath)); Validator validator = schema.newValidator(); Source source = new StreamSource(new StringReader(xml)); validator.validate(source); } catch (Exception e) { e.printStackTrace(); return false; } return true; }
From source file:Main.java
public static void stringToResult(String source, Result result) throws IOException { try {/*from w w w . ja va2 s . com*/ Transformer trans = transFactory.newTransformer(); StringReader reader = new StringReader(source); trans.transform(new StreamSource(reader), result); } catch (TransformerException ex) { throw new IOException(ex); } }
From source file:Main.java
public static Node bytesToXml(final byte[] body) throws TransformerException { final Transformer transformer = FACTORY.newTransformer(); final Source source = new StreamSource(new ByteArrayInputStream(body)); final DOMResult result = new DOMResult(); transformer.transform(source, result); return result.getNode(); }
From source file:Main.java
public static boolean validateXMLSchema(String xsdPath, String xmlPath) { try {/* w w w .j av a2 s .co m*/ SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = factory.newSchema(new File(xsdPath)); Validator validator = schema.newValidator(); validator.validate(new StreamSource(new File(xmlPath))); } catch (IOException e) { System.out.println("Exception: " + e.getMessage()); return false; } catch (SAXException e) { System.out.println("Exception: " + e.getMessage()); return false; } return true; }
From source file:Main.java
public static Node transform(String xslSource, Node original) { StringReader sr = new StringReader(xslSource); DOMResult result = new DOMResult(); doTransform(new StreamSource(sr), new DOMSource(original), result); return result.getNode(); }