List of usage examples for java.io StringWriter StringWriter
public StringWriter()
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new File("in.xml")); TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); transformer.setOutputProperty("indent", "yes"); StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); NodeList nl = doc.getDocumentElement().getChildNodes(); DOMSource source = null;/*from ww w . j av a2s . co m*/ for (int x = 0; x < nl.getLength(); x++) { Node e = nl.item(x); if (e instanceof Element) { source = new DOMSource(e); break; } } transformer.transform(source, result); System.out.println(sw.toString()); }
From source file:Main.java
static public void main(String[] arg) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(true);//from w w w . j a v a 2 s . c om dbf.setNamespaceAware(true); dbf.setIgnoringElementContentWhitespace(true); DocumentBuilder builder = dbf.newDocumentBuilder(); StringReader sr = new StringReader("<tag>java2s.com</tag>"); Document document = builder.parse(new InputSource(sr)); deleteFirstElement(document); TransformerFactory tf = TransformerFactory.newInstance(); Transformer trans = tf.newTransformer(); StringWriter sw = new StringWriter(); trans.transform(new DOMSource(document), new StreamResult(sw)); System.out.println(sw.toString()); }
From source file:Main.java
public static void main(String args[]) throws Exception { Order o = new Order(); o.setCustId(123);// w w w . java2 s . com o.setDescription("New order"); o.setOrderDate(new Date()); List<Item> items = new ArrayList<Item>(); Item i = new Item(); i.setName("PC"); i.setQty(10); items.add(i); i = new Item(); i.setName("Box"); i.setQty(4); items.add(i); o.setItems(items); // Write it JAXBContext ctx = JAXBContext.newInstance(Order.class); Marshaller m = ctx.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); StringWriter sw = new StringWriter(); m.marshal(o, sw); sw.close(); System.out.println(sw.toString()); // Read it back JAXBContext readCtx = JAXBContext.newInstance(Order.class); Unmarshaller um = readCtx.createUnmarshaller(); Order newOrder = (Order) um.unmarshal(new StringReader(sw.toString())); System.out.println(newOrder); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getHSQLConnection(); System.out.println("Got Connection."); Statement st = conn.createStatement(); st.executeUpdate("create table survey (id int,name varchar);"); st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')"); st.executeUpdate("insert into survey (id,name ) values (2,'anotherValue')"); Statement stmt = conn.createStatement(); String sqlQuery = "SELECT * FROM survey WHERE id='1'"; WebRowSet webRS = new WebRowSetImpl(); webRS.setCommand(sqlQuery);//from w w w. j av a2s .c o m webRS.execute(conn); File file = new File("1.xml"); FileWriter fw = new FileWriter(file); System.out.println("Writing db data to file " + file.getAbsolutePath()); webRS.writeXml(fw); StringWriter sw = new StringWriter(); webRS.writeXml(sw); System.out.println(sw.toString()); fw.flush(); fw.close(); stmt.close(); conn.close(); }
From source file:Main.java
public static void main(String args[]) throws Exception { XMLReader xmlReader = new XMLFilterImpl(XMLReaderFactory.createXMLReader()) { String namespace = "http://www.java2s.com/schemas.xsd"; String pref = "ns0:"; @Override// w w w . ja v a 2 s . co m public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { super.startElement(namespace, localName, pref + qName, atts); } @Override public void endElement(String uri, String localName, String qName) throws SAXException { super.endElement(namespace, localName, pref + qName); } }; TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); StringWriter s = new StringWriter(); t.transform(new SAXSource(xmlReader, new InputSource("test.xml")), new StreamResult(s)); System.out.println(s); }
From source file:SelectingComboSample.java
public static void main(String args[]) { String labels[] = { "A", "B", "C", "D", "E", "F", "G", "H", "J", "I" }; JFrame frame = new JFrame("Selecting JComboBox"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container contentPane = frame.getContentPane(); JComboBox comboBox = new JComboBox(labels); contentPane.add(comboBox, BorderLayout.SOUTH); final JTextArea textArea = new JTextArea(); textArea.setEditable(false);//from w w w. j a va 2 s . c o m JScrollPane sp = new JScrollPane(textArea); contentPane.add(sp, BorderLayout.CENTER); ItemListener itemListener = new ItemListener() { public void itemStateChanged(ItemEvent itemEvent) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); int state = itemEvent.getStateChange(); String stateString = ((state == ItemEvent.SELECTED) ? "Selected" : "Deselected"); pw.print("Item: " + itemEvent.getItem()); pw.print(", State: " + stateString); ItemSelectable is = itemEvent.getItemSelectable(); pw.print(", Selected: " + selectedString(is)); pw.println(); textArea.append(sw.toString()); } }; comboBox.addItemListener(itemListener); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); pw.print("Command: " + actionEvent.getActionCommand()); ItemSelectable is = (ItemSelectable) actionEvent.getSource(); pw.print(", Selected: " + selectedString(is)); pw.println(); textArea.append(sw.toString()); } }; comboBox.addActionListener(actionListener); frame.setSize(400, 200); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); DOMImplementation impl = builder.getDOMImplementation(); Document doc = impl.createDocument(null, null, null); Element e1 = doc.createElement("api"); doc.appendChild(e1);// w ww . ja v a 2 s . c o m Element e2 = doc.createElement("java"); e1.appendChild(e2); e2.setAttribute("url", "http://www.domain.com"); DOMSource domSource = new DOMSource(doc); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StringWriter sw = new StringWriter(); StreamResult sr = new StreamResult(sw); transformer.transform(domSource, sr); System.out.println(sw.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 www . j a v a 2s .c om // 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:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); DOMImplementation impl = builder.getDOMImplementation(); Document doc = impl.createDocument(null, null, null); Element e1 = doc.createElement("api"); doc.appendChild(e1);//from w w w. j av a2 s . co m Element e2 = doc.createElement("java"); e1.appendChild(e2); e2.setAttribute("url", "http://www.java2s.com"); //transform the DOM for showing the result in console DOMSource domSource = new DOMSource(doc); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StringWriter sw = new StringWriter(); StreamResult sr = new StreamResult(sw); transformer.transform(domSource, sr); System.out.println(sw.toString()); }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); DOMImplementation impl = builder.getDOMImplementation(); Document doc = impl.createDocument(null, null, null); Element e1 = doc.createElement("api"); doc.appendChild(e1);/*from w ww .ja va2 s. c o m*/ Element e2 = doc.createElement("java"); e1.appendChild(e2); e2.setAttribute("url", "http://www.java2s.com"); // transform the DOM for showing the result in console DOMSource domSource = new DOMSource(doc); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StringWriter sw = new StringWriter(); StreamResult sr = new StreamResult(sw); transformer.transform(domSource, sr); System.out.println(sw.toString()); }