List of usage examples for javax.xml.parsers DocumentBuilder newDocument
public abstract Document newDocument();
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true);/*from w ww . j ava 2 s. c om*/ DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.newDocument(); Element root = doc.createElementNS(null, "person"); // Create Root Element Element item = doc.createElementNS(null, "name"); // Create element item.appendChild(doc.createTextNode("Jeff")); root.appendChild(item); // Attach element to Root element item = doc.createElementNS(null, "age"); // Create another Element item.appendChild(doc.createTextNode("28")); root.appendChild(item); // Attach Element to previous element down tree item = doc.createElementNS(null, "height"); item.appendChild(doc.createTextNode("1.80")); root.appendChild(item); // Attach another Element - grandaugther doc.appendChild(root); // Add Root to Document DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS domImplLS = (DOMImplementationLS) registry.getDOMImplementation("LS"); LSSerializer ser = domImplLS.createLSSerializer(); // Create a serializer // for the DOM LSOutput out = domImplLS.createLSOutput(); StringWriter stringOut = new StringWriter(); // Writer will be a String out.setCharacterStream(stringOut); ser.write(doc, out); // Serialize the DOM System.out.println("STRXML = " + stringOut.toString()); // DOM as a String }
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 www . jav a 2s . 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
public static void main(String[] args) throws Exception { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder domBuilder = domFactory.newDocumentBuilder(); Document newDoc = domBuilder.newDocument(); Element rootElement = newDoc.createElement("CSV2XML"); newDoc.appendChild(rootElement);//from www. j ava 2 s .c om BufferedReader csvReader = new BufferedReader(new FileReader("csvFileName.txt")); String curLine = csvReader.readLine(); String[] csvFields = curLine.split(","); Element rowElement = newDoc.createElement("row"); for (String value : csvFields) { Element curElement = newDoc.createElement(value); curElement.appendChild(newDoc.createTextNode(value)); rowElement.appendChild(curElement); rootElement.appendChild(rowElement); } csvReader.close(); TransformerFactory tranFactory = TransformerFactory.newInstance(); Transformer aTransformer = tranFactory.newTransformer(); Source src = new DOMSource(newDoc); Result dest = new StreamResult(new File("xmlFileName")); aTransformer.transform(src, dest); }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true);/* w w w . j a va 2 s .com*/ DocumentBuilder db = dbf.newDocumentBuilder(); // Create original document Document document = db.newDocument(); Element root = document.createElementNS("urn:FOO", "ns0:Root"); document.appendChild(root); Element request = document.createElementNS("urn:FOO", "ns0:Request"); root.appendChild(request); // Create new Request element. Element newRequest = document.createElementNS("urn:BAR", "ns1:Request"); // Replace Request element root.replaceChild(newRequest, request); // Output the new 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:DOMGenerate.java
public static void main(String[] argv) { try {/*from w w w .jav a 2 s. c o m*/ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.newDocument(); Element root = doc.createElementNS(null, "person"); // Create Root Element Element item = doc.createElementNS(null, "name"); // Create element item.appendChild(doc.createTextNode("Jeff")); root.appendChild(item); // Attach element to Root element item = doc.createElementNS(null, "age"); // Create another Element item.appendChild(doc.createTextNode("28")); root.appendChild(item); // Attach Element to previous element down tree item = doc.createElementNS(null, "height"); item.appendChild(doc.createTextNode("1.80")); root.appendChild(item); // Attach another Element - grandaugther doc.appendChild(root); // Add Root to Document DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS domImplLS = (DOMImplementationLS) registry.getDOMImplementation("LS"); LSSerializer ser = domImplLS.createLSSerializer(); // Create a serializer // for the DOM LSOutput out = domImplLS.createLSOutput(); StringWriter stringOut = new StringWriter(); // Writer will be a String out.setCharacterStream(stringOut); ser.write(doc, out); // Serialize the DOM System.out.println("STRXML = " + stringOut.toString()); // Spit out the // DOM as a String } catch (Exception ex) { ex.printStackTrace(); } }
From source file:Main.java
public static void main(String args[]) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.newDocument(); Element results = doc.createElement("Results"); doc.appendChild(results);//www. j av a 2s. co m Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager .getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:/access.mdb"); ResultSet rs = con.createStatement().executeQuery("select * from product"); ResultSetMetaData rsmd = rs.getMetaData(); int colCount = rsmd.getColumnCount(); while (rs.next()) { Element row = doc.createElement("Row"); results.appendChild(row); for (int i = 1; i <= colCount; i++) { String columnName = rsmd.getColumnName(i); Object value = rs.getObject(i); Element node = doc.createElement(columnName); node.appendChild(doc.createTextNode(value.toString())); row.appendChild(node); } } DOMSource domSource = new DOMSource(doc); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1"); StringWriter sw = new StringWriter(); StreamResult sr = new StreamResult(sw); transformer.transform(domSource, sr); System.out.println(sw.toString()); con.close(); rs.close(); }
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);// ww w . j av a 2 s . c om // 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!"); }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); // Create a new document Document xmlDoc = builder.newDocument(); // Create root node for the document... Element root = xmlDoc.createElement("Players"); xmlDoc.appendChild(root);/*from w w w. j a v a 2 s . c o m*/ // Create a "player" node Element player = xmlDoc.createElement("player"); // Set the players ID attribute player.setAttribute("ID", "1"); // Create currentRank node... Element currentRank = xmlDoc.createElement("currentRank"); currentRank.setTextContent("1"); player.appendChild(currentRank); // Create previousRank node... Element previousRank = xmlDoc.createElement("previousRank"); previousRank.setTextContent("1"); player.appendChild(previousRank); // Create playerName node... Element playerName = xmlDoc.createElement("PlayerName"); playerName.setTextContent("Max"); player.appendChild(playerName); // Create Money node... Element money = xmlDoc.createElement("Money"); money.setTextContent("15"); player.appendChild(money); // Add the player to the root node... root.appendChild(player); ByteArrayOutputStream baos = null; baos = new ByteArrayOutputStream(); Transformer tf = TransformerFactory.newInstance().newTransformer(); tf.setOutputProperty(OutputKeys.INDENT, "yes"); tf.setOutputProperty(OutputKeys.METHOD, "xml"); tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); DOMSource domSource = new DOMSource(xmlDoc); StreamResult sr = new StreamResult(baos); tf.transform(domSource, sr); baos.flush(); System.out.println(new String(baos.toByteArray())); baos.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { InputStream is = new FileInputStream("test.xml"); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document oldDoc = builder.parse(is); Node oldRoot = oldDoc.getDocumentElement(); Document newDoc = builder.newDocument(); Element newRoot = newDoc.createElement("newroot"); newDoc.appendChild(newRoot);// www . j a v a2 s . c o m newRoot.appendChild(newDoc.importNode(oldRoot, true)); ByteArrayOutputStream out = new ByteArrayOutputStream(); DOMSource domSource = new DOMSource(newDoc); Writer writer = new OutputStreamWriter(out); StreamResult result = new StreamResult(writer); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.transform(domSource, result); writer.flush(); InputStream isNewXML = new ByteArrayInputStream(out.toByteArray()); }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document originalDocument = db.parse(new File("input.xml")); Node originalRoot = originalDocument.getDocumentElement(); Document copiedDocument = db.newDocument(); Node copiedRoot = copiedDocument.importNode(originalRoot, true); copiedDocument.appendChild(copiedRoot); }