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:edu.ur.ir.ir_export.service.DefaultContributorTypeExportService.java

/**
 * Add the name element./*w  w  w. ja  va 2s.c o m*/
 * 
 * @param parent - parent element to add the name to
 * @param name - name value to add
 * @param doc - document element
 */
private void addSystemCode(Element parent, String systemCode, Document doc) {
    Element systemCodeVal = doc.createElement("system_code");
    Text data = doc.createTextNode(systemCode);
    systemCodeVal.appendChild(data);
    parent.appendChild(systemCodeVal);
}

From source file:main.java.vasolsim.common.file.__NONUSEDREFERENCE_VaSOLSimExam.java

protected static void appendSubNode(String elementName, String nodeData, Element parentElement, Document doc) {
    Element subElement = doc.createElement(elementName);
    subElement.appendChild(doc.createTextNode(nodeData));
    parentElement.appendChild(subElement);
}

From source file:org.openmrs.module.atomfeed.AtomFeedUtil.java

/**
 * Updates content of atom feed header file by re-creating new xml header block and writing it
 * into given file. Actually, if given atom feed header file does not exists, it creates it.
 * Otherwise, it changes values of "updated", "versionId" and "entriesSize" elements within
 * header xml tree./*www.ja v a  2 s  . co  m*/
 * 
 * @param atomfeedheader the file target to be updated
 * @param entriesSize the size in bytes of entries payload, which is related to given feed
 *            header
 */
private static void updateFeedFileHeader(File atomfeedheader, long entriesSize) {
    try {
        // We need a Document
        DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
        Document doc = docBuilder.newDocument();

        // //////////////////////
        // Creating the XML tree

        // create the root element and add it to the document
        Element root = doc.createElement("feed");
        root.setAttribute("xmlns", "http://www.w3.org/2005/Atom");
        doc.appendChild(root);

        // create title element, add its text, and add to root
        Element title = doc.createElement("title");
        Text titleText = doc.createTextNode(AtomFeedConstants.ATOM_FEED_TITLE);
        title.appendChild(titleText);
        root.appendChild(title);

        // create title element, add its attrs, and add to root
        Element selflink = doc.createElement("link");
        selflink.setAttribute("href", AtomFeedUtil.getFeedUrl());
        selflink.setAttribute("rel", "self");
        root.appendChild(selflink);

        Element serverlink = doc.createElement("link");
        serverlink.setAttribute("href", getWebServiceUrl());
        root.appendChild(serverlink);

        // create title element, add its text, and add to root
        Element id = doc.createElement("id");
        Text idText = doc.createTextNode(AtomFeedConstants.ATOM_FEED_ID);
        id.appendChild(idText);
        root.appendChild(id);

        // create updated element, add its text, and add to root
        Element updated = doc.createElement("updated");
        Date lastModified = new Date();
        Text updatedText = doc.createTextNode(dateToRFC3339(lastModified));
        updated.appendChild(updatedText);
        root.appendChild(updated);

        // create versionId element, add its text, and add to root
        Element versionId = doc.createElement("versionId");
        Text versionIdText = doc.createTextNode(String.valueOf(lastModified.getTime()));
        versionId.appendChild(versionIdText);
        root.appendChild(versionId);

        // create versionId element, add its text, and add to root
        Element entriesSizeElement = doc.createElement("entriesSize");
        Text entriesSizeText = doc.createTextNode(String.valueOf(entriesSize));
        entriesSizeElement.appendChild(entriesSizeText);
        root.appendChild(entriesSizeElement);

        /*
         * Print the xml to the file
         */

        // set up a transformer
        TransformerFactory transfac = TransformerFactory.newInstance();
        Transformer trans = transfac.newTransformer();
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        trans.setOutputProperty(OutputKeys.INDENT, "no");

        // create string from xml tree
        FileWriter fw = new FileWriter(atomfeedheader);
        StreamResult result = new StreamResult(fw);
        DOMSource source = new DOMSource(doc);
        trans.transform(source, result);

        // print xml for debugging purposes
        if (log.isTraceEnabled()) {
            StringWriter sw = new StringWriter();
            result = new StreamResult(sw);
            trans.transform(source, result);
            log.trace("Here's the initial xml:\n\n" + sw.toString());
        }

    } catch (Exception e) {
        log.error("unable to initialize feed at: " + atomfeedheader.getAbsolutePath(), e);
    }
}

From source file:org.jolokia.roo.JolokiaCommands.java

private void addLineBreak(Node pRootElement, Node pBeforeThis, Document pWebXmlDoc) {
    pRootElement.insertBefore(pWebXmlDoc.createTextNode("\n    "), pBeforeThis);
    pRootElement.insertBefore(pWebXmlDoc.createTextNode("\n    "), pBeforeThis);
}

