Example usage for org.w3c.dom Document createTextNode

List of usage examples for org.w3c.dom Document createTextNode

Introduction

In this page you can find the example usage for org.w3c.dom Document createTextNode.

Prototype

public Text createTextNode(String data);

Source Link

Document

Creates a Text node given the specified string.

Usage

From source file:net.sf.jabref.exporter.OpenDocumentRepresentation.java

private void addTableCell(Document doc, Element parent, String content) {
    Element cell = doc.createElement("table:table-cell");
    Element text = doc.createElement("text:p");
    Text textNode = doc.createTextNode(content);
    text.appendChild(textNode);/*from ww  w. j a  va  2s.  co m*/
    //text.setTextContent(content);
    cell.appendChild(text);
    parent.appendChild(cell);
}

From source file:com.zacwolf.commons.wbxcon.WBXCONorg.java

/**
 * This is a helper method that correctly handles setting the text content
 * for a given tag in a DOM <code>Document</code>
 * /*from  ww w .java 2  s .c  o m*/
 * @param tagName   DOM tag to create and add to the parent node.
 * @param value      The text value to be assigned to the tag
 * @param parent   The parent Node that the tag/text will be added to
 */
final static void documentSetTextContentOfNode(final String tagName, final String value, final Node parent) {
    final Document dom = parent.getOwnerDocument();
    final Node node = dom.createElement(tagName);
    final Text nodeVal = dom.createTextNode(value);
    node.appendChild(nodeVal);
    parent.appendChild(node);
}

From source file:de.juwimm.cms.util.SmallSiteConfigReader.java

public void saveValue(String xmlPath, String value) {
    String[] pathComponents = xmlPath.split("/");
    String itemTag = pathComponents[pathComponents.length - 1];
    Node parentNode = buildXmlPathToLeaf(xmlPath);
    if (parentNode == null) {
        return;//from   w w w . j a va  2  s .c o m
    }

    if (value == null) {
        return;
    }

    //delete old value
    try {
        Node oldNode = XercesHelper.findNode(parentNode, "//" + itemTag);
        parentNode.removeChild(oldNode);
    } catch (Exception e) {
        if (log.isDebugEnabled()) {
            log.debug("SmalSiteConfigReader: old value at path " + xmlPath + " does not exist");
        }
    }
    //create new value
    Document document = this.getConfdoc();
    Node valueNode = document.createElement(itemTag);
    valueNode.appendChild(document.createTextNode(value));
    parentNode.appendChild(valueNode);

}

From source file:com.viettel.ws.client.JDBCUtil.java

/**
 * Create document using DOM api/*from  ww  w  .ja v a  2  s  . co  m*/
 *
 * @param rs a result set
 * @param doc a input document for append content
 * @param rsName name of the appended element
 * @return a document after append content
 * @throws ParserConfigurationException If error when parse XML string
 * @throws SQLException If error when read data from database
 */
public static Document add2Document1(ResultSet rs1, ResultSet rs2, Document doc, String rsName)
        throws ParserConfigurationException, SQLException {

    if (rs1 == null && rs2 == null) {
        return doc;
    }

    //Get root element
    Element root = doc.getDocumentElement();

    Element rsElement = doc.createElement(rsName);
    root.appendChild(rsElement);

    if (rs1 != null) {
        ResultSetMetaData rsmd = rs1.getMetaData();
        int colCount = rsmd.getColumnCount();

        while (rs1.next()) {
            Element row = doc.createElement("Row");
            rsElement.appendChild(row);
            try {
                for (int i = 1; i <= colCount; i++) {
                    String columnName = rsmd.getColumnName(i);
                    Object value = rs1.getObject(i);
                    if (value == null) {
                        value = "";
                    }

                    Element node = doc.createElement(columnName);
                    node.appendChild(doc.createTextNode(value.toString()));
                    row.appendChild(node);
                }
            } catch (Exception ex) {
                LogUtil.addLog(ex);//binhnt sonar a160901
                //                    logger.error(e, e);
            }
        }
    }

    if (rs2 != null) {
        ResultSetMetaData rsmd = rs2.getMetaData();
        int colCount = rsmd.getColumnCount();

        while (rs2.next()) {
            Element row = doc.createElement("Row");
            rsElement.appendChild(row);
            try {
                for (int i = 1; i <= colCount; i++) {
                    String columnName = rsmd.getColumnName(i);
                    Object value = rs2.getObject(i);
                    if (value == null) {
                        value = "";
                    }

                    Element node = doc.createElement(columnName);
                    node.appendChild(doc.createTextNode(value.toString()));
                    row.appendChild(node);
                }
            } catch (Exception ex) {
                LogUtil.addLog(ex);//binhnt sonar a160901
                //                    logger.error(e, e);
            }
        }
    }
    return doc;

}

From source file:org.callimachusproject.xproc.Pipeline.java

private void appendText(InputStreamReader reader, Document doc, Element data) throws IOException {
    char buf[] = new char[bufSize];
    int len = reader.read(buf, 0, bufSize);
    while (len >= 0) {
        data.appendChild(doc.createTextNode(new String(buf, 0, len)));
        len = reader.read(buf, 0, bufSize);
    }//from  w  ww .  ja v a2  s  .  c o m
}

From source file:net.bpelunit.util.XMLUtilTest.java

@Test
public void testGetContentsOfTextOnlyNodeOnlyTextNodes() throws Exception {
    Document doc = XMLUtil.createDocument();

    Element e = doc.createElement("a");
    doc.appendChild(e);//from   w w w.ja  v a2  s .co  m

    e.appendChild(doc.createTextNode("a"));
    e.appendChild(doc.createTextNode("b"));
    e.appendChild(doc.createTextNode("c"));

    assertEquals("abc", XMLUtil.getContentsOfTextOnlyNode(e));
}

