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/*from w ww .  j  av  a2s.  c o m*/
 * @param watcherPersonBeans
 * @param root
 * @param dom
 * @param watcherListName
 * @param watcherElement
 */
private static void addWatcherNodes(List<TPersonBean> watcherPersonBeans, Element root, Document dom,
        String watcherListName, String watcherElement) {
    if (watcherPersonBeans == null || watcherPersonBeans.isEmpty()) {
        appendChild(root, watcherListName + CUSTOM_XML_ELEMENTS.PRESENT_SUFFIX, Boolean.FALSE.toString(), dom);
    } else {
        Element watcherList = dom.createElement(watcherListName);
        for (TPersonBean personBean : watcherPersonBeans) {
            Element nameElement = createDomElement(CUSTOM_XML_ELEMENTS.NAME, personBean.getName(), dom);
            Element person = createDomElement(watcherElement, "", dom);
            person.appendChild(nameElement);
            watcherList.appendChild(person);
        }
        root.appendChild(watcherList);
        appendChild(root, watcherListName + CUSTOM_XML_ELEMENTS.PRESENT_SUFFIX, Boolean.TRUE.toString(), dom);
    }
}

From source file:Main.java

public static String setInnerText(Element node, String value) {
    StringBuilder sb = new StringBuilder();
    while (node.hasChildNodes()) {
        Node tn = node.getFirstChild();
        if (tn.getNodeType() == Node.TEXT_NODE) {
            sb.append(tn.getNodeValue());
        }/* w  w  w.  j av a2s . co m*/
        node.removeChild(tn);
    }
    node.appendChild(node.getOwnerDocument().createTextNode(value));
    return sb.toString();
}

From source file:Main.java

public static void setChildText(Element e, String tagname, String newValue, boolean cdata) {
    Element child = getChildByTagName(e, tagname);

    if (child == null) {
        Document doc = e.getOwnerDocument();
        child = doc.createElement(tagname);
        e.appendChild(child);
    }//from   www  . j av  a 2 s . c om

    setElementText(child, newValue, cdata);
}

From source file:com.omertron.thetvdbapi.tools.DOMHelper.java

/**
 * Add a child element to a parent element
 *
 * @param doc/*  w  w  w  . j  a v  a  2 s  .co m*/
 * @param parentElement
 * @param elementName
 * @param elementValue
 */
public static void appendChild(Document doc, Element parentElement, String elementName, String elementValue) {
    Element child = doc.createElement(elementName);
    Text text = doc.createTextNode(elementValue);
    child.appendChild(text);
    parentElement.appendChild(child);
}

From source file:Main.java

public static void appendElementText(Element parentElement, String elementName, String elementValue) {

    if (parentElement == null || elementName == null || elementValue == null)
        throw new NullPointerException();

    Element childElement = parentElement.getOwnerDocument().createElement(elementName);
    setTextContent(childElement, elementValue);
    parentElement.appendChild(childElement);
}

From source file:Main.java

/**
 * Adds a new element containing a CDATA section to the parent element
 * @param parent the parent element to add the new element to
 * @param namespaceURI the namespace of the added element's tag name, or null if there isn't any..
 * @param tag the tag name of the new element
 * @param text the text of the CDATA section (if text is null or empty no CDATA section will be added).
 *//*from  ww  w .j av a  2  s .  co m*/
public static void addTextElementNS(Element parent, String namespaceURI, String tag, String text) {

    // Add an element with the given tag name.
    Document document = parent.getOwnerDocument();
    Element textElement = namespaceURI != null ? document.createElementNS(namespaceURI, tag)
            : document.createElement(tag);
    parent.appendChild(textElement);

    if (text != null && text.length() > 0) {
        CDATASection cdata = createCDATASection(document, text);
        textElement.appendChild(cdata);
    }
}

From source file:com.sonar.maven.it.ItUtils.java

/**
 * Creates a settings xml with a sonar profile, containing all the given properties
 * Also adds repox to continue to use QAed artifacts 
 *///from   w w  w .  j  a  v a 2 s  .  com
public static String createSettingsXml(Map<String, String> props) throws Exception {
    DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = docBuilder.newDocument();

    Element settings = doc.createElement("settings");
    Element profiles = doc.createElement("profiles");
    Element profile = doc.createElement("profile");

    Element id = doc.createElement("id");
    id.setTextContent("sonar");

    Element properties = doc.createElement("properties");

    for (Map.Entry<String, String> e : props.entrySet()) {
        Element el = doc.createElement(e.getKey());
        el.setTextContent(e.getValue());
        properties.appendChild(el);
    }

    profile.appendChild(id);
    profile.appendChild(properties);
    profile.appendChild(createRepositories(doc));
    profile.appendChild(createPluginRepositories(doc));

    profiles.appendChild(profile);
    settings.appendChild(profiles);
    doc.appendChild(settings);

    Writer writer = new StringWriter();
    Transformer tf = TransformerFactory.newInstance().newTransformer();
    tf.transform(new DOMSource(doc), new StreamResult(writer));
    return writer.toString();
}

From source file:Main.java

public static Element cloneElementAs(Element srcEl, Document dstDoc, String elName) {
    if (srcEl.getNodeName().equals(elName)) {
        if (srcEl.getOwnerDocument() == dstDoc) {
            return (Element) srcEl.cloneNode(true);
        } else {//from w  ww  .j  av  a2s .c o m
            return (Element) dstDoc.importNode(srcEl, true);
        }
    } else {
        final Element dstEl = dstDoc.createElement(elName);
        final NodeList srcChildren = srcEl.getChildNodes();
        final int n = srcChildren.getLength();
        for (int i = 0; i < n; ++i) {
            final Node srcChild = srcChildren.item(i);
            final Node dstChild = dstDoc.importNode(srcChild, true);
            dstEl.appendChild(dstChild);
        }
        return dstEl;
    }
}

From source file:Main.java

/**
 * A helper method for quick property saving/modification. If the property element does not exist, it is automatically created.
 *
 * @param element The parent element./*  www.  j  a va 2s  .co  m*/
 * @param name The property name.
 * @param value The property value
 * @return
 */
public static void quickPropertyWrite(Element element, String name, String value) {
    NodeList nodeList = element.getElementsByTagName(name);
    if (nodeList.getLength() == 1) {
        nodeList.item(0).setTextContent(value);
    } else {
        Element newProp = element.getOwnerDocument().createElement(name);
        newProp.setTextContent(value);
        element.appendChild(newProp);
    }
}

From source file:Main.java

public static Document parseMetadataMap(Map<String, String> metainfo) throws Exception {
    DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
    DocumentBuilder bd = fact.newDocumentBuilder();
    Document doc = bd.newDocument();
    Element rt = doc.createElement("ROOT");
    doc.appendChild(rt);/*from w w w. j a va2s.c  o  m*/
    for (String key : metainfo.keySet()) {
        Element e1 = doc.createElement("param");
        e1.setAttribute("key", key);
        String value = metainfo.get(key);
        e1.setAttribute("value", value);
        rt.appendChild(e1);
    }
    return doc;
}