Example usage for org.w3c.dom Document createElement

List of usage examples for org.w3c.dom Document createElement

Introduction

In this page you can find the example usage for org.w3c.dom Document createElement.

Prototype

public Element createElement(String tagName) throws DOMException;

Source Link

Document

Creates an element of the type specified.

Usage

From source file:Main.java

public static Element createTextElement(Document document, String tag, String text) {
    if (document == null || tag == null || text == null) {
        return null;
    }/*  ww  w. ja va  2 s  .  c  om*/

    Element element = document.createElement(tag);
    Text textNode = document.createTextNode(text);
    element.appendChild(textNode);
    return element;
}

From source file:Main.java

public static Document startXMLDocument(String _firstTag) {
    DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = null;
    Document doc = null;
    try {/* ww w.j a v  a 2s  .c o  m*/
        docBuilder = dbfac.newDocumentBuilder();
        doc = docBuilder.newDocument();

        Element xmlRoot = doc.createElement(_firstTag);
        doc.appendChild(xmlRoot);

    } catch (ParserConfigurationException e) {
        System.out.println("C:" + e.getClass() + "\nM:" + e.getMessage());
        return null;
    }
    return doc;
}

From source file:Main.java

public static void createElementAndAppend(String name, int value, Document doc, Element appendeeElement,
        String attributeName, String attributeValue) {
    Element newElement = doc.createElement(name);
    Text text = doc.createTextNode(String.valueOf(value));
    newElement.appendChild(text);/*  w ww. j  a  v a 2s. c  om*/
    if (attributeName != null && !attributeName.equals("")) {
        newElement.setAttribute(attributeName, attributeValue);
    }
    appendeeElement.appendChild(newElement);
}

From source file:Main.java

public static void createElementAndAppend(String name, double value, Document doc, Element appendeeElement,
        String attributeName, String attributeValue) {
    Element newElement = doc.createElement(name);
    Text text = doc.createTextNode(String.valueOf(value));
    newElement.appendChild(text);//from  ww w  .j  ava2 s. c o  m
    if (attributeName != null && !attributeName.equals("")) {
        newElement.setAttribute(attributeName, attributeValue);
    }
    appendeeElement.appendChild(newElement);
}

From source file:Main.java

public static void exportXmlFile(ArrayList<?> listObject, String rootElement, String interfaceName,
        String pathSaveFile) {/*from  w  w  w .j  a  va 2s.c  o  m*/
    try {
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
        // creating a new instance of a DOM to build a DOM tree.
        Document doc = docBuilder.newDocument();

        Element root = doc.createElement(rootElement);
        // adding a node after the last child node of the specified node.
        doc.appendChild(root);

        Element interfaceElement = null;

        Element child = null;
        Text text;

        for (Object obj : listObject) {
            Class srcClass = obj.getClass();
            Field[] field = srcClass.getFields();
            interfaceElement = doc.createElement(interfaceName);
            for (int i = 0; i < field.length; i++) {
                // System.out.println(field[i].getName() + ":" +
                // field[i].get(obj));
                child = doc.createElement(field[i].getName());

                text = doc.createTextNode((field[i].get(obj)).toString());
                child.appendChild(text);

                interfaceElement.appendChild(child);
            }
            root.appendChild(interfaceElement);
        }

        // TransformerFactory instance is used to create Transformer
        // objects.
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();

        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        // create string from xml tree
        StringWriter sw = new StringWriter();
        StreamResult result = new StreamResult(sw);
        DOMSource source = new DOMSource(doc);
        transformer.transform(source, result);
        String xmlString = sw.toString();

        File file = new File(pathSaveFile);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
        bw.write(xmlString);
        bw.flush();
        bw.close();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

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);/* w  ww.  j a  v a 2  s.  c  o  m*/

    //artifactId
    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

/**
 * attach (append to child) RGB to the element, in the format <red>12</red> etc
 * @param rgb//from  ww w  . j a  v a  2s.com
 * @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 void appendChild(Document document, Node root, String name, String value) {
    Node node = document.createElement(name);
    node.appendChild(document.createTextNode(value != null ? value : ""));
    root.appendChild(node);//from   ww  w .  ja  va 2s. co m
    return;
}

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);
    }/*from  w  ww . j a  v  a  2  s.  c  o m*/

From source file:Main.java

public static void addNewElementWithAttribute(Document xmlDoc, Node node, String elementName,
        String elementValue, String attrName, String attrValue) {
    // element/*  w  ww.ja  v a2s .c  o  m*/
    Node newVal = xmlDoc.createElement(elementName);
    Node newValText = xmlDoc.createTextNode(elementValue);
    newVal.appendChild(newValText);
    // attribute
    addAttribute(xmlDoc, newVal, attrName, attrValue);
    // add to node
    node.appendChild(newVal);
}