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

public static Element createFailure(Document outputDoc, String commandName, String type) {
    Element failure = outputDoc.createElement("failure");
    failure.setAttribute("type", type);
    Element cmd = outputDoc.createElement("command");
    cmd.setAttribute("name", commandName);
    failure.appendChild(cmd);

    Element parameter = outputDoc.createElement("parameters");
    failure.appendChild(parameter);//from   w ww. j  av  a  2 s.  c o m

    // Element output = outputDoc.createElement("output");
    // failure.appendChild(output);
    return failure;
}

From source file:Main.java

public static Element prependChildElement(Element parent, Element child, boolean addWhitespace, Document doc) {

    Node firstChild = parent.getFirstChild();
    if (firstChild == null) {
        parent.appendChild(child);
    } else {/*from  ww  w.java 2  s  .  c  o m*/
        parent.insertBefore(child, firstChild);
    }

    if (addWhitespace) {
        Node whitespaceText = doc.createTextNode("\n");
        parent.insertBefore(whitespaceText, child);
    }
    return child;
}

From source file:Main.java

public static String setChildText(Element elt, String name, String value) {
    Element child = getChildElement(elt, name);

    if (child == null) {
        child = elt.getOwnerDocument().createElement(name);
        elt.appendChild(child);
    }//from  w w w  .j  a  va2 s  .  co  m
    return setInnerText(child, value);

}

From source file:Main.java

public static Element createElement(Element element, String tagName, String text) {
    Document doc = element.getOwnerDocument();
    Element child = doc.createElement(tagName);
    if (text != null)
        child.setTextContent(text);/*  ww  w  . j a  va  2 s. c  om*/
    element.appendChild(child);
    return child;
}

From source file:Main.java

public static Element clear(Element node, String subElement) {
    Element child = findFirst(node, subElement);
    if (child == null) {
        child = node.getOwnerDocument().createElement(subElement);
        node.appendChild(child);
    }//from  w w  w. j  a  v a  2 s .c om
    child.setTextContent(null); // remove all descendants
    return child;
}

From source file:DOMEdit.java

public static void insert(Document doc, String name, String phone, String email) {
    Element personNode = doc.createElement("person");

    Element nameNode = doc.createElement("name");
    personNode.appendChild(nameNode);
    Text nametextNode = doc.createTextNode(name);
    nameNode.appendChild(nametextNode);/*from w ww.ja  va 2  s. co  m*/

    Element phoneNode = doc.createElement("phone");
    personNode.appendChild(phoneNode);
    Text phonetextNode = doc.createTextNode(phone);
    phoneNode.appendChild(phonetextNode);

    Element emailNode = doc.createElement("email");
    personNode.appendChild(emailNode);
    Text emailtextNode = doc.createTextNode(email);
    emailNode.appendChild(emailtextNode);

    Element root = doc.getDocumentElement();
    Node firstChildNode = root.getFirstChild();
    root.insertBefore(personNode, firstChildNode);
}

From source file:Main.java

public static Element getCustomerContactTypeWithText(Document doc, String elementName, String name,
        String email) {//from  www . j a v  a  2 s  .  c om

    Element elem_customer_contact_type = doc.createElement(elementName);

    Element elem_name = getElementWithText(doc, "Name", name);
    Element elem_email = getElementWithText(doc, "Email", email);

    elem_customer_contact_type.appendChild(elem_name);
    elem_customer_contact_type.appendChild(elem_email);

    return elem_customer_contact_type;
}

From source file:Main.java

/**
 * attach (append to child) RGB to the element, in the format <red>12</red> etc
 * @param rgb/*from  w w  w.j  a  va2s. c  o  m*/
 * @param elem
 * @param doc
 */
public static void attachRGB(Color rgb, Element elem, Document doc) {
    int r = rgb.getRed();
    int g = rgb.getGreen();
    int b = rgb.getBlue();

    //create element
    Element red = doc.createElement(TAG_NAME_RED);
    Element green = doc.createElement(TAG_NAME_GREEN);
    Element blue = doc.createElement(TAG_NAME_BLUE);

    //fill the content
    red.appendChild(doc.createTextNode(Integer.toString(r)));
    green.appendChild(doc.createTextNode(Integer.toString(g)));
    blue.appendChild(doc.createTextNode(Integer.toString(b)));

    //structure the content
    elem.appendChild(red);
    elem.appendChild(green);
    elem.appendChild(blue);
}

From source file:Main.java

public static String convertResultSetToXML(ResultSet rs)
        throws SQLException, ParserConfigurationException, TransformerException {
    ResultSetMetaData rsmd = rs.getMetaData();
    int colCount = rsmd.getColumnCount();

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.newDocument();
    Element results = doc.createElement("Results");
    doc.appendChild(results);/*from w  ww  .  j  ava  2 s.co m*/

    while (rs.next()) {
        Element row = doc.createElement("Row");
        results.appendChild(row);
        for (int i = 1; i <= colCount; i++) {
            String columnName = rsmd.getColumnName(i);
            Object value = rs.getObject(i);
            if (value != null) {
                Element node = doc.createElement(columnName.replaceAll("\\(", "_").replaceAll("\\)", "_"));
                node.appendChild(doc.createTextNode(value.toString()));
                row.appendChild(node);
            }
        }
    }

    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    StringWriter writer = new StringWriter();
    transformer.transform(new DOMSource(doc), new StreamResult(writer));
    String output = writer.getBuffer().toString().replaceAll("\n|\r", "");

    return output;
}

From source file:DOMEdit.java

private static Element makePersonNode(Document doc, String name, String phone) {
        Element nameNode = doc.createElement("name");
        Text nametextNode = doc.createTextNode(name);
        nameNode.appendChild(nametextNode);

        Element phoneNode = doc.createElement("phone");
        Text phonetextNode = doc.createTextNode(phone);
        phoneNode.appendChild(phonetextNode);

        Element personNode = doc.createElement("person");
        personNode.appendChild(nameNode);
        personNode.appendChild(phoneNode);
        return (personNode);
    }/*  w  w w.  j  a va2  s . co m*/