List of usage examples for javax.xml.transform.stream StreamResult getWriter
public Writer getWriter()
From source file:Main.java
public static void main(final String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true);//from w ww.ja v a2s. c o m DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new ByteArrayInputStream(( // "<?xml version=\"1.0\"?>" + // "<people>" + // "<person><name>First Person Name</name></person>" + // "<person><name>Second Person Name</name></person>" + // "</people>" // ).getBytes())); String fragment = "<name>Changed Name</name>"; Document fragmentDoc = builder.parse(new ByteArrayInputStream(fragment.getBytes())); Node injectedNode = doc.adoptNode(fragmentDoc.getFirstChild()); XPath xPath = XPathFactory.newInstance().newXPath(); XPathExpression expr = xPath.compile("//people/person[2]/name"); Element nodeFound = (Element) expr.evaluate(doc, XPathConstants.NODE); Node parentNode = nodeFound.getParentNode(); parentNode.removeChild(nodeFound); parentNode.appendChild(injectedNode); DOMSource domSource = new DOMSource(doc); Transformer transformer = TransformerFactory.newInstance().newTransformer(); StreamResult result = new StreamResult(new StringWriter()); transformer.transform(domSource, result); System.out.println(result.getWriter().toString()); }
From source file:Main.java
License:asdf
public static void main(String[] args) throws Exception { String initial = "<root><param value=\"abc\"/><param value=\"bc\"/></root>"; ByteArrayInputStream is = new ByteArrayInputStream(initial.getBytes()); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(is);/*from ww w . ja va 2 s . c o m*/ // Create the new xml fragment Text a = doc.createTextNode("asdf"); Node p = doc.createElement("parameterDesc"); p.appendChild(a); Node i = doc.createElement("insert"); i.appendChild(p); Element r = doc.getDocumentElement(); r.insertBefore(i, r.getFirstChild()); r.normalize(); // Format the xml for output Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // initialize StreamResult with File object to save to file StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(doc); transformer.transform(source, result); System.out.println(result.getWriter().toString()); }
From source file:dk.defxws.fedoragsearch.server.GTransformer.java
public static void main(String[] args) { int argCount = 2; try {/*from w ww.j av a2 s . c o m*/ if (args.length == argCount) { File f = new File(args[1]); StreamSource ss = new StreamSource(new File(args[1])); GTransformer gt = new GTransformer(); StreamResult destStream = new StreamResult(new StringWriter()); gt.transform(args[0], ss, destStream); StringWriter sw = (StringWriter) destStream.getWriter(); System.out.print(sw.getBuffer().toString()); } else { throw new IOException("Must supply " + argCount + " arguments."); } } catch (Exception e) { e.printStackTrace(); System.err.println("Usage: GTransformer xsltName xmlFileName"); } }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = docBuilderFactory.newDocumentBuilder(); Document node = documentBuilder.parse(new FileInputStream("data.xml")); cleanEmptyTextNodes(node);// ww w . j a v a 2 s . c o m StreamResult result = new StreamResult(new StringWriter()); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", Integer.toString(4)); transformer.transform(new DOMSource(node), result); System.out.println(result.getWriter().toString()); }
From source file:Main.java
public static String doc2str(Document doc) throws TransformerConfigurationException, TransformerException { StreamResult result = new StreamResult(new StringWriter()); doc2stream(doc, result);// w w w. ja va2 s.co m String xmlString = result.getWriter().toString(); return xmlString; }
From source file:Main.java
public static String transformToString(Source source) { try {//from w ww . j av a2 s. c o m Transformer transformer = TransformerFactory.newInstance().newTransformer(); StreamResult result = new StreamResult(new StringWriter()); transformer.transform(source, result); return result.getWriter().toString(); } catch (TransformerException ex) { ex.printStackTrace(); return null; } }
From source file:Main.java
/** * Transforms the given XML document into its textual representation. * * @param doc the document to transform. * @return the XML document transformed to a string. * * @throws TransformerException if the transformation failed. *///from w ww.jav a 2s . com public static String documentToString(Document doc) throws TransformerException { StreamResult result = new StreamResult(new StringWriter()); transform(doc, result); String xmlString = result.getWriter().toString(); return xmlString; }
From source file:Main.java
public static String saveInString(Document doc) { StreamResult streamResult = new StreamResult(new StringWriter()); save(doc, streamResult);// ww w. j av a2 s . co m return streamResult.getWriter().toString(); }
From source file:Main.java
public static String sourceToXMLString(Source result) { String xmlResult = null;/* w ww .j a va 2 s . co m*/ try { TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); //transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); StringWriter out = new StringWriter(); StreamResult streamResult = new StreamResult(out); transformer.transform(result, streamResult); xmlResult = streamResult.getWriter().toString(); } catch (TransformerException e) { e.printStackTrace(); } return xmlResult; }
From source file:Main.java
public static String prettyPrint(DOMSource domSource) { try {/*from ww w . j a v a 2 s .c o m*/ Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult streamResult = new StreamResult(new StringWriter()); transformer.transform(domSource, streamResult); return streamResult.getWriter().toString(); } catch (Exception e) { throw new RuntimeException("Error while pretty printing xml", e); } }