List of usage examples for org.w3c.dom Document createElement
public Element createElement(String tagName) throws DOMException;
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder loader = factory.newDocumentBuilder(); Document document = loader.newDocument(); Element root = document.createElement("order"); document.appendChild(root);//from w w w . j av a2 s. c o m }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder loader = factory.newDocumentBuilder(); Document document = loader.newDocument(); Element order = document.createElement("order"); document.appendChild(order);//from w ww .j a v a 2 s. c om XMLSerializer serializer = new XMLSerializer(); serializer.setOutputCharStream(new java.io.FileWriter("order.xml")); serializer.serialize(document); }
From source file:MainClass.java
public static void main(String args[]) { Document dom = DOMImplementation.createDocument(null, null, null); Element root = dom.createElement("games"); Element child1 = dom.createElement("game"); root.appendChild(child1);// www . jav a 2 s . com dom.appendChild(root); }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true);/*ww w . jav a2 s . c o m*/ DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.newDocument(); Element rootElement = document.createElement("root"); document.appendChild(rootElement); rootElement.setAttributeNS(XMLConstants.XML_NS_URI, "space", "preserve"); TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); t.transform(new DOMSource(document), new StreamResult(System.out)); }
From source file:MainClass.java
public static void main(String args[]) { Document dom = DOMImplementation.createDocument(null, null, null); Element root = dom.createElement("games"); Element child1 = dom.createElement("game"); root.appendChild(child1);/* ww w .j a va2 s . c o m*/ child1.setAttribute("A", "a"); }
From source file:Main.java
public static void main(String[] args) throws Throwable { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true);// w w w . jav a2 s. co m DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.newDocument(); Element root = doc.createElement("root"); root.setAttribute("xmlns:m", "http://www.java2s.com/blog"); root.setAttribute("xmlns:rt", "http://www.java2s.com/forum"); doc.appendChild(root); Element elt = doc.createElement("simple"); elt.setAttribute("m:Path", "false"); elt.setAttribute("m:Content", "false"); elt.setAttribute("rt:file", "false"); root.appendChild(doc.createTextNode("\n\t")); root.appendChild(elt); root.appendChild(doc.createTextNode("\n")); TransformerFactory.newInstance().newTransformer().transform(new DOMSource(doc), new StreamResult(System.out)); }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = dbf.newDocumentBuilder(); Document doc = builder.newDocument(); Element element = doc.createElement("root"); doc.appendChild(element);// w ww . j a va 2s . com Comment comment = doc.createComment("This is a comment"); doc.insertBefore(comment, element); Element itemElement = doc.createElement("item"); element.appendChild(itemElement); itemElement.setAttribute("myattr", "attr>value"); itemElement.insertBefore(doc.createTextNode("te<xt"), itemElement.getLastChild()); prettyPrint(doc); }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.newDocument(); Element book = document.createElement("book"); book.setAttribute("id", "javanut4"); document.appendChild(book);/*from w ww . java 2s .com*/ for (int i = 1; i <= 3; i++) { Element chapter = document.createElement("chapter"); Element title = document.createElement("title"); title.appendChild(document.createTextNode("Chapter " + i)); chapter.appendChild(title); chapter.appendChild(document.createElement("para")); book.appendChild(chapter); } TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); DOMSource source = new DOMSource(document); StreamResult result = new StreamResult(System.out); transformer.transform(source, result); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); Document retval = dbf.newDocumentBuilder().newDocument(); Element parent = retval.createElement("parent"); retval.appendChild(parent);/*from www . j a v a 2 s.c om*/ Element child1 = retval.createElement("child"); child1.setTextContent("child.text"); parent.appendChild(child1); Element child2 = retval.createElement("child"); child2.setTextContent("child.text.2"); parent.appendChild(child2); XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); System.out.println(xPath.evaluate("//child/text()", retval, XPathConstants.NODE).getClass()); }
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);//from w ww . j av a 2 s.com 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()); }