List of usage examples for javax.xml.transform TransformerFactory newTransformer
public abstract Transformer newTransformer() throws TransformerConfigurationException;
From source file:Main.java
/** * Writes out XML./*from w w w . j a v a2 s . c o m*/ * * @param doc document * @param file target file * @throws TransformerException on transformer error */ public static void writeXml(Document doc, File file) throws TransformerException { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(file); transformer.transform(source, result); }
From source file:Main.java
private static String xmlToString(Node node) { try {// w w w .j a v a 2s . com Source source = new DOMSource(node); StringWriter stringWriter = new StringWriter(); Result result = new StreamResult(stringWriter); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.transform(source, result); return stringWriter.getBuffer().toString(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } return null; }
From source file:scala.c24.demo.java.C24DemoUtils.java
public static String getDocumentAsString(Resource resource) throws Exception { Document document = getDocument(resource); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(document), new StreamResult(writer)); String output = writer.getBuffer().toString().replaceAll("\n|\r", ""); return output; }
From source file:Main.java
public static String getContent(Node node, boolean omitXMLDeclaration) { try {//from ww w. ja v a2 s . c o m ByteArrayOutputStream baos = new ByteArrayOutputStream(); // Use a Transformer for output TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); transformer.setOutputProperty("indent", "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.setOutputProperty("encoding", "UTF-8"); if (omitXMLDeclaration) { transformer.setOutputProperty("omit-xml-declaration", "yes"); } DOMSource source = new DOMSource(node); StreamResult result = new StreamResult(baos); transformer.transform(source, result); String cont = baos.toString("UTF8"); baos.close(); return cont; } catch (Exception ex) { return ""; } }
From source file:Main.java
/** * Creates a string representation of a {@link Node} instance. This method * does not introduce any character to the string representation of the * {@link Node} (eg. \n or \r characters) * * @param node A {@link Node} instance//w w w. j a va 2 s . c om * @return A string representation of the node instance * @throws RequestSecurityTokenException */ public static String xmlToString(Node node) throws Exception { try { Source source = new DOMSource(node); StringWriter stringWriter = new StringWriter(); Result result = new StreamResult(stringWriter); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.transform(source, result); return stringWriter.getBuffer().toString(); } catch (TransformerConfigurationException e) { e.printStackTrace(); throw new Exception("Cannot build a string representation of the assertion."); } catch (TransformerException e) { e.printStackTrace(); throw new Exception("Cannot build a string representation of the assertion."); } }
From source file:Main.java
static public void outputToStream(Document document, OutputStream outputStream) throws TransformerException { // Use a Transformer for output TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); // transformer.setOutputProperty(OutputKeys.INDENT, "yes"); DOMSource source = new DOMSource(document); StreamResult result = new StreamResult(outputStream); transformer.transform(source, result); }
From source file:Main.java
static public void outputToStream(Element document, OutputStream outputStream) throws TransformerException { // Use a Transformer for output TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); // transformer.setOutputProperty(OutputKeys.INDENT, "yes"); DOMSource source = new DOMSource(document); StreamResult result = new StreamResult(outputStream); transformer.transform(source, result); }
From source file:Main.java
public static org.w3c.dom.Document writeToFile(String xmlContent, String path) { System.out.println("This is the path " + path); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder;/* ww w .ja v a2s.c o m*/ try { builder = factory.newDocumentBuilder(); org.w3c.dom.Document doc = builder.parse(new InputSource(new StringReader(xmlContent))); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new File(path)); transformer.transform(source, result); } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:Main.java
/** * Converts an XML {@link org.w3c.dom.Document} to a string. * * @param doc the XML document/* ww w. j a v a 2 s. com*/ * @return the string representation of the XML document */ public static String documentToString(Document doc) { try { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(writer)); return writer.getBuffer().toString(); } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(System.err); } return ""; }
From source file:Main.java
public static void addHandset(String file, String name, Hashtable<String, String> attri) throws Exception { DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance(); DocumentBuilder dombuilder = domfac.newDocumentBuilder(); FileInputStream is = new FileInputStream(file); Document doc = dombuilder.parse(is); NodeList nodeList = doc.getElementsByTagName("devices"); if (nodeList != null && nodeList.getLength() >= 1) { Node deviceNode = nodeList.item(0); Element device = doc.createElement("device"); device.setTextContent(name);//from ww w . j av a2s .c o m for (Iterator itrName = attri.keySet().iterator(); itrName.hasNext();) { String attriKey = (String) itrName.next(); String attriValue = (String) attri.get(attriKey); device.setAttribute(attriKey, attriValue); } deviceNode.appendChild(device); } //save TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); Properties props = t.getOutputProperties(); props.setProperty(OutputKeys.ENCODING, "GB2312"); t.setOutputProperties(props); DOMSource dom = new DOMSource(doc); StreamResult sr = new StreamResult(file); t.transform(dom, sr); }