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

/**
 * Creates (only if necessary) and returns the element which is at the end of the specified
 * path./*from  w  ww .ja v a2s.  c  o  m*/
 * @param doc the target document where the specified path should be created
 * @param path a dot separated string indicating the path to be created
 * @return the component at the end of the newly created path.
 */
public static Element createLastPathComponent(Document doc, String[] path) {
    Element parent = (Element) doc.getFirstChild();
    if (path == null || parent == null || doc == null)
        throw new IllegalArgumentException("Document parent and path must not be null");

    Element e = parent;
    for (int i = 0; i < path.length; i++) {
        Element newEl = getChildElementByTagName(e, path[i]);
        if (newEl == null) {
            newEl = doc.createElement(path[i]);
            e.appendChild(newEl);
        }
        e = newEl;
    }
    return e;
}

From source file:Main.java

public static Element generateMvnFailsafePlugin(Document document) {
    //generate plugin element
    Element plugin = document.createElement(PLUGIN_NAME);

    //generate groupId tag, add to plugin
    Element groupId = document.createElement(GROUPID_NAME);
    groupId.setTextContent("org.apache.maven.plugins");
    plugin.appendChild(groupId);

    //artifactId//from  ww  w .jav  a  2 s.  com
    Element artifactId = document.createElement(ARTIFACTID_NAME);
    artifactId.setTextContent("maven-failsafe-plugin");
    plugin.appendChild(artifactId);

    //version
    Element version = document.createElement(VERSION_NAME);
    version.setTextContent("2.13");
    plugin.appendChild(version);

    //Executions
    Element executions = document.createElement(EXECUTIONS_NAME);
    plugin.appendChild(executions);

    //There are two Execute elements

    //Execute1
    Element execution1 = document.createElement(EXECUTION_NAME);
    executions.appendChild(execution1);
    //Its id
    Element id1 = document.createElement(ID_NAME);
    id1.setTextContent("integration-test");
    execution1.appendChild(id1);
    //Its goals
    Element goals1 = document.createElement(GOALS_NAME);
    execution1.appendChild(goals1);
    //The goals' goal
    Element goal1 = document.createElement(GOAL_NAME);
    goal1.setTextContent("integration-test");
    goals1.appendChild(goal1);

    //Execute2
    Element execution2 = document.createElement(EXECUTION_NAME);
    executions.appendChild(execution2);
    //Its id
    Element id2 = document.createElement(ID_NAME);
    id2.setTextContent("verify");
    execution2.appendChild(id2);
    //Its goals
    Element goals2 = document.createElement(GOALS_NAME);
    execution2.appendChild(goals2);
    //The goals' goal
    Element goal2 = document.createElement(GOAL_NAME);
    goal2.setTextContent("verify");
    goals2.appendChild(goal2);

    return plugin;
}

From source file:Main.java

