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

/**
 * add element to parent withe the specified value by the node name
 *
 * @param parent/*w w  w.  j  a  va  2s .  c  o  m*/
 * @param tagName
 * @param text
 */
public static void addElement(Element parent, String tagName, String text) {
    Document doc = parent.getOwnerDocument();
    Element child = doc.createElement(tagName);
    setElementValue(child, text);
    parent.appendChild(child);
}

From source file:Main.java

public static Element addElement(Element parent, String childName, String attributeName, String attributeValue,
        Document doc) {//from w  w w . j  a v a 2s.c  o m
    Element childElement = doc.createElementNS(DEFAULT_NAMESPACE, childName);
    childElement.setAttribute(attributeName, attributeValue);
    parent.appendChild(childElement);
    return childElement;
}

From source file:Main.java

/**
 * Appends the CDATA element to the parent element.
 * //  w  w w . ja  va  2  s  .c o m
 * @param parent the parent element
 * @param tagName the CDATA element name
 * @param value the CDATA element value
 * @return the CDATA element added to the parent element
 */
public static Element appendCDATAElement(Element parent, String tagName, String value) {
    Element child = appendElement(parent, tagName);
    if (value == null) { // avoid "null" word in the XML payload
        value = "";
    }

    Node cdata = child.getOwnerDocument().createCDATASection(value);
    child.appendChild(cdata);
    return child;
}

From source file:Main.java

private static void addEntry(String file_path, String operation, String key, String value) {

    Document doc = getDocument(file_path);
    if (doc == null) {
        return;//from w w w  . j  a va2  s .c  om
    }

    Element entry_elem = doc.createElement("Entry");
    entry_elem.setAttribute("Type", operation);
    entry_elem.setAttribute("Key", key);
    if (operation.equals("STORE")) {
        entry_elem.appendChild(doc.createTextNode(value));
    }

    Element log_element = (Element) doc.getElementsByTagName("Log").item(0);
    log_element.appendChild(entry_elem);

    saveFile(file_path, doc);

}

From source file:Main.java

static void generateXMLIntervalSampleCoarse(double time, long tracks, String hardware, boolean finalStats) {
    Element element;/*  w  w w  . j  av a 2s  .  c  om*/
    Element root = doc.getDocumentElement();

    Element me;
    if (finalStats)
        me = doc.createElement("FinalSample");
    else
        me = doc.createElement("Sample");
    root.appendChild(me);
    root = me;

    element = doc.createElement("Timestamp");
    element.setTextContent(Double.toString(time));
    root.appendChild(element);

    element = doc.createElement("Hardware");
    element.setTextContent(hardware);
    root.appendChild(element);

    // if (transactions > 0) {
    element = doc.createElement("DataAvailable");
    element.setTextContent("True");
    root.appendChild(element);

    element = doc.createElement("LaidTracks");
    element.setTextContent(Long.toString(tracks));
    root.appendChild(element);

}

From source file:Main.java

public static Element createElmWithText(Document doc, String tagName, String text) {
    Element elm = doc.createElement(tagName);
    text = text != null ? text.trim() : "";
    Node node = (text.indexOf('<') >= 0 || text.indexOf('\n') >= 0) ? doc.createCDATASection(text)
            : doc.createTextNode(text);/* w w w. jav  a 2  s.  com*/
    elm.appendChild(node);
    return elm;
}

From source file:Main.java

@SuppressWarnings({ "ChainOfInstanceofChecks" })
private static void formatElementEnd(/*@Nonnull*/Document document, /*@Nonnull*/Element element, /*@Nonnull*/
        String formatString) {/*from  w w  w  .  j  a  v  a 2  s  . c  om*/
    Node lastChild = element.getLastChild();

    if (lastChild != null) {
        if (lastChild instanceof Element) {
            element.appendChild(document.createTextNode(formatString));
        } else if (lastChild instanceof Text) {
            Text textNode = (Text) lastChild;
            String text = textNode.getWholeText();

            if (hasOnlyTextSiblings(textNode)) {
                String trimmedText = text.trim();
                if (!trimmedText.equals(text)) {
                    textNode.replaceWholeText(trimmedText);
                }
            } else {
                if (!formatString.equals(text)) {
                    textNode.replaceWholeText(trimRight(text) + formatString);
                }
            }
        }
    }
}

From source file:Main.java

/** 
 * Appends the CDATA element to the parent element. 
 * //from  w  w w  .  j  av a2 s.  c  o  m
 * @param parent the parent element 
 * @param tagName the CDATA element name 
 * @param value the CDATA element value 
 * @return the CDATA element added to the parent element 
 */
public static Element appendCDATAElement(Element parent, String tagName, String value) {
    Element child = appendElement(parent, tagName);
    if (value == null) { // avoid "null" word in the XML payload  
        value = "";
    }

    Node cdata = child.getOwnerDocument().createCDATASection(value);
    child.appendChild(cdata);
    return child;
}

From source file:Main.java

/**
 * Sets element CDATA data/*w  w w. j a v a 2  s.  co  m*/
 * 
 * @param e
 *            the lement
 * @param data
 *            the new data
 */
public static void setElementCDataValue(Element e, String data) {
    CDATASection txt = getElementCDataNode(e);
    if (txt != null) {
        txt.setData(data);
    } else {
        txt = e.getOwnerDocument().createCDATASection(data);
        e.appendChild(txt);
    }
}

From source file:fr.ece.epp.tools.Utils.java

public static void updatePom(String path, String[] repo, boolean outOrno) {
    Document document = null;//from  ww  w  .  j ava2  s .  c  o m
    try {
        document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(path);
        Element root = document.getDocumentElement();
        Node repositories = root.getElementsByTagName("repositories").item(0);
        for (int i = 0; i < repo.length; i++) {
            Element repository = document.createElement("repository");

            Element id = document.createElement("id");
            id.appendChild(document.createTextNode("repository" + i));
            repository.appendChild(id);

            Element layout = document.createElement("layout");
            layout.appendChild(document.createTextNode("p2"));
            repository.appendChild(layout);

            Element url = document.createElement("url");
            url.appendChild(document.createTextNode(repo[i]));

            repository.appendChild(url);
            repositories.appendChild(repository);
        }
        output(root, path);
        if (outOrno) {
            output(root, null);
        }

    } catch (SAXException e) {
    } catch (IOException e) {
    } catch (ParserConfigurationException e) {
    }
}