List of usage examples for javax.xml.parsers DocumentBuilderFactory newDocumentBuilder
public abstract DocumentBuilder newDocumentBuilder() throws ParserConfigurationException;
From source file:Main.java
License:asdf
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); Document doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(getXMLData()))); newEmail(doc, "email", "newEmail"); System.out.println(documentToString(doc)); }
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. ja v a2 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
public static void main(String args[]) throws Exception { File stocks = new File("Stocks.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(stocks); doc.getDocumentElement().normalize(); System.out.println(doc.getDocumentElement().getNodeName()); NodeList nodes = doc.getElementsByTagName("stock"); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) node; System.out.println("Stock Symbol: " + getValue("symbol", element)); System.out.println("Stock Price: " + getValue("price", element)); System.out.println("Stock Quantity: " + getValue("quantity", element)); }// w ww . j a v a 2 s. c o m } }
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;// ww w .jav a2 s .c om 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
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); Document doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(getXMLData()))); append(doc, "newName", "1111111111", "newEmail"); System.out.println(documentToString(doc)); }
From source file:Main.java
public static void main(String[] args) throws Exception { // Create the JAXBContext JAXBContext jc = JAXBContext.newInstance(Main.class); // Create the Object Main foo = new Main(); foo.setBar("Hello World"); // Create the Document DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.newDocument(); // Marshal the Object to a Document Marshaller marshaller = jc.createMarshaller(); marshaller.marshal(foo, document);/*from w w w . j a v a 2 s . c o m*/ // Output the Document TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); DOMSource source = new DOMSource(document); StreamResult result = new StreamResult(System.out); t.transform(source, result); }
From source file:Main.java
License:asdf
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); Document doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(getXMLData()))); replacePerson(doc, "newName", "111111", "newEmail@email.com"); System.out.println(documentToString(doc)); }
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 w w . ja v a 2s .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()); }
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 w w . ja v a2s . 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
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 w w w . j a v a 2 s. co 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()); }