From source file:de.erdesignerng.model.serializer.CommonAbstractXMLSerializer.java

protected void serializeCommentElement(Document aDocument, Element aElement, ModelItem aItem) {
    if (!StringUtils.isEmpty(aItem.getComment())) {
        Element theCommentElement = aDocument.createElement(COMMENT);
        theCommentElement.appendChild(aDocument.createTextNode(aItem.getComment()));

        aElement.appendChild(theCommentElement);
    }//w w w  .j a  v a 2s. c  om
}

From source file:BuildXml.java

public Node createContactNode(Document document) {

    // create FirstName and LastName elements
    Element firstName = document.createElement("FirstName");
    Element lastName = document.createElement("LastName");

    firstName.appendChild(document.createTextNode("First Name"));
    lastName.appendChild(document.createTextNode("Last Name"));

    // create contact element
    Element contact = document.createElement("contact");

    // create attribute
    Attr genderAttribute = document.createAttribute("gender");
    genderAttribute.setValue("F");

    // append attribute to contact element
    contact.setAttributeNode(genderAttribute);
    contact.appendChild(firstName);//w ww  . j a  v  a 2s  .c  om
    contact.appendChild(lastName);

    return contact;
}

From source file:channellistmaker.channelfilemaker.ChannelDocumentMaker.java

/**
 * ?????? {@literal <element_name>text_value</element_name>}
 *
 * @paramdocument ??XML/*from w  ww .  ja v  a  2s. co  m*/
 * @paramparent ??????
 * @paramelementName ???
 * @paramtextValue ??
 */
private void addTextElement(Document document, Element parent, String elementName, String textValue) {
    final Element e = document.createElement(elementName);
    parent.appendChild(e);
    final Text textMessage = document.createTextNode(textValue);
    e.appendChild(textMessage);
}

From source file:com.netspective.sparx.form.DialogContext.java

static public void exportParamToXml(Element parent, String name, String[] values) {
    Document doc = parent.getOwnerDocument();
    Element fieldElem = doc.createElement("request-param");
    fieldElem.setAttribute("name", name);
    if (values != null && values.length > 1) {
        fieldElem.setAttribute("value-type", "strings");
        Element valuesElem = doc.createElement("values");
        for (int i = 0; i < values.length; i++) {
            Element valueElem = doc.createElement("value");
            valueElem.appendChild(doc.createTextNode(values[i]));
            valuesElem.appendChild(valueElem);
        }//w w w . java 2 s  .co m
        fieldElem.appendChild(valuesElem);
        parent.appendChild(fieldElem);
    } else if (values != null) {
        fieldElem.setAttribute("value-type", "string");
        Element valueElem = doc.createElement("value");
        valueElem.appendChild(doc.createTextNode(values[0]));
        fieldElem.appendChild(valueElem);
        parent.appendChild(fieldElem);
    }
}

From source file:Main.java

/**
 * Clones the given DOM node into the given DOM document.
 * //from   w w w.java2s  .  co  m
 * @param node
 *            The DOM node to clone.
 * @param doc
 *            The target DOM document.
 * @return The cloned node in the target DOM document.
 */
public static Node cloneNode(Node node, Document doc) {
    Node clone = null;
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
        clone = doc.createElement(node.getNodeName());
        NamedNodeMap attrs = node.getAttributes();
        for (int i = 0; i < attrs.getLength(); i++) {
            Node attrNode = attrs.item(i);
            Attr attrClone = doc.createAttribute(attrNode.getNodeName());
            attrClone.setNodeValue(attrNode.getNodeValue());
            ((Element) clone).setAttributeNode(attrClone);
        }

        // Iterate through each child nodes.
        NodeList childNodes = node.getChildNodes();
        if (childNodes != null) {
            for (int i = 0; i < childNodes.getLength(); i++) {
                Node childNode = childNodes.item(i);
                if (childNode == null) {
                    continue;
                }
                Node childClone = cloneNode(childNode, doc);
                if (childClone == null) {
                    continue;
                }
                clone.appendChild(childClone);
            }
        }
        break;

    case Node.TEXT_NODE:
    case Node.CDATA_SECTION_NODE:
        clone = doc.createTextNode(node.getNodeName());
        clone.setNodeValue(node.getNodeValue());
        break;
    }
    return clone;
}

From source file:de.codesourcery.eve.skills.calendar.impl.PlaintextPayloadType.java

@Override
public void storePayload(Document document, Element parent, ICalendarEntry entry) {
    final PlaintextPayload payload = (PlaintextPayload) entry.getPayload();

    final Element node = document.createElement("plaintext");
    parent.appendChild(node);/*  ww w .j a  va 2s.  c  o  m*/

    node.setAttribute("summary", payload.getSummary());
    node.appendChild(document.createTextNode(payload.getNotes()));
}