List of usage examples for org.w3c.dom Document createTextNode
public Text createTextNode(String data);
Text
node given the specified string. From source file:Main.java
public static void main(String args[]) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); DOMImplementation impl = builder.getDOMImplementation(); Document xmldoc = impl.createDocument(null, "XMLDoc", null); Element root = xmldoc.getDocumentElement(); Element e0 = xmldoc.createElement("Doc"); Element e1 = xmldoc.createElement("TITLE"); Node n1 = xmldoc.createTextNode("Java"); e1.appendChild(n1);//from w w w.j a v a 2 s . c om Element e2 = xmldoc.createElement("data"); Node n2 = xmldoc.createTextNode("text node"); e2.appendChild(n2); e0.appendChild(e1); e0.appendChild(e2); root.appendChild(e0); StreamResult out = new StreamResult("howto.xml"); DOMSource domSource = new DOMSource(xmldoc); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); 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"); transformer.transform(domSource, out); }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);//from w ww. j ava2 s . co m factory.setExpandEntityReferences(false); Document doc = factory.newDocumentBuilder().parse(new File("filename")); Element element = doc.getElementById("key1"); CDATASection cdataNode = doc.createCDATASection(""); Comment commentNode = doc.createComment(""); Text textNode = doc.createTextNode(""); CharacterData cdata = cdataNode; cdata = commentNode; cdata = textNode; cdata.setData("some data"); int len = cdata.getLength(); }
From source file:Main.java
public static void main(String args[]) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); DOMImplementation impl = builder.getDOMImplementation(); Document xmldoc = impl.createDocument(null, "TODOs", null); Element root = xmldoc.getDocumentElement(); Element e0 = xmldoc.createElement("TOPIC"); Element e1 = xmldoc.createElement("TITLE"); Node n1 = xmldoc.createTextNode("Java"); e1.appendChild(n1);/*from www . ja v a 2s. c o m*/ Element e2 = xmldoc.createElement("URL"); Node n2 = xmldoc.createTextNode("http://www.server.com"); e2.appendChild(n2); e0.appendChild(e1); e0.appendChild(e2); root.appendChild(e0); Node pi = xmldoc.createProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"TODOs.xsl\""); xmldoc.insertBefore(pi, root); StreamResult out = new StreamResult("howto.xml"); DOMSource domSource = new DOMSource(xmldoc); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); 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"); transformer.transform(domSource, out); }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);//from w ww . j a v a2s.co m factory.setExpandEntityReferences(false); Document doc = factory.newDocumentBuilder().parse(new File("filename")); Element element = doc.getElementById("key1"); CDATASection cdataNode = doc.createCDATASection(""); Comment commentNode = doc.createComment(""); Text textNode = doc.createTextNode(""); // All three types of nodes implement the CharacterData interface CharacterData cdata = cdataNode; cdata = commentNode; cdata = textNode; // Delete text int offset = 0; int len = 5; cdata.deleteData(offset, len); }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);//ww w.jav a2 s.c om factory.setExpandEntityReferences(false); Document doc = factory.newDocumentBuilder().parse(new File("filename")); Element element = doc.getElementById("key1"); CDATASection cdataNode = doc.createCDATASection(""); Comment commentNode = doc.createComment(""); Text textNode = doc.createTextNode(""); // All three types of nodes implement the CharacterData interface CharacterData cdata = cdataNode; cdata = commentNode; cdata = textNode; cdata.setData("some data"); int len = cdata.getLength(); int offset = 5; len = 4; String s = cdata.substringData(offset, len); }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);//w ww . j a v a2 s. c om factory.setExpandEntityReferences(false); Document doc = factory.newDocumentBuilder().parse(new File("filename")); Element element = doc.getElementById("key1"); CDATASection cdataNode = doc.createCDATASection(""); Comment commentNode = doc.createComment(""); Text textNode = doc.createTextNode(""); // All three types of nodes implement the CharacterData interface CharacterData cdata = cdataNode; cdata = commentNode; cdata = textNode; // data int offset = 5; cdata.insertData(offset, "a "); cdata.appendData(" b"); }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);// w ww . j ava 2s . c om factory.setExpandEntityReferences(false); Document doc = factory.newDocumentBuilder().parse(new File("filename")); Element element = doc.getElementById("key1"); CDATASection cdataNode = doc.createCDATASection(""); Comment commentNode = doc.createComment(""); Text textNode = doc.createTextNode(""); // All three types of nodes implement the CharacterData interface CharacterData cdata = cdataNode; cdata = commentNode; cdata = textNode; // Replace text String replacement = "c"; int offset = 10; int len = 6; cdata.replaceData(offset, len, replacement); }
From source file:DOMGenerate.java
public static void main(String[] argv) { try {//from w w w.j av a 2 s . co 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 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 .j a v a 2s. c o m 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: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);//from w w w .j a v a2 s . c o m 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); }