List of usage examples for javax.xml.transform TransformerFactory newInstance
public static TransformerFactory newInstance() throws TransformerFactoryConfigurationError
From source file:Main.java
/** * Performs XSL transformation.//from ww w . j a v a2s. c o m * @param xmlDoc Document * @param xslDoc Document * @throws TransformerConfigurationException * @throws TransformerException * @return String The output text. */ public static String xslTransform(Document xmlDoc, Document xslDoc) throws TransformerConfigurationException, TransformerException { DOMSource source = new DOMSource(xmlDoc); DOMSource xslSrc = new DOMSource(xslDoc); TransformerFactory xformFactory = TransformerFactory.newInstance(); Transformer transformer = xformFactory.newTransformer(xslSrc); java.io.StringWriter sw = new java.io.StringWriter(); StreamResult outXml = new StreamResult(sw); transformer.transform(source, outXml); return sw.toString(); }
From source file:Main.java
private static Transformer createXmlTransformer() throws Exception { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); return transformer; }
From source file:Main.java
/** * Makes the specified XML string pretty. * //from www . j av a2 s.c o m * @param xmlString * The XML string to process. * @return Pretty-printed XML string. */ public static final String prettyPrint(final String xmlString) { try (InputStream inputStream = new ByteArrayInputStream(xmlString.getBytes()); ByteArrayOutputStream baos = new ByteArrayOutputStream()) { final DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); final Document document = documentBuilder.parse(inputStream); final TransformerFactory tfactory = TransformerFactory.newInstance(); final Transformer serializer = tfactory.newTransformer(); // Setup indenting to "pretty print" serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); final DOMSource xmlSource = new DOMSource(document); final StreamResult outputTarget = new StreamResult(baos); serializer.transform(xmlSource, outputTarget); return baos.toString("utf-8"); } catch (ParserConfigurationException | TransformerException | SAXException | IOException ex) { throw new RuntimeException("Can't pretty print xml!", ex); } }
From source file:Main.java
public static void removeHandset(String file, String name) throws Exception { DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance(); DocumentBuilder dombuilder = domfac.newDocumentBuilder(); FileInputStream is = new FileInputStream(file); Document doc = dombuilder.parse(is); NodeList devices = doc.getElementsByTagName("devices"); NodeList nodeList = doc.getElementsByTagName("device"); for (int i = 0; i < nodeList.getLength(); i++) { Node deviceNode = nodeList.item(i); if (deviceNode.getTextContent().equals(name)) { devices.item(0).removeChild(deviceNode); }/*from ww w . j a v a 2 s .c o m*/ } //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); }
From source file:Main.java
public static String getStringFromDocument(Document doc) throws Exception { DOMSource domSource = new DOMSource(doc); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.transform(domSource, result); return writer.toString(); }
From source file:Main.java
private static DOMResult transform(SOAPPart part) throws Exception { Transformer trans = TransformerFactory.newInstance().newTransformer(); DOMResult rs = new DOMResult(); trans.transform(part.getContent(), rs); return rs;//w ww . j a v a 2 s .c om }
From source file:Main.java
public static String createString(Element element) { try {/*w w w .j a v a 2 s . c om*/ TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); DOMSource source = new DOMSource(element); Writer writer = new StringWriter(); Result result = new StreamResult(writer); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(source, result); return writer.toString(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } return ""; }
From source file:Main.java
/** * Converts a DOM Tree to a String./* www . j a v a2 s.c om*/ * * @param xml The DOM document to convert. * * @return A string containing the XML if successful, null otherwise. */ public static String xmlToString(Document xml) { try { TransformerFactory f = TransformerFactory.newInstance(); StringWriter writer = new StringWriter(); Transformer t = f.newTransformer(); DOMSource src = new DOMSource(xml); StreamResult result = new StreamResult(writer); t.transform(src, result); return writer.toString(); } catch (TransformerException e) { return null; } }
From source file:Main.java
/** * Saves the XML file to disk./*w ww. j av a 2s . c om*/ * @param fileName The name of the file. * @param doc The XML Document to save to disk. * @throws TransformerConfigurationException * @throws TransformerException */ private static void store(String fileName, Document doc) throws TransformerConfigurationException, TransformerException { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(new DOMSource(doc), new StreamResult(new File(fileName))); }
From source file:Main.java
public static String nodeToString(final Node node, final boolean omitXMLDecl) { final StringWriter writer = new StringWriter(); final Transformer transformer; try {/*w w w.ja va2 s.c om*/ transformer = TransformerFactory.newInstance().newTransformer(); } catch (final TransformerException e) { throw new AssertionError("No errors expected when creating an identity transformer", e); } if (omitXMLDecl) { transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); } else { transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); } try { transformer.transform(new DOMSource(node), new StreamResult(writer)); } catch (final TransformerException e) { throw new AssertionError("No errors expected during identity transformation", e); } return writer.toString(); }