Example usage for org.w3c.dom Element appendChild

List of usage examples for org.w3c.dom Element appendChild

Introduction

In this page you can find the example usage for org.w3c.dom Element appendChild.

Prototype

public Node appendChild(Node newChild) throws DOMException;

Source Link

Document

Adds the node newChild to the end of the list of children of this node.

Usage

From source file:com.aurel.track.exchange.docx.exporter.CustomXML.java

/**
 * Add watcher nodes to xml// w  ww. j a v a2  s. c om
 * @param watcherPersonBeans
 * @param root
 * @param dom
 * @param commentsName
 * @param watcherElement
 */
private static void addCommentNodes(List<HistoryValues> comments, Element root, Document dom, Locale locale) {
    if (comments == null || comments.isEmpty()) {
        appendChild(root, CUSTOM_XML_ELEMENTS.COMMENTS + CUSTOM_XML_ELEMENTS.PRESENT_SUFFIX,
                Boolean.FALSE.toString(), dom);
    } else {
        Element consultedList = dom.createElement(CUSTOM_XML_ELEMENTS.COMMENTS);
        for (HistoryValues historyValue : comments) {
            String commentText = historyValue.getNewShowValue();
            commentText = StringEscapeUtils.escapeHtml3(commentText);
            Element commentTextElement = createDomElement(CUSTOM_XML_ELEMENTS.COMMENT_TEXT, commentText, dom);
            Element commentedByElement = createDomElement(CUSTOM_XML_ELEMENTS.COMMENTED_BY,
                    historyValue.getChangedByName(), dom);
            Element commentedAtElement = createDomElement(CUSTOM_XML_ELEMENTS.COMMENTED_AT,
                    DateTimeUtils.getInstance().formatGUIDate(historyValue.getLastEdit(), locale), dom);
            Element commentElement = createDomElement(CUSTOM_XML_ELEMENTS.COMMENT, "", dom);
            commentElement.appendChild(commentTextElement);
            commentElement.appendChild(commentedByElement);
            commentElement.appendChild(commentedAtElement);
            consultedList.appendChild(commentElement);
        }
        root.appendChild(consultedList);
        appendChild(root, CUSTOM_XML_ELEMENTS.COMMENTS + CUSTOM_XML_ELEMENTS.PRESENT_SUFFIX,
                Boolean.TRUE.toString(), dom);
    }
}

From source file:Main.java

/**
 * Convenience function for creating elements.
 * //from  www. jav  a  2  s .c om
 * @param document
 *            the DOM document we're creating the element in
 * @param name
 *            the tagname for the element
 * @param attributes
 *            a map from attribute names to attributes, or <CODE>null</CODE>
 *            if this element should have no attributes
 * @param text
 *            the text for the element, which will be made into a text node
 *            and added as a child of the created element, or <CODE>null</CODE>
 *            if the element should have no children
 * @return a new element
 */
public static Element createElement(Document doc, String name, Object text, Map<String, Object> attributes) {
    Element e = doc.createElement(name);
    // Set the attributes.
    if (attributes != null) {
        for (Entry<String, Object> entry : attributes.entrySet()) {
            e.setAttribute(entry.getKey(), entry.getValue().toString());
        }
    }
    // Add the text element.
    if (text != null)
        e.appendChild(createTextNode(doc, text.toString()));
    return e;
}

From source file:Main.java

/**
 * Create a child of the given node at the given index.
 * /*from www .  j  a  v  a2  s.  co  m*/
 * @param doc
 *            a document
 * @param element
 *            an element
 * @param index
 *            an index
 * @param nodeName
 *            a node name
 * @return org.w3c.dom.Element
 */
public static Element createChildElement(Document doc, Element element, int index, String nodeName) {
    Element element2 = doc.createElement(nodeName);
    try {
        NodeList childList = element.getElementsByTagName(nodeName);
        Node child = childList.item(index);
        element.insertBefore(element2, child);
    } catch (Exception e) {
        element.appendChild(element2);
    }
    return element2;
}

From source file:no.kantega.commons.util.XMLHelper.java

public static Element setChildText(Document doc, Element parent, String name, String value) {
    Element child = getChildByName(parent, name);
    if (child == null) {
        child = doc.createElement(name);
        child.appendChild(doc.createTextNode(value == null ? "" : value));
        parent.appendChild(child);//from w  w  w. ja  va2  s. c o m
    } else {
        Node text = child.getFirstChild();
        if (text != null) {
            text.setNodeValue(value);
        } else {
            child.appendChild(doc.createTextNode(value == null ? "" : value));
        }
    }
    return child;
}