From source file:net.bpelunit.util.XMLUtilTest.java

@Test
public void testGetContentsOfTextOnlyNodeNotOnlyTextNodes() throws Exception {
    Document doc = XMLUtil.createDocument();

    Element e = doc.createElement("a");
    doc.appendChild(e);/* ww  w. j  a  v  a 2  s.co m*/

    e.appendChild(doc.createTextNode("a"));
    e.appendChild(doc.createElement("b"));
    e.appendChild(doc.createTextNode("c"));

    assertNull(XMLUtil.getContentsOfTextOnlyNode(e));
}

From source file:org.joy.config.Configuration.java

/**
 * Convenience function to set the text of a child element.
 *//* w w w  .  jav  a2s  .  c om*/
private void setTextChild(Element elem, String name, String value) {
    Document doc = elem.getOwnerDocument();
    Element e = doc.createElement(name);
    e.appendChild(doc.createTextNode(value));
    elem.appendChild(e);
}

From source file:de.juwimm.cms.util.SmallSiteConfigReader.java

public void saveValues(String xmlPath, List<String> values) {
    String[] pathComponents = xmlPath.split("/");
    String itemTag = pathComponents[pathComponents.length - 1];
    Node parentNode = buildXmlPathToLeaf(xmlPath);

    if (parentNode == null) {
        return;/*from  w w w  . j av a2  s  .  c om*/
    }
    //remove old values
    int oldLength = parentNode.getChildNodes().getLength();
    for (int i = 0; i < oldLength; i++) {
        parentNode.removeChild(parentNode.getChildNodes().item(0));
    }

    if (values == null || values.size() == 0) {
        return;
    }

    //save new values
    Document document = this.getConfdoc();
    for (String value : values) {
        Node valueNode = document.createElement(itemTag);
        valueNode.appendChild(document.createTextNode(value));
        parentNode.appendChild(valueNode);
    }
}

From source file:com.msopentech.odatajclient.engine.data.atom.AtomSerializer.java

private static Element entry(final AtomEntry entry) throws ParserConfigurationException {
    final DocumentBuilder builder = ODataConstants.DOC_BUILDER_FACTORY.newDocumentBuilder();
    final Document doc = builder.newDocument();

    final Element entryElem = doc.createElement(ODataConstants.ATOM_ELEM_ENTRY);
    entryElem.setAttribute(XMLConstants.XMLNS_ATTRIBUTE, ODataConstants.NS_ATOM);
    entryElem.setAttribute(ODataConstants.XMLNS_METADATA, ODataConstants.NS_METADATA);
    entryElem.setAttribute(ODataConstants.XMLNS_DATASERVICES, ODataConstants.NS_DATASERVICES);
    entryElem.setAttribute(ODataConstants.XMLNS_GML, ODataConstants.NS_GML);
    entryElem.setAttribute(ODataConstants.XMLNS_GEORSS, ODataConstants.NS_GEORSS);
    if (entry.getBaseURI() != null) {
        entryElem.setAttribute(ODataConstants.ATTR_XMLBASE, entry.getBaseURI().toASCIIString());
    }//w  ww.j a v  a  2  s .  c  o m
    doc.appendChild(entryElem);

    final Element category = doc.createElement(ODataConstants.ATOM_ELEM_CATEGORY);
    category.setAttribute(ODataConstants.ATOM_ATTR_TERM, entry.getType());
    category.setAttribute(ODataConstants.ATOM_ATTR_SCHEME, ODataConstants.ATOM_CATEGORY_SCHEME);
    entryElem.appendChild(category);

    if (StringUtils.isNotBlank(entry.getTitle())) {
        final Element title = doc.createElement(ODataConstants.ATOM_ELEM_TITLE);
        title.appendChild(doc.createTextNode(entry.getTitle()));
        entryElem.appendChild(title);
    }

    if (StringUtils.isNotBlank(entry.getSummary())) {
        final Element summary = doc.createElement(ODataConstants.ATOM_ELEM_SUMMARY);
        summary.appendChild(doc.createTextNode(entry.getSummary()));
        entryElem.appendChild(summary);
    }

    setLinks(entryElem, entry.getAssociationLinks());
    setLinks(entryElem, entry.getNavigationLinks());
    setLinks(entryElem, entry.getMediaEditLinks());

    final Element content = doc.createElement(ODataConstants.ATOM_ELEM_CONTENT);
    if (entry.isMediaEntry()) {
        if (StringUtils.isNotBlank(entry.getMediaContentType())) {
            content.setAttribute(ODataConstants.ATTR_TYPE, entry.getMediaContentType());
        }
        if (StringUtils.isNotBlank(entry.getMediaContentSource())) {
            content.setAttribute(ODataConstants.ATOM_ATTR_SRC, entry.getMediaContentSource());
        }
        if (content.getAttributes().getLength() > 0) {
            entryElem.appendChild(content);
        }

        if (entry.getMediaEntryProperties() != null) {
            entryElem.appendChild(doc.importNode(entry.getMediaEntryProperties(), true));
        }
    } else {
        content.setAttribute(ODataConstants.ATTR_TYPE, ContentType.APPLICATION_XML.getMimeType());
        if (entry.getContent() != null) {
            content.appendChild(doc.importNode(entry.getContent(), true));
        }
        entryElem.appendChild(content);
    }

    return entryElem;
}