static void initializeXMLReport(int numThreads, int experiment, int sampleInterval, String managerClassName,
        String benchmarkClassName, String adapterClassName) {
    try {/*from   ww w.j av  a2s  . c  o m*/
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        doc = builder.newDocument();

        Element root = doc.createElement("Statistics");
        doc.appendChild(root);

        Element element = doc.createElement("Benchmark");
        element.setTextContent(benchmarkClassName);
        root.appendChild(element);

        element = doc.createElement("Adapter");
        element.setTextContent(adapterClassName);
        root.appendChild(element);

        element = doc.createElement("ContentionManager");
        element.setTextContent(managerClassName);
        root.appendChild(element);

        element = doc.createElement("Threads");
        element.setTextContent(Integer.toString(numThreads));
        root.appendChild(element);

        element = doc.createElement("Mix");
        element.setTextContent(Integer.toString(experiment));
        root.appendChild(element);

        element = doc.createElement("SampleInterval");
        element.setTextContent(Long.toString(sampleInterval));
        root.appendChild(element);

        String name = System.getProperty("user.name");
        if (name == null)
            name = "";
        element = doc.createElement("Owner");
        element.setTextContent(name);
        root.appendChild(element);

        java.util.Calendar cal = java.util.Calendar.getInstance(java.util.TimeZone.getDefault());
        String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_FORMAT);
        sdf.setTimeZone(java.util.TimeZone.getDefault());
        element = doc.createElement("Date");
        element.setTextContent(sdf.format(cal.getTime()));
        root.appendChild(element);

    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

private static Element addNewProfileSection(Document document, Element profiles, String profileIdVal) {
    //start with profile node
    Element profile = document.createElement(PROFILE_NODE_NAME);
    profiles.appendChild(profile);

    //create the id tag
    Node id = document.createElement(PROFILE_ID_NAME);
    //id.setNodeValue(profileIdVal);
    id.setTextContent(profileIdVal);//from  w  w  w.  j  av a2s.c  om
    //add it to the profile
    profile.appendChild(id);

    //create activation tag
    Node activeByDefault = document.createElement(ACTIVEBYDEFAULT_NAME);
    //activeByDefault.setNodeValue(ACTIVEBYDEFAULT_VAL);
    activeByDefault.setTextContent(ACTIVEBYDEFAULT_VAL);
    Node activation = document.createElement(ACTIVATION_NAME);
    activation.appendChild(activeByDefault);
    //add it to profile
    profile.appendChild(activation);

    //create build tag
    Node build = document.createElement(BUILD_NAME);
    //add it to profile
    profile.appendChild(build);

    //create plugins tag, add to build
    Node plugins = document.createElement(PLUGINS_NAME);
    build.appendChild(plugins);

    return (Element) plugins;
}

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

private static Element buildAssociations(Document doc, List<AssociationBean> list) {
    Element associationsE = doc.createElement(ENTRY_ASSOCIATIONS_NODE);
    for (AssociationBean associationBean : list) {
        Element asso = buildAssociation(doc, associationBean);
        associationsE.appendChild(asso);
    }//from   ww w.ja v a 2  s  . c o m
    return associationsE;
}

From source file:Main.java

public static void edit(Document doc) {
    Element element = doc.getDocumentElement();
    Element element2 = doc.createElement("newname");
    NamedNodeMap attrs = element.getAttributes();
    for (int i = 0; i < attrs.getLength(); i++) {
        Attr attr2 = (Attr) doc.importNode(attrs.item(i), true);
        element2.getAttributes().setNamedItem(attr2);
    }// w  w w .  j a  va2s  .  c  o  m
    while (element.hasChildNodes()) {
        element2.appendChild(element.getFirstChild());
    }
    element.getParentNode().replaceChild(element2, element);
}

From source file:Main.java

/**
 * Sets data to be the TEXT content of element
 *
 * @param parentNode the parent element.
 * @param data the data to set./*from  w w  w.  j  av a  2  s  . co  m*/
 */
public static void setText(Element parentNode, String data) {
    if (data == null)
        return;

    Text txt = getTextNode(parentNode);

    if (txt != null)
        txt.setData(data);
    else {
        txt = parentNode.getOwnerDocument().createTextNode(data);
        parentNode.appendChild(txt);
    }
}

From source file:Main.java

static void generateXMLIntervalSample(double time, long commits, long transactions, long commitMemRefs,
        long totalMemRefs, String hardware, boolean finalStats) {
    Element element;//from   w w  w.j  a  v a2s.c  o m
    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("Transactions");
    element.setTextContent(Long.toString(transactions));
    root.appendChild(element);

    element = doc.createElement("Commits");
    element.setTextContent(Long.toString(commits));
    root.appendChild(element);

    String pc;
    if (commits != 0 && transactions != 0) {
        pc = Long.toString(100 * commits / transactions);
    } else {
        pc = "0";
    }
    element = doc.createElement("PercentCommits");
    element.setTextContent(pc);
    root.appendChild(element);

    element = doc.createElement("MemRefs");
    element.setTextContent(Long.toString(totalMemRefs));
    root.appendChild(element);

    element = doc.createElement("CommitMemRefs");
    element.setTextContent(Long.toString(commitMemRefs));
    root.appendChild(element);

    String pm;
    if (commitMemRefs != 0 && totalMemRefs != 0) {
        pm = Long.toString(100 * commitMemRefs / totalMemRefs);
    } else {
        pm = "0";
    }
    element = doc.createElement("PercentCommitMemRefs");
    element.setTextContent(pm);
    root.appendChild(element);
}

From source file:Main.java

/**
 * Sets data to be the CDATA content of element
 *
 * @param element the parent element.//w  ww.ja  v  a2s . c o m
 * @param data the data to set.
 */
public static void setCData(Element element, String data) {
    if (data == null)
        return;

    CDATASection txt = getCDataNode(element);
    if (txt != null)
        txt.setData(data);
    else {
        txt = element.getOwnerDocument().createCDATASection(data);
        element.appendChild(txt);
    }
}

From source file:Main.java

public static Element getInitPortalPlacementTypeWithText(Document doc, String elementName, String category,
        String subCategory, String position) {
    Element elem_init_portal_placement_type = doc.createElement(elementName);

    Element elem_category = getElementWithText(doc, "Category", category);
    Element elem_subCategory = getElementWithText(doc, "Subcategory", subCategory);
    Element elem_position = getElementWithText(doc, "Position", position);

    elem_init_portal_placement_type.appendChild(elem_category);
    elem_init_portal_placement_type.appendChild(elem_subCategory);
    elem_init_portal_placement_type.appendChild(elem_position);

    return elem_init_portal_placement_type;
}