From source file:Main.java

public static void setText(Element element, String Text) {
    Node node;//from  w ww.j  ava2 s .c  om
    node = element.getFirstChild();
    while (null != node) {
        if (Node.TEXT_NODE == node.getNodeType()) {
            Text text = (Text) node;
            text.setData(Text);
            return;
        }
    }
    Text text = element.getOwnerDocument().createTextNode(Text);
    element.appendChild(text);
}

From source file:com.bluexml.side.Integration.alfresco.xforms.webscript.XmlBuilder.java

private static Element buildAttribute(Document doc, String attributeName, String attributeValue) {
    Element attribute = doc.createElement(ENTRY_ATTRIBUTE_NODE);
    attribute.setAttribute(ENTRY_QUALIFIEDNAME, attributeName);
    Element value = doc.createElement(ENTRY_ATTRIBUTE_VALUE);
    value.setTextContent(attributeValue);
    attribute.appendChild(value);
    return attribute;
}

From source file:Main.java

/**
 * Set the value of element//from ww w.  ja va2  s  .  c  o m
 *
 * @param element
 * @param val
 */
public static void setElementValue(Element element, String val) {
    Node node = element.getOwnerDocument().createTextNode(val);
    NodeList nl = element.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node nd = nl.item(i);
        if (nd.getNodeType() == Node.TEXT_NODE) {
            nd.setNodeValue(val);
            return;
        }
    }
    element.appendChild(node);
}

From source file:Main.java

/**
 * Method to delete old elements and add new elements in target file based on tag name of source file.
 *
 * @param source/*  ww w  . ja  va 2 s. co m*/
 * @param target
 * @param sourceTagName
 * @param targetTagNameParent
 * @param targetTagNameChild
 * @return document
 * @throws ParseException
 */
public static Document addNewElements(Document source, Document target, String sourceTagName,
        String targetTagNameParent, String targetTagNameChild, List<String> ignoreList) throws ParseException { //list for attributes to be more dynamic if reuse
    //remove old entries 
    target = removeAllOptions(target);
    // add new entries
    NodeList nameList = source.getElementsByTagName(sourceTagName);
    NodeList parents = target.getElementsByTagName(targetTagNameParent);
    Element parent = (Element) parents.item(0); // g:options - only 1
    // element
    //add space placeholder as first entry
    Element o = target.createElement(targetTagNameChild);
    o.setAttribute("g:value", "");
    o.setAttribute("g:label", "");
    parent.appendChild(o);

    for (int i = 0; i < nameList.getLength(); i++) {
        String value = nameList.item(i).getTextContent();
        if (value != null && !value.equals("") && !ignoreList.contains(value)) {
            Element p = target.createElement(targetTagNameChild);
            p.setAttribute("g:value", value);
            p.setAttribute("g:label", value);
            parent.appendChild(p);
        }
    }
    return changeDate(target);
}

From source file:Main.java

/**
 * Adds or replaces node in parent./*from w  w w .  ja va2 s . c  o  m*/
 * @param parent
 * @param node
 * @throws Exception - Node cannot exist more than once,
 * i.e. multiple nodes with the same name cannot exist in parent.
 */
public static void replaceSingleNode(Element parent, final Node node) throws RuntimeException {

    NodeList nodes = parent.getElementsByTagName(node.getNodeName());

    if (nodes.getLength() > 1) {
        throw new RuntimeException(
                "Parent element contains multiple nodes with the name " + node.getNodeName());
    }
    if (nodes.getLength() == 0) {
        parent.appendChild(node);
    } else {
        parent.replaceChild(node, nodes.item(0));
    }
}

From source file:cz.incad.kramerius.rest.api.k5.client.utils.SOLRUtils.java

public static Element arr(Document doc, String attname, List vals) {
    synchronized (doc) {
        Element arrElm = doc.createElement("arr");
        if (attname != null)
            arrElm.setAttribute("name", attname);
        for (Object obj : vals) {
            if (obj instanceof String) {
                arrElm.appendChild(value(doc, (String) obj));
            } else if (obj instanceof Integer) {
                arrElm.appendChild(value(doc, (Integer) obj));
            } else
                throw new IllegalArgumentException("unsupported type " + obj.getClass().getName() + "");
        }//from   ww  w  .  j a  va2 s.  co  m
        return arrElm;
    }
}