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 void appendChild(Document doc, Node parentNode, String childName, String childContents) {
    if (childContents == null)
        throw (new NullPointerException("ChildNode is null."));

    Element child = doc.createElement(childName);
    child.setTextContent(childContents);
    parentNode.appendChild(child);//from  w w w  . jav a 2  s  .  c o m
}

From source file:net.bpelunit.framework.control.util.BPELUnitUtil.java

/**
 * Creates a new document with a dummy root node, intended to store literal data for a receive
 * or send.//  ww  w  .jav a  2 s .c o  m
 * 
 * CAUTION: This method depends on initialization which is done by calling
 * {@link initializeParsing}. Not initializing this class will cause uncaught NPEs.
 * 
 * @return
 */
public static Element generateDummyElementNode() {
    Document document = fgDocumentBuilder.newDocument();
    Element root = document.createElement(DUMMY_ELEMENT_NAME);
    document.appendChild(root);
    return root;
}

From source file:Main.java

/**
 * Add the service references to the document.
 *
 * @param document//from   w w  w  .  jav a 2  s  .  c  om
 */
// FIXME: This provides trivial information. Necessary?
public static void addServices(Document document) {

    if (document != null) {

        Element root = document.getDocumentElement();

        if (root != null) {

            Element element = document.createElement("services");

            Element service = document.createElement("service");
            service.setAttribute("name", "run");
            service.setAttribute("url", "run");
            element.appendChild(service);

            service = document.createElement("service");
            service.setAttribute("name", "module");
            service.setAttribute("url", "module");
            element.appendChild(service);

            root.appendChild(element);
        }
    }
}

From source file:Main.java

/**
 * Convert Properties to string// w  ww .j  a  va 2  s.c  om
 *
 * @param props
 * @return xml string
 * @throws IOException
 */
public static String writePropToString(Properties props) throws IOException {
    try {
        org.w3c.dom.Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
        org.w3c.dom.Element conf = doc.createElement("configuration");
        doc.appendChild(conf);
        conf.appendChild(doc.createTextNode("\n"));
        for (Enumeration e = props.keys(); e.hasMoreElements();) {
            String name = (String) e.nextElement();
            Object object = props.get(name);
            String value;
            if (object instanceof String) {
                value = (String) object;
            } else {
                continue;
            }
            org.w3c.dom.Element propNode = doc.createElement("property");
            conf.appendChild(propNode);

            org.w3c.dom.Element nameNode = doc.createElement("name");
            nameNode.appendChild(doc.createTextNode(name.trim()));
            propNode.appendChild(nameNode);

            org.w3c.dom.Element valueNode = doc.createElement("value");
            valueNode.appendChild(doc.createTextNode(value.trim()));
            propNode.appendChild(valueNode);

            conf.appendChild(doc.createTextNode("\n"));
        }

        Source source = new DOMSource(doc);
        StringWriter stringWriter = new StringWriter();
        Result result = new StreamResult(stringWriter);
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.transform(source, result);

        return stringWriter.getBuffer().toString();
    } catch (Exception e) {
        throw new IOException(e);
    }
}

From source file:Main.java

/**
 * Add a RSS 2.0 Item to your channel node <channel>
 * Note: None of the parameter is supposed to be NULL
 *
 * @param XMLDocument the current XML Document
 * @param channelRoot your channel node/* ww w .jav  a  2  s .c o m*/
 * @param title title of your entry
 * @param link link to further information
 * @param guid globally unique identifier - same as link
 * @param pubDate date of the new entry
 * @param text text of the item
 */
public static void addRSSItem(Document XMLDocument, Node channelRoot, String title, String link, String guid,
        String pubDate, String text) {
    // Create <item> node
    Node entry = XMLDocument.createElement("item");

    // Set title node beneath <item>
    Node subentry = XMLDocument.createElement("title");
    subentry.appendChild(XMLDocument.createTextNode(title));
    entry.appendChild(subentry);

    // Set link node beneath <item>
    subentry = XMLDocument.createElement("link");
    subentry.appendChild(XMLDocument.createTextNode(link));
    entry.appendChild(subentry);

    // Set guid node beneath <item>
    subentry = XMLDocument.createElement("guid");
    subentry.appendChild(XMLDocument.createTextNode(guid));
    entry.appendChild(subentry);

    // Set pubDate beneath <item>
    subentry = XMLDocument.createElement("pubDate");
    subentry.appendChild(XMLDocument.createTextNode(pubDate));
    entry.appendChild(subentry);

    // Set last message node beneath <host>
    subentry = XMLDocument.createElement("description");
    subentry.appendChild(XMLDocument.createTextNode(text));
    entry.appendChild(subentry);

    channelRoot.appendChild(entry);
}

From source file:Utils.java

