List of usage examples for javax.xml.transform.stream StreamResult StreamResult
public StreamResult(File f)
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = db.parse(new InputSource( new StringReader("<emp><empname><firstName></firstName><lastName></lastName></empname></emp>"))); NodeList customerNodes = doc.getElementsByTagName("empname"); for (int i = 0; i < customerNodes.getLength(); i++) { NodeList children = customerNodes.item(i).getChildNodes(); for (int j = 0; j < children.getLength(); j++) { String childNode = children.item(j).getNodeName(); if (childNode.equalsIgnoreCase("firstName")) { children.item(j).setTextContent(String.valueOf("John")); } else if (childNode.equalsIgnoreCase("lastName")) { children.item(j).setTextContent(String.valueOf("Doe")); }/*from w ww .j av a 2 s . c o m*/ } } TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(writer)); System.out.println(writer.getBuffer().toString()); }
From source file:UseStylesheetPI.java
public static void main(String[] args) throws TransformerException, TransformerConfigurationException { String media = null, title = null, charset = null; try {// ww w . j av a 2 s .co m TransformerFactory tFactory = TransformerFactory.newInstance(); Source stylesheet = tFactory.getAssociatedStylesheet(new StreamSource("fooX.xml"), media, title, charset); Transformer transformer = tFactory.newTransformer(stylesheet); transformer.transform(new StreamSource("fooX.xml"), new StreamResult(new java.io.FileOutputStream("foo.out"))); System.out.println("************* The result is in foo.out *************"); } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true);/*from www. j a va 2 s .c o m*/ DocumentBuilder db = dbf.newDocumentBuilder(); File file1 = new File("input1.xml"); Document doc1 = db.parse(file1); Element rootElement1 = doc1.getDocumentElement(); File file2 = new File("input2.xml"); Document doc2 = db.parse(file2); Element rootElement2 = doc2.getDocumentElement(); // Copy Attributes NamedNodeMap namedNodeMap2 = rootElement2.getAttributes(); for (int x = 0; x < namedNodeMap2.getLength(); x++) { Attr importedNode = (Attr) doc1.importNode(namedNodeMap2.item(x), true); rootElement1.setAttributeNodeNS(importedNode); } // Copy Child Nodes NodeList childNodes2 = rootElement2.getChildNodes(); for (int x = 0; x < childNodes2.getLength(); x++) { Node importedNode = doc1.importNode(childNodes2.item(x), true); rootElement1.appendChild(importedNode); } // Output Document TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); DOMSource source = new DOMSource(doc1); StreamResult result = new StreamResult(System.out); t.transform(source, result); }
From source file:org.blanco.test.spring.xmltest.Main.java
public static void main(String args[]) throws XmlMappingException, IOException { ApplicationContext ctx = new ClassPathXmlApplicationContext("/spring-config.xml"); Marshaller mar = (Marshaller) ctx.getBean("marshaller"); Person alex = (Person) ctx.getBean("alex"); Result result = new StreamResult(System.out); mar.marshal(alex, result);//from w w w .j ava2 s .c o m System.exit(0); }
From source file:XMLTransform.java
public static void main(String args[]) { if (args.length != 2) { System.err.println("Usage: java XMLTransform xmlfile.xml stylesheet.xsl"); System.exit(-1);//from w w w . j a v a 2 s . c o m } try { StreamSource source = new StreamSource(args[0]); StreamSource stylesource = new StreamSource(args[1]); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(stylesource); StreamResult result = new StreamResult(System.out); transformer.transform(source, result); } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
public static void main(final String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true);/*from w w w.j a 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
public static void main(String[] args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true);/*from w ww . ja v a 2 s .c o m*/ DocumentBuilder db = dbf.newDocumentBuilder(); File file1 = new File("input1.xml"); Document doc1 = db.parse(file1); Element rootElement1 = doc1.getDocumentElement(); File file2 = new File("input2.xml"); Document doc2 = db.parse(file2); Element rootElement2 = doc2.getDocumentElement(); // Copy Child Nodes NodeList childNodes2 = rootElement2.getChildNodes(); for (int x = 0; x < childNodes2.getLength(); x++) { Node importedNode = doc1.importNode(childNodes2.item(x), true); if (importedNode.getNodeType() == Node.ELEMENT_NODE) { Element importedElement = (Element) importedNode; // Copy Attributes NamedNodeMap namedNodeMap2 = rootElement2.getAttributes(); for (int y = 0; y < namedNodeMap2.getLength(); y++) { Attr importedAttr = (Attr) doc1.importNode(namedNodeMap2.item(y), true); importedElement.setAttributeNodeNS(importedAttr); } } rootElement1.appendChild(importedNode); } // Output Document TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); DOMSource source = new DOMSource(doc1); StreamResult result = new StreamResult(System.out); t.transform(source, result); }
From source file:Main.java
public static void main(String argv[]) throws Exception { String next = "keyword,123"; String[] input = next.split(","); String textToFind = input[0].replace("'", "\\'"); // "CEO"; String textToReplace = input[1].replace("'", "\\'"); // "Chief Executive Officer"; String filepath = "root.xml"; String fileToBeSaved = "root2.xml"; DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); Document doc = docBuilder.parse(filepath); XPath xpath = XPathFactory.newInstance().newXPath(); // change ELEMENTS String xPathExpression = "//*[text()='" + textToFind + "']"; NodeList nodes = (NodeList) xpath.evaluate(xPathExpression, doc, XPathConstants.NODESET); for (int idx = 0; idx < nodes.getLength(); idx++) { nodes.item(idx).setTextContent(textToReplace); }// w ww .j av a2 s .c o m // change ATTRIBUTES String xPathExpressionAttr = "//*/@*[.='" + textToFind + "']"; NodeList nodesAttr = (NodeList) xpath.evaluate(xPathExpressionAttr, doc, XPathConstants.NODESET); for (int i = 0; i < nodesAttr.getLength(); i++) { nodesAttr.item(i).setTextContent(textToReplace); } System.out.println("Everything replaced."); // save xml file back TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new File(fileToBeSaved)); transformer.transform(source, result); }
From source file:UseStylesheetParam.java
public static void main(String[] args) throws TransformerException, TransformerConfigurationException, SAXException, IOException { if (args.length != 1) { System.err.println("Please pass one string to this program"); return;// w ww. jav a 2 s .c o m } // Get the parameter value from the command line. String paramValue = args[0]; TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(new StreamSource("foo.xsl")); // Set the parameter. I can't get non-null namespaces to work!! transformer.setParameter("param1", /* parameter name */ paramValue /* parameter value */ ); transformer.transform(new StreamSource("foo.xml"), new StreamResult(new OutputStreamWriter(System.out))); }
From source file:Main.java
public static void main(String argv[]) throws Exception { DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); // root elements Document doc = docBuilder.newDocument(); Element rootElement = doc.createElement("company"); doc.appendChild(rootElement);//from w ww . j a va 2s . c o m // staff elements Element staff = doc.createElement("Staff"); rootElement.appendChild(staff); // set attribute to staff element Attr attr = doc.createAttribute("id"); attr.setValue("1"); staff.setAttributeNode(attr); // shorten way // staff.setAttribute("id", "1"); // firstname elements Element firstname = doc.createElement("firstname"); firstname.appendChild(doc.createTextNode("A")); staff.appendChild(firstname); // lastname elements Element lastname = doc.createElement("lastname"); lastname.appendChild(doc.createTextNode("B")); staff.appendChild(lastname); // nickname elements Element nickname = doc.createElement("nickname"); nickname.appendChild(doc.createTextNode("C")); staff.appendChild(nickname); // salary elements Element salary = doc.createElement("salary"); salary.appendChild(doc.createTextNode("100000")); staff.appendChild(salary); // write the content into xml file TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new File("C:\\file.xml")); // Output to console for testing // StreamResult result = new StreamResult(System.out); transformer.transform(source, result); System.out.println("File saved!"); }