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 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;
}

From source file:Main.java

public static Element appendSingleValElement(Document owner, Element appendElement, String newElemName,
        String newElemVal) {/*from   w w w.  j a v a  2 s .  c  o m*/
    Element newElem;
    newElem = owner.createElement(newElemName);
    if (newElemVal != null) {
        Text value = owner.createTextNode(newElemVal);
        newElem.appendChild(value);
    }
    appendElement.appendChild(newElem);
    return (newElem);
}

From source file:Main.java

/**
 * Create an root element with the specified name
 *
 * @param doc//from   ww w  .ja  va2 s.  co  m
 * @param rootTagName
 * @return
 */
public static Element createRootElement(Document doc, String rootTagName) {
    if (doc.getDocumentElement() == null) {
        Element root = doc.createElement(rootTagName);
        doc.appendChild(root);
        return root;
    }
    return doc.getDocumentElement();
}

From source file:com.sonar.maven.it.ItUtils.java

private static Element createRepositories(Document doc) {
    Element repositories = doc.createElement("repositories");
    Element repository = doc.createElement("repository");
    Element id = doc.createElement("id");
    Element url = doc.createElement("url");

    id.setTextContent("sonarsource");
    url.setTextContent("https://repox.sonarsource.com/sonarsource");

    repositories.appendChild(repository);
    repository.appendChild(id);/*from   www .  j  ava  2 s  .  co m*/
    repository.appendChild(url);

    return repositories;
}

From source file:com.sonar.maven.it.ItUtils.java

private static Element createPluginRepositories(Document doc) {
    Element repositories = doc.createElement("pluginRepositories");
    Element repository = doc.createElement("pluginRepository");
    Element id = doc.createElement("id");
    Element url = doc.createElement("url");

    id.setTextContent("sonarsource");
    url.setTextContent("https://repox.sonarsource.com/sonarsource");

    repositories.appendChild(repository);
    repository.appendChild(id);/*from ww  w.j  a v a 2s .  c o  m*/
    repository.appendChild(url);

    return repositories;
}

From source file:Main.java

public static Node duplicate(Node node, Document ownerDocument) {
    if (node instanceof Text)
        return ownerDocument.createTextNode(node.getNodeValue());
    Node newNode = ownerDocument.createElement(node.getNodeName());
    NamedNodeMap attribs = node.getAttributes();
    for (int i = 0; i < attribs.getLength(); i++) {
        Node attrib = attribs.item(i);
        addAttribute(newNode, attrib.getNodeName(), attrib.getNodeValue());
    }/*from w ww  .ja  v a2  s  .  com*/
    for (Node n = node.getFirstChild(); n != null; n = n.getNextSibling()) {
        Node newN = duplicate(n, ownerDocument);
        newNode.appendChild(newN);
    }
    return newNode;
}

From source file:Main.java

public static Element getAddressTypeWithText(Document doc, String street, String city, String state,
        String zip) {//  w w w.  ja  va  2 s . co  m

    Element elem_address = doc.createElement("Address");

    Element elem_street = getElementWithText(doc, "Street", street);
    Element elem_city = getElementWithText(doc, "City", city);
    Element elem_state = getElementWithText(doc, "State", state);
    Element elem_zip = getElementWithText(doc, "Zip", zip);

    elem_address.appendChild(elem_street);
    elem_address.appendChild(elem_city);
    elem_address.appendChild(elem_state);
    elem_address.appendChild(elem_zip);

    return elem_address;
}

From source file:Main.java

public static Element addChildElement(Node parent, String elementName) {
    Document ownerDocument = parent.getOwnerDocument();
    Objects.requireNonNull(ownerDocument, "nodes ownerDocument " + parent);
    Element element = ownerDocument.createElement(elementName);
    parent.appendChild(element);//from   w w  w.  ja v  a  2 s. c  o  m
    return element;
}

From source file:Main.java

public static void addElementToXml(String xmlFile, String nodeToAdd, String nodeContent)
        throws ParserConfigurationException, SAXException, IOException, TransformerException {

    Document doc = getXmlDoc(xmlFile);

    NodeList nodes = doc.getChildNodes();

    Node node = doc.createElement(nodeToAdd);
    node.setNodeValue(nodeContent);//from w  ww  .j ava  2  s. c  o m

    nodes.item(0).appendChild(node);

    saveXml(doc, xmlFile);
}

From source file:Main.java

public static Element appendSingleValElementEncoded(Document owner, Element appendElement, String newElemName,
        String newElemVal) {//  w  ww. java  2 s  .  com
    Element newElem;
    newElem = owner.createElement(newElemName);
    if (newElemVal != null) {
        String encodedVal = "";
        try {
            encodedVal = URLEncoder.encode(newElemVal, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Text value = owner.createTextNode(encodedVal);
        newElem.appendChild(value);
        newElem.setAttribute("enc", "t");
        newElem.setAttribute("charSet", "UTF-8");
    }
    appendElement.appendChild(newElem);
    return (newElem);
}