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 Element createElement(Document document, String name, String value) { if (value != null) { Element element = (Element) document.createElement(name); element.appendChild(document.createTextNode(value)); return element; }// w w w. j a v a 2s . c om throw new IllegalArgumentException( "XMLDocumentUtils.createElement: value of " + name + " element can't be null."); }
From source file:Main.java
/** * For compatibility reasons the following is required: * If the value of a text node is to be changed, but a CDATA section with this name * already exists, the CDATA section is removed an a text node is created or changed. * * If the value of a CDATA section is to be changed, but a text node with this name * already exists, the text node is removed an a CDATA section is created or changed. * *///from w ww .jav a 2 s . com public static void setElementText(Element e, String newValue, boolean cdata) { if (e == null) { return; } Node node = null; NodeList children = e.getChildNodes(); if (children != null) { Node childToRemove = null; boolean changed = false; int listLength = children.getLength(); for (int i = 0; i < listLength; i++) { node = children.item(i); int nodeType = node.getNodeType(); if (nodeType == Node.TEXT_NODE) { if (cdata) { childToRemove = node; } else { node.setNodeValue(newValue); changed = true; } } if (nodeType == Node.CDATA_SECTION_NODE) { if (!cdata) { childToRemove = node; } else { node.setNodeValue(newValue); changed = true; } } } if (childToRemove != null) { // System.out.println("removing child " + childToRemove.getNodeValue()); childToRemove.setNodeValue(""); e.removeChild(childToRemove); } if (changed) { return; } } Document doc = e.getOwnerDocument(); if (cdata) { node = doc.createCDATASection(newValue); } else { node = doc.createTextNode(newValue); } e.appendChild(node); }
From source file:Main.java
public static Element createElmWithText(Document doc, String tagName, String text) { Element elm = doc.createElement(tagName); text = text != null ? text.trim() : ""; Node node = (text.indexOf('<') >= 0 || text.indexOf('\n') >= 0) ? doc.createCDATASection(text) : doc.createTextNode(text); elm.appendChild(node);// w w w . ja v a2s . co m return elm; }
From source file:Main.java
@SuppressWarnings({ "ChainOfInstanceofChecks" }) private static void formatElementStart(/*@Nonnull*/Document document, /*@Nonnull*/Element element, /*@Nonnull*/ String formatString) {//from ww w .ja v a2s .c o m Node previousSibling = element.getPreviousSibling(); if (previousSibling == null || previousSibling instanceof Element) { element.getParentNode().insertBefore(document.createTextNode(formatString), element); } else if (previousSibling instanceof Text) { Text textNode = (Text) previousSibling; String text = textNode.getWholeText(); if (!formatString.equals(text)) { textNode.replaceWholeText(trimRight(text) + formatString); } } }
From source file:Main.java
public static Element createElement(Document document, String nsURI, String name, String value) { if (value != null) { Element element = (Element) document.createElementNS(nsURI, name); element.appendChild(document.createTextNode(value != null ? value : "")); return element; }// w w w . ja v a 2 s . c om throw new IllegalArgumentException( "XMLDocumentUtils.createElement: value of " + name + " element can't be null."); }
From source file:Main.java
public static Document createXMLResult(String rootName, Map<String, String> map, Document d) { try {//from www.j ava 2s . c o m Element r = d.createElement(rootName); d.appendChild(r); for (String elementName : map.keySet()) { Element eltName = d.createElement(elementName); eltName.appendChild(d.createTextNode(map.get(elementName))); r.appendChild(eltName); } d.normalizeDocument(); return d; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
From source file:Utils.java
public static Element findElementElseCreateAndSetAndAttribute(Document document, Element parent, String element, String value, String attributeName, String attributeValue) { Element e = findElementElseCreateAndAttribute(document, parent, element, attributeName, attributeValue); if (e != null) e.appendChild(document.createTextNode(value)); return e;// w w w .j ava 2 s .c o m }
From source file:Main.java
public static Node addChildNode(Node pNode, String name, String value) { Document _doc = null; if (pNode.getNodeType() == Node.DOCUMENT_NODE) { _doc = (Document) pNode;//from w ww . j a v a 2 s .c o m } else { _doc = pNode.getOwnerDocument(); } Element _ele = _doc.createElement(name); Node _node = _doc.createTextNode(value); _ele.appendChild(_node); return pNode.appendChild(_ele); }
From source file:Utils.java
public static Element findContainerWithAttributeValueElseCreateAndSet(Document document, Element parent, String element, String value, String attributeName, String attributeValue) { Element e = findContainerWithAttributeValueElseCreate(document, parent, element, attributeName, attributeValue);//from w w w . ja v a 2s.c o m e.appendChild(document.createTextNode(value)); return e; }
From source file:Main.java
public static void createXML222(String xmlFile, String xpath, String element, String data) { try {//from w ww . jav a2s . c o m DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.newDocument(); Element rootElement = document.createElement(xpath); document.appendChild(rootElement); for (int i = 1; i <= 1; i++) { Element em = document.createElement(element); em.appendChild(document.createTextNode(data)); rootElement.appendChild(em); } TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(document); FileOutputStream fo = new FileOutputStream(xmlFile); StreamResult result = new StreamResult(fo); transformer.transform(source, result); } catch (Exception ex) { ex.printStackTrace(); } }