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 createElement(Document document, String name, Element child) {
    Element element = (Element) document.createElement(name);
    element.appendChild(child);//w ww. ja v  a  2 s .c  o  m
    return element;
}

From source file:Utils.java

public static Element findContainerElseCreate(Document document, Element parent, String child) {
    NodeList nl = parent.getElementsByTagName(child);
    if (nl.getLength() == 0) {
        parent.appendChild(document.createElement(child));
    }//from  ww w .  j  ava 2 s.co m
    return (Element) parent.getElementsByTagName(child).item(0);
}

From source file:Main.java

/**
 * This method is used to insert a new tag below the tag specified by
 * <code>appendTo</code> parameter.
 * /* w w w  . j  a  v  a 2 s  .c om*/
 * @param d
 *            the <code>Document</code> object to which a new tag is to be
 *            inserted.
 * @param appendTo
 *            the tag below which a new tag needs to be inserted.
 * @param tagName
 *            the name of new tag
 * @param tagValue
 *            the value of new tag
 */
public static Element insertNewTagBelow(Document d, String appendTo, String tagName, String tagValue) {
    Node element = d.getElementsByTagName(appendTo).item(0);
    if (element == null) {
        element = d.createElement(appendTo);
    }
    Element newElement = d.createElement(tagName);
    element.appendChild(newElement);
    newElement.appendChild(d.createTextNode(tagValue));
    return newElement;
}

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);/*from w  ww. j  av  a 2s  . com*/
    Text nametextNode = doc.createTextNode(name);
    nameNode.appendChild(nametextNode);

    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

/**
 * Append application specific tags into xml signature document
 * @param document/*from  w  w w.ja  va  2  s .  com*/
 * @return
 */
public static Document addUserInfoToSignature(Document document) {

    Element signature = document.getDocumentElement();

    // initially it has no root-element, ... so we create it:
    Element root = document.createElement("digital-signature");

    Element subjInfo = document.createElement("subject-information");
    subjInfo.appendChild(document.createElement("subject"));
    subjInfo.appendChild(document.createElement("date"));
    subjInfo.appendChild(document.createElement("time"));
    subjInfo.appendChild(document.createElement("timestamp"));

    root.appendChild(subjInfo);
    root.appendChild(signature);
    // we can add an element to a document only once,
    // the following calls will raise exceptions:
    document.appendChild(root);
    document.setXmlStandalone(true);

    return document;
}

From source file:de.betterform.connector.file.FileURIResolver.java

/**
 * Returns a plain file listing as a document.
 *
 * @param directory the directory to list.
 * @return a plain file listing as a document.
 *//* ww  w .  ja v a 2s.  com*/
public static Document buildDirectoryListing(File directory) {
    Document dirList = DOMUtil.newDocument(false, false);
    Element root = dirList.createElement("dir");
    root.setAttribute("path", directory.toURI().toString());
    root.setAttribute("parentDir", directory.getParentFile().toURI().toString());

    File[] fileList = directory.listFiles();
    File file;
    Element element;
    for (int i = 0; i < fileList.length; i++) {
        file = fileList[i];

        if (file.isDirectory()) {
            element = dirList.createElement("dir");
        } else {
            element = dirList.createElement("file");
        }

        element.setAttribute("name", file.getName());
        element.setAttribute("path", file.toURI().toString());
        root.appendChild(element);
    }

    dirList.appendChild(root);
    return dirList;
}

From source file:io.selendroid.server.model.internal.JsonXmlUtil.java

public static Document buildXmlDocument(JSONObject tree) {
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = null;
    try {/*from  ww w  .  j ava2  s.  c o  m*/
        builder = builderFactory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        SelendroidLogger.error("Failed to create documentBuilder", e);
        throw new RuntimeException(e);
    }
    Document document = builder.newDocument();

    // document.setXMLEncoding("UTF-8");
    Element root = document.createElement("views");
    document.appendChild(root);
    buildXmlNode(tree, root, document);
    return document;
}

From source file:Main.java

/**
 * Clones the given DOM node into the given DOM document.
 * /*from ww  w  . j a v  a 2 s .  co m*/
 * @param node
 *            The DOM node to clone.
 * @param doc
 *            The target DOM document.
 * @return The cloned node in the target DOM document.
 */
public static Node cloneNode(Node node, Document doc) {
    Node clone = null;
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
        clone = doc.createElement(node.getNodeName());
        NamedNodeMap attrs = node.getAttributes();
        for (int i = 0; i < attrs.getLength(); i++) {
            Node attrNode = attrs.item(i);
            Attr attrClone = doc.createAttribute(attrNode.getNodeName());
            attrClone.setNodeValue(attrNode.getNodeValue());
            ((Element) clone).setAttributeNode(attrClone);
        }

        // Iterate through each child nodes.
        NodeList childNodes = node.getChildNodes();
        if (childNodes != null) {
            for (int i = 0; i < childNodes.getLength(); i++) {
                Node childNode = childNodes.item(i);
                if (childNode == null) {
                    continue;
                }
                Node childClone = cloneNode(childNode, doc);
                if (childClone == null) {
                    continue;
                }
                clone.appendChild(childClone);
            }
        }
        break;

    case Node.TEXT_NODE:
    case Node.CDATA_SECTION_NODE:
        clone = doc.createTextNode(node.getNodeName());
        clone.setNodeValue(node.getNodeValue());
        break;
    }
    return clone;
}

From source file:Main.java

/**
 * Add a RSS 2.0 Channel Information to a given node - which is your channel node <channel>
 * Note: None of the parameter is supposed to be NULL
 *
 * @param XMLDocument current XML Document
 * @param channelRoot the channel node to which you want the information to be attached to
 * @param title title of your channel/*w  w w  .  ja  va2s  .c om*/
 * @param link  link to your channel home
 * @param description description of your channel
 */
public static void addRSSChannelInformation(Document XMLDocument, Node channelRoot, String title, String link,
        String description) {
    // Title node
    Node entry = XMLDocument.createElement("title");
    entry.appendChild(XMLDocument.createTextNode(title));
    channelRoot.appendChild(entry);

    // Link node
    entry = XMLDocument.createElement("link");
    entry.appendChild(XMLDocument.createTextNode(link));
    channelRoot.appendChild(entry);

    // Description node
    entry = XMLDocument.createElement("description");
    entry.appendChild(XMLDocument.createTextNode(description));
    channelRoot.appendChild(entry);

    // lastBuildDate node
    entry = XMLDocument.createElement("lastBuildDate");
    entry.appendChild(XMLDocument.createTextNode(
            DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL).format(new Date())));
    channelRoot.appendChild(entry);

    // language node
    entry = XMLDocument.createElement("language");
    entry.appendChild(XMLDocument.createTextNode("en-gb"));
    channelRoot.appendChild(entry);
}

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;/*w w w.  ja  v a  2 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);

}