List of usage examples for javax.xml.transform TransformerFactory newTransformer
public abstract Transformer newTransformer() throws TransformerConfigurationException;
From source file:com.github.woozoo73.ht.XmlUtils.java
public static String getContent(String path) { try {//from w ww . j a v a 2 s .com URL url = XmlUtils.class.getClassLoader().getResource(path); File schemaLocation = new File(url.toURI()); DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.parse(schemaLocation); StringWriter writer = new StringWriter(); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(new DOMSource(document), new StreamResult(writer)); String content = writer.getBuffer().toString(); return content; } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
public static String docToString(Document doc, boolean formated) { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = null; try {// ww w. ja v a2s. com transformer = tf.newTransformer(); } catch (TransformerConfigurationException e) { e.printStackTrace(); return null; } transformer.setOutputProperty(OutputKeys.METHOD, "xml"); if (formated) { // linefeed formatting transformer.setOutputProperty(OutputKeys.INDENT, "yes"); } else { // remove xml header transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); } StringWriter sw = new StringWriter(); StreamResult sr = new StreamResult(sw); DOMSource domSource = new DOMSource(doc); try { transformer.transform(domSource, sr); } catch (TransformerException e) { e.printStackTrace(); return null; } return sw.toString(); }
From source file:Main.java
public static void getStringFromXML(Node node, String dtdFilename, Writer outputWriter) throws TransformerException { File dtdFile = null;/* w ww. ja v a 2s . c om*/ String workingPath = null; if (dtdFilename != null) { dtdFile = new File(dtdFilename); workingPath = System.setProperty("user.dir", dtdFile.getAbsoluteFile().getParent()); } try { if (node instanceof Document) node = ((Document) node).getDocumentElement(); TransformerFactory tranFact = TransformerFactory.newInstance(); Transformer tf = tranFact.newTransformer(); if (dtdFile != null) { tf.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, dtdFile.getName()); tf.setOutputProperty(OutputKeys.INDENT, "yes"); } Source src = new DOMSource(node); Result dest = new StreamResult(outputWriter); tf.transform(src, dest); } finally { if (workingPath != null) System.setProperty("user.dir", workingPath); } }
From source file:Main.java
public static boolean creteEntity(Object entity, String fileName) { boolean flag = false; try {//from ww w .ja v a 2 s .com DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.parse(fileName); Class clazz = entity.getClass(); Field[] fields = clazz.getDeclaredFields(); Node EntityElement = document.getElementsByTagName(clazz.getSimpleName() + "s").item(0); Element newEntity = document.createElement(clazz.getSimpleName()); EntityElement.appendChild(newEntity); for (Field field : fields) { field.setAccessible(true); Element element = document.createElement(field.getName()); element.appendChild(document.createTextNode(field.get(entity).toString())); newEntity.appendChild(element); } TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource domSource = new DOMSource(document); StreamResult streamResult = new StreamResult(new File(fileName)); transformer.transform(domSource, streamResult); flag = true; } catch (ParserConfigurationException pce) { System.out.println(pce.getLocalizedMessage()); pce.printStackTrace(); } catch (TransformerException te) { System.out.println(te.getLocalizedMessage()); te.printStackTrace(); } catch (IOException ioe) { System.out.println(ioe.getLocalizedMessage()); ioe.printStackTrace(); } catch (SAXException sae) { System.out.println(sae.getLocalizedMessage()); sae.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return flag; }
From source file:Main.java
public static String toXML(Document document, boolean format) throws Exception { if (format) { removeWhitespaceNodes(document.getDocumentElement()); }//ww w .j av a 2 s.c o m TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", 2); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); Writer out = new StringWriter(); transformer.transform(new DOMSource(document), new StreamResult(out)); return out.toString(); }
From source file:Main.java
/** Writes the given DOM to the specified output stream. */ public static void writeXML(final OutputStream os, final Document doc) { try {//from w ww.j a v a 2s. c om final TransformerFactory transformFactory = TransformerFactory.newInstance(); final Transformer idTransform = transformFactory.newTransformer(); final Source input = new DOMSource(doc); final Result output = new StreamResult(os); idTransform.transform(input, output); } catch (final TransformerException exc) { exc.printStackTrace(); } // append newline to end of output try { os.write(System.getProperty("line.separator").getBytes()); } catch (final IOException exc) { exc.printStackTrace(); } }
From source file:Main.java
private static boolean updateXML(Document document, String path) throws TransformerException { TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); // out put encoding. transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); DocumentType type = document.getDoctype(); if (type != null) { System.out.println("doctype -->" + type.getPublicId()); transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, type.getPublicId()); transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, type.getSystemId()); }//from ww w . jav a 2s. c om DOMSource source = new DOMSource(document); StreamResult result = new StreamResult(new File(path)); transformer.transform(source, result); transformer.reset(); return true; }
From source file:Main.java
public static String format(String input, int indent) { try {/*from w ww . j a va 2s . co 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
/** * @see //http://stackoverflow.com/questions/139076/how-to-pretty-print-xml-from-java *///from ww w .ja va 2s . c om public static String toXML(Document document, boolean format) throws TransformerException { if (format) { removeWhitespaceNodes(document.getDocumentElement()); } TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", 2); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); Writer out = new StringWriter(); transformer.transform(new DOMSource(document), new StreamResult(out)); return out.toString(); }
From source file:Main.java
public static void saveDocument(Document document, String path) throws TransformerConfigurationException, TransformerFactoryConfigurationError, TransformerFactoryConfigurationError, TransformerException, IOException { StringWriter sw = new StringWriter(); StreamResult sr = new StreamResult(sw); DOMSource dom = new DOMSource(document); TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", 4); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(dom, sr);//from ww w. j av a 2s . c o m String string = sw.toString(); FileWriter fw = new FileWriter(new File(path)); fw.write(string); fw.close(); }