public static Element findElementAndSetElseCreateAndSet(Document document, Element parent, String child,
        String value) {//from w w  w  . j ava 2 s  . c  o  m
    NodeList nl = parent.getElementsByTagName(child);
    if (nl.getLength() == 0) {
        parent.appendChild(document.createElement(child));
    }
    Element ret = (Element) parent.getElementsByTagName(child).item(0);
    if (ret.getFirstChild() != null) {
        ret.removeChild(ret.getFirstChild());
    }
    ret.appendChild(document.createTextNode(value));
    return ret;
}

From source file:Main.java

public static void writeXMLObject(String path, String name, String tag, String model, double ry, String version,
        double x, double y, double z) {
    try {//from  ww  w.  j a  v a  2s .  co  m
        id++;
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

        // Root elements
        Document doc = docBuilder.newDocument();
        doc.setXmlStandalone(true);
        Element rootElement = doc.createElement(tag);
        doc.appendChild(rootElement);

        rootElement.setAttributeNode(addAttribut(doc, "action", "void"));
        rootElement.setAttributeNode(addAttribut(doc, "actionDist", "10.0"));
        rootElement.setAttributeNode(addAttribut(doc, "collidable", "true"));
        rootElement.setAttributeNode(addAttribut(doc, "id", id + ""));
        rootElement.setAttributeNode(addAttribut(doc, "model", model));
        rootElement.setAttributeNode(addAttribut(doc, "pCameraFacing", "true"));
        rootElement.setAttributeNode(addAttribut(doc, "pControlFlow", "false"));
        rootElement.setAttributeNode(addAttribut(doc, "pEmitH", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pEmitInnerRadius", "0.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pEmitOutterRadius", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pEmitType", "0"));
        rootElement.setAttributeNode(addAttribut(doc, "pEmitW", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pEndA", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pEndB", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pEndMass", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pEndR", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pEndSize", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pEndV", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pFactoryNumber", "500"));
        rootElement.setAttributeNode(addAttribut(doc, "pInitialVelocity", "0.0030"));
        rootElement.setAttributeNode(addAttribut(doc, "pMaxAngle", "10.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pMaxLife", "2000.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pMinAngle", "0.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pMinLife", "1000.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pParticulesPerSecVar", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pParticulesPerSeconds", "100"));
        rootElement.setAttributeNode(addAttribut(doc, "pSpeed", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pStartA", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pStartB", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pStartMass", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pStartR", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pStartSize", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pStartV", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "rx", "0.0"));
        rootElement.setAttributeNode(addAttribut(doc, "ry", ry + ""));
        rootElement.setAttributeNode(addAttribut(doc, "rz", "0.0"));
        rootElement.setAttributeNode(addAttribut(doc, "s", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "type", "basic"));
        rootElement.setAttributeNode(addAttribut(doc, "versionCode", version));
        rootElement.setAttributeNode(addAttribut(doc, "x", x + ""));
        rootElement.setAttributeNode(addAttribut(doc, "y", y + ""));
        rootElement.setAttributeNode(addAttribut(doc, "z", z + ""));

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(path + "\\" + name + id + ".xml");
        transformer.transform(source, result);
    } catch (ParserConfigurationException pce) {
        pce.printStackTrace();
    } catch (TransformerException tfe) {
        tfe.printStackTrace();
    }
}

From source file:Main.java

/**
 * adds an Element with one attribut to a DOM-tree.
 * /*ww w  .ja v  a2s  .  co m*/
 * @param document
 *            Document: the DOM-tree to add to
 * @param father
 *            Element: the new element will be inserted directly under this
 *            Element in the tree
 * @param name
 *            String: the name of the new element
 * @param attOneName
 *            String: the name of the attribut
 * @param attOneValue
 *            String: the value of the attribut
 */
public static void addElement(Document document, Element father, String name, String attOneName,
        String attOneValue) {
    Element element = document.createElement(name);
    element.setAttribute(attOneName, attOneValue);
    father.appendChild(element);
}

From source file:Main.java

/**
 * Return the root element for specified document<br>
 * Create if it does not already exist with the specified name
 *//*from  w w w . j ava2 s .  c  om*/
private static Element getRootElement(Document doc, boolean create, String name) {
    if (doc != null) {
        Element result = doc.getDocumentElement();

        if ((result == null) && create) {
            result = doc.createElement(name);
            doc.appendChild(result);
        }

        return result;
    }

    return null;
}

From source file:Main.java

public static Element appendResultSetToNode(Element root, String rowTag, ResultSet rs) throws SQLException {
    Document doc = root.getOwnerDocument();

    ResultSetMetaData meta = rs.getMetaData();
    int columnCount = meta.getColumnCount();
    int rowCount = 0;
    while (rs.next()) {
        Element rowElement = doc.createElement(rowTag);
        rowElement.setAttribute("row", "" + rowCount);
        for (int i = 1; i <= columnCount; i++) {
            rowElement.setAttribute(meta.getColumnName(i), rs.getString(i));
        }/*  ww  w .  j  av a 2  s  .  co m*/
        rowCount++;
        root.appendChild(rowElement);
    }

    return root;
}