List of usage examples for org.w3c.dom Element setTextContent
public void setTextContent(String textContent) throws DOMException;
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);// ww w . ja v a2s . c o m 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 { 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);// ww w . j a va2 s. co 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 sendXml(String result, int operationN) throws Exception { String filepath = "file.xml"; DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); Document doc = docBuilder.parse(filepath); Node tonode = doc.getElementsByTagName("command").item(0); Element res = doc.createElement("result"); res.setTextContent(result); tonode.appendChild(res);// w w w. j a v a 2 s .c o m TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result1 = new StreamResult(new File("file2.xml")); StreamResult result3 = new StreamResult(System.out); transformer.transform(source, result1); }
From source file:Main.java
private static void addTextContent(Document doc, Element root, String param, String value) { Element ele = doc.createElement(param); ele.setTextContent(value); root.appendChild(ele);/*from w ww. j ava2s . c o m*/ }
From source file:Main.java
/** * Set or remove text for an element.//w w w. ja v a 2s.c om * @param element * @param text the text to set or <code>null</code> to remove */ public static void setText(Element element, String text) { element.setTextContent(text); }
From source file:Main.java
public static Document AppendContentToEnd(Document doc, String tag, String content) { if (tag == null || tag == "" || content == null || content == "") return doc; Element e = doc.createElement(tag); e.setTextContent(content); Node lastone = doc.getLastChild(); lastone.appendChild(e);//ww w . ja v a2 s . c o m return doc; }
From source file:Main.java
public static void addSimpleOutput(Element success, Document outDoc, String tagName, String value) { Element output = (Element) success.getElementsByTagName("output").item(0); Element outItem = outDoc.createElement(tagName); outItem.setTextContent(value); output.appendChild(outItem);/*from www. java2 s.c o m*/ }
From source file:Main.java
public static Element addElementAndSetContent(Document doc, String name, Element parent, String text) { Element ee = addElement(doc, name, parent); ee.setTextContent(text); return ee;/*from ww w . ja va2s . c o m*/ }
From source file:Main.java
public static void createElement(Element parent, String elementName, String text) { Document doc = parent.getOwnerDocument(); Element id = doc.createElement(elementName); id.setTextContent(text); parent.appendChild(id);//w w w.ja v a2 s .c o m }
From source file:Main.java
private static Element createElement(Document document, String name, String textContent) { Element element = document.createElement(name); element.setTextContent(textContent); return element; }