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:Main.java

public static Element createParagraphElement(String name, String content, Document doc) {

    Element element = doc.createElement(name);

    BufferedReader br = new BufferedReader(new StringReader(content));

    try {//w ww.j a  va  2s.c o m
        while (br.ready()) {
            // Create an element for this node
            String p = br.readLine();

            // To avoid an infinite loop
            if (p == null)
                break;
            Element pe = doc.createElement("p");

            //Add text to paragraph node
            Text t = doc.createTextNode(p);
            pe.appendChild(t);
            element.appendChild(pe);
        }
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }

    return element;
}

From source file:Main.java

/**
 * Set the text of the specified element to the given string.
 * /* w w  w  .  ja va 2 s .c  o m*/
 * @param e The element.
 * @param text The text string.
 */
public static void setText(Element e, String text) {
    NodeList lst = e.getChildNodes();
    int size = lst.getLength();
    for (int i = 0; i < size; i++) {
        Node n = lst.item(i);
        if (n.getNodeType() == Node.TEXT_NODE) {
            Text t = (Text) n;
            t.setData(text.trim());
            return;
        }
    }
    Document doc = e.getOwnerDocument();
    // bit of a hack - we preserve the cdata on the way in so we can serialize correctly
    // This only works on xml to xml 
    // TODO need to have a "preserve format" or some such on the mdmi structure
    if (text.startsWith("<![CDATA[") && text.endsWith("]]>")) {
        CDATASection cdata = doc
                .createCDATASection(text != null ? text.substring(9, text.lastIndexOf("]]>")) : null);
        e.appendChild(cdata);
    } else {
        Text txt = doc.createTextNode(text != null ? text.trim() : null);
        e.appendChild(txt);
    }

}

From source file:Main.java

private static void makeNamelist(Document doc) {
    String names = null;/*from w ww . j  av a2s .  com*/
    Element root = doc.getDocumentElement();
    NodeList nameElements = root.getElementsByTagName("name");
    for (int i = 0; i < nameElements.getLength(); i++) {
        Element name = (Element) nameElements.item(i);
        Text nametext = (Text) name.getFirstChild();
        if (names == null) {
            names = nametext.getData();
        } else {
            names += ", " + nametext.getData();
        }
    }
    Element namelist = doc.createElement("names");
    Text namelisttext = doc.createTextNode(names);
    namelist.appendChild(namelisttext);
    root.insertBefore(namelist, root.getFirstChild());
}

From source file:Main.java

public static Element appendNewElement(Document document, Element parent, String element, Object content,
        String namespace) {/*from ww w .  j ava  2 s  . c o  m*/
    Element childElement;
    if (namespace != null) {
        childElement = document.createElementNS(namespace, element);
    } else {
        childElement = document.createElement(element);
    }

    if (content != null) {
        // TODO: We'll have that on Android 2.2:
        // childElement.setTextContent(content.toString());
        // Meanwhile:
        childElement.appendChild(document.createTextNode(content.toString()));
    }

    parent.appendChild(childElement);
    return childElement;
}

From source file:com.moviejukebox.tools.DOMHelper.java

/**
 * Append a child element to a parent element with a single attribute/value pair
 *
 * @param doc//  w w  w  .j  a  v a  2  s . c om
 * @param parentElement
 * @param elementName
 * @param elementValue
 * @param attribName
 * @param attribValue
 */
public static void appendChild(Document doc, Element parentElement, String elementName, String elementValue,
        String attribName, String attribValue) {
    Element child = doc.createElement(elementName);
    Text text = doc.createTextNode(elementValue);
    child.appendChild(text);
    child.setAttribute(attribName, attribValue);
    parentElement.appendChild(child);
}

From source file:com.moviejukebox.tools.DOMHelper.java

/**
 * Add a child element to a parent element with a set of attributes
 *
 * @param doc/*from   w  ww  .  j  a va2 s  .  c o m*/
 * @param parentElement
 * @param elementName
 * @param elementValue
 * @param childAttributes
 */
public static void appendChild(Document doc, Element parentElement, String elementName, String elementValue,
        Map<String, String> childAttributes) {
    Element child = doc.createElement(elementName);
    Text text = doc.createTextNode(elementValue);
    child.appendChild(text);

    if (childAttributes != null && !childAttributes.isEmpty()) {
        for (Map.Entry<String, String> attrib : childAttributes.entrySet()) {
            child.setAttribute(attrib.getKey(), attrib.getValue());
        }
    }

    parentElement.appendChild(child);
}

From source file:com.nridge.core.base.std.XMLUtl.java

public static void makeEscElemStrValue(Document aDocument, Element anElement, String aName, String aValue) {
    Element subElement;/*from   w  w  w  . j  a v a 2 s  .c  o  m*/

    if ((StringUtils.isNotEmpty(aName)) && (StringUtils.isNotEmpty(aValue))) {
        subElement = aDocument.createElement(aName);
        if (isEscapeNeeded(aValue))
            aValue = escapeElemStrValue(aValue);
        subElement.appendChild(aDocument.createTextNode(aValue));
        anElement.appendChild(subElement);
    }
}

From source file:com.nridge.core.base.std.XMLUtl.java

public static void makeElemBoolValue(Document aDocument, Element anElement, String aName, boolean aFlag) {
    String aValue;//from  ww w.j a  v  a2s.co m
    Element subElement;

    if (StringUtils.isNotEmpty(aName)) {
        if (aFlag)
            aValue = XML_UPPER_YES;
        else
            aValue = XML_UPPER_NO;
        subElement = aDocument.createElement(aName);
        subElement.appendChild(aDocument.createTextNode(aValue));
        anElement.appendChild(subElement);
    }
}

From source file:com.hphoto.server.ApiServlet.java

private static Element addNode(Document doc, Node parent, String name, String text) {
    Element child = doc.createElement(name);
    child.appendChild(doc.createTextNode(getLegalXml(text)));
    parent.appendChild(child);/*from  w w w . jav a 2s .  co m*/
    return child;
}

From source file:Main.java

private static void convert(Node toCopy, Node saveTo, Document doc) {
    Node newNode;//from  w ww.  j  ava  2 s.  c o  m
    switch (toCopy.getNodeType()) {
    case Node.ELEMENT_NODE:
        Element newElement = doc.createElementNS(toCopy.getNamespaceURI(), toCopy.getNodeName());
        newNode = newElement;
        Element baseElement = (Element) toCopy;
        NamedNodeMap children = baseElement.getAttributes();
        for (int i = 0; i < children.getLength(); i++) {
            convertAttribute((Attr) children.item(i), newElement, doc);
        }
        break;
    case Node.TEXT_NODE:
        newNode = doc.createTextNode(toCopy.getTextContent());
        break;
    default:
        newNode = null;
    }
    if (newNode != null) {
        NodeList children = toCopy.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            convert(children.item(i), newNode, doc);
        }

        saveTo.appendChild(newNode);
    }
}