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
@SuppressWarnings({ "ChainOfInstanceofChecks" }) private static void formatElementEnd(/*@Nonnull*/Document document, /*@Nonnull*/Element element, /*@Nonnull*/ String formatString) {//w w w.ja v a2 s. c om Node lastChild = element.getLastChild(); if (lastChild != null) { if (lastChild instanceof Element) { element.appendChild(document.createTextNode(formatString)); } else if (lastChild instanceof Text) { Text textNode = (Text) lastChild; String text = textNode.getWholeText(); if (hasOnlyTextSiblings(textNode)) { String trimmedText = text.trim(); if (!trimmedText.equals(text)) { textNode.replaceWholeText(trimmedText); } } else { if (!formatString.equals(text)) { textNode.replaceWholeText(trimRight(text) + formatString); } } } } }
From source file:XMLUtils.java
public static Text createTextNode(Document root, String data) { return root.createTextNode(data); }
From source file:Main.java
public static Element prependChildElement(Element parent, Element child, boolean addWhitespace, Document doc) { Node firstChild = parent.getFirstChild(); if (firstChild == null) { parent.appendChild(child);/*www. j a va2 s .c om*/ } else { parent.insertBefore(child, firstChild); } if (addWhitespace) { Node whitespaceText = doc.createTextNode("\n"); parent.insertBefore(whitespaceText, child); } return child; }
From source file:Main.java
/** * Convert Properties to string// ww w .j av a2 s. co m * * @param props * @return xml string * @throws IOException */ public static String writePropToString(Properties props) throws IOException { try { org.w3c.dom.Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); org.w3c.dom.Element conf = doc.createElement("configuration"); doc.appendChild(conf); conf.appendChild(doc.createTextNode("\n")); for (Enumeration e = props.keys(); e.hasMoreElements();) { String name = (String) e.nextElement(); Object object = props.get(name); String value; if (object instanceof String) { value = (String) object; } else { continue; } org.w3c.dom.Element propNode = doc.createElement("property"); conf.appendChild(propNode); org.w3c.dom.Element nameNode = doc.createElement("name"); nameNode.appendChild(doc.createTextNode(name.trim())); propNode.appendChild(nameNode); org.w3c.dom.Element valueNode = doc.createElement("value"); valueNode.appendChild(doc.createTextNode(value.trim())); propNode.appendChild(valueNode); conf.appendChild(doc.createTextNode("\n")); } Source source = new DOMSource(doc); StringWriter stringWriter = new StringWriter(); Result result = new StreamResult(stringWriter); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.transform(source, result); return stringWriter.getBuffer().toString(); } catch (Exception e) { throw new IOException(e); } }
From source file:Main.java
public static void encodeStringList(Document doc, Element parent, List<String> list, String namespaceURI, String qualifiedName) {/* w w w. ja va2 s. c o m*/ Element currElm; Text currVal; if (list != null) { for (String item : list) { currElm = doc.createElementNS(namespaceURI, qualifiedName); currVal = doc.createTextNode(item); currElm.appendChild(currVal); parent.appendChild(currElm); } } }
From source file:Main.java
public static Document createXMLResult(String rootName, Map<String, String> map) { try {/* ww w .j ava 2 s.co m*/ DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder; docBuilder = dbfac.newDocumentBuilder(); Document doc = docBuilder.newDocument(); Element root = doc.createElement(rootName); doc.appendChild(root); for (String elementName : map.keySet()) { Element child = doc.createElement(elementName); Text text = doc.createTextNode(map.get(elementName)); child.appendChild(text); root.appendChild(child); } return doc; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
From source file:Main.java
/** * returns XML representation of a string, with specified XML node name * //from w w w .j a v a 2s .com * @param doc XML Document in which to build the XML element * @param namespaceURI the namespace URI * @param qname top level qualified XML node name in XML representation * @param str the string * @return XML representation of the string, with specified XML node name **/ public final static Element strToXML(final Document doc, final String namespaceURI, final String qname, final String str) { if (str == null /*|| str.length() == 0*/) { return null; } final Element xml = doc.createElementNS(namespaceURI, qname); xml.appendChild(doc.createTextNode(str)); return xml; }
From source file:DOMEdit.java
public static void insert(Document doc, String name, String phone, String email) { Element personNode = doc.createElement("person"); Element nameNode = doc.createElement("name"); personNode.appendChild(nameNode);/*from w ww . ja v a2 s .co m*/ Text nametextNode = doc.createTextNode(name); nameNode.appendChild(nametextNode); Element phoneNode = doc.createElement("phone"); personNode.appendChild(phoneNode); Text phonetextNode = doc.createTextNode(phone); phoneNode.appendChild(phonetextNode); Element emailNode = doc.createElement("email"); personNode.appendChild(emailNode); Text emailtextNode = doc.createTextNode(email); emailNode.appendChild(emailtextNode); Element root = doc.getDocumentElement(); Node firstChildNode = root.getFirstChild(); root.insertBefore(personNode, firstChildNode); }
From source file:Main.java
/** * Set the value of the first tag found with the given name in the given document<br/>. If the tag with the * requested name doesn't have a child as textnode with the tagValue is created. * * @param doc/*from w w w. java 2s . c o m*/ * @param tagName * @param tagValue */ public static void setTagValue(Document doc, String tagName, String tagValue) { NodeList tagList = doc.getElementsByTagName(tagName); if (tagList.getLength() > 0) { if (tagList.item(0).getFirstChild() != null) { tagList.item(0).getFirstChild().setNodeValue(tagValue); } else { tagList.item(0).appendChild(doc.createTextNode(tagValue)); } } }
From source file:Main.java
/** * This method is used to insert a new tag below the tag specified by * <code>appendTo</code> parameter. * /*from w w w . j a va 2 s . co m*/ * @param d * the <code>Document</code> object to which a new tag is to be * inserted. * @param appendTo * the tag below which a new tag needs to be inserted. * @param tagName * the name of new tag * @param tagValue * the value of new tag */ public static Element insertNewTagBelow(Document d, String appendTo, String tagName, String tagValue) { Node element = d.getElementsByTagName(appendTo).item(0); if (element == null) { element = d.createElement(appendTo); } Element newElement = d.createElement(tagName); element.appendChild(newElement); newElement.appendChild(d.createTextNode(tagValue)); return newElement; }