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.vasolsim.common.GenericUtils.java

/**
 * Creates a text node within a created element and then attaches the date to a parent.
 *
 * @param elementName   the name for the new element
 * @param nodeData      the data for the new text node
 * @param parentElement the parent element
 * @param doc           the document (root)
 *///  www  .  java  2s  .co m
public static void appendSubNode(String elementName, String nodeData, Element parentElement, Document doc) {
    Element subElement = doc.createElement(elementName);
    subElement.appendChild(doc.createTextNode(nodeData));
    parentElement.appendChild(subElement);
}

From source file:main.java.vasolsim.common.GenericUtils.java

/**
 * Creates a cdata node within a created element and then attaches the date to a parent.
 *
 * @param elementName   the name for the new element
 * @param nodeData      the data for the new text node
 * @param parentElement the parent element
 * @param doc           the document (root)
 *//*ww w.  j  ava 2  s  .  co  m*/
public static void appendCDATASubNode(String elementName, String nodeData, Element parentElement,
        Document doc) {
    Element subElement = doc.createElement(elementName);
    subElement.appendChild(doc.createCDATASection(nodeData));
    parentElement.appendChild(subElement);
}

From source file:com.photon.phresco.framework.commons.QualityUtil.java

private static void appendTypeProp(Document document, Node parentProp, String tag, String nameAttr,
        String textContent) {/* www  . j  av a 2  s .c  o  m*/
    Node typeProp = document.createElement(tag);
    NamedNodeMap attributes = typeProp.getAttributes();
    attributes.setNamedItem(createAttribute(document, "name", nameAttr));
    typeProp.setTextContent(textContent);
    parentProp.appendChild(typeProp);
}

From source file:Main.java

private static Node replaceTagName(Document document, Node node, String... tags) {

    NodeList nodeList = node.getChildNodes();

    for (int i = 0; i < nodeList.getLength(); i++) {
        Node subNode = nodeList.item(i);
        if (subNode.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }//  w  w  w  . j av a  2s. c o m

        String nodeName = subNode.getNodeName();
        for (int j = 0; j < tags.length / 2; j++) {
            if (nodeName.equals(tags[j])) {
                Element element = document.createElement(tags[j + 1]);
                for (int k = 0; k < subNode.getChildNodes().getLength(); k++) {
                    element.appendChild(subNode.getChildNodes().item(k));
                }

                node.replaceChild(element, subNode);
                subNode = element;
            }
        }

        replaceTagName(document, subNode, tags);
    }

    return node;
}

From source file:com.connexta.arbitro.utils.PolicyUtils.java

/**
 * This method creates the attribute value element
 * @param attributeValueElementDTO attribute value element data object
 * @param doc XML document/*  w ww.jav  a2  s. c  o m*/
 * @return attribute value element
 */
public static Element createAttributeValueElement(AttributeValueElementDTO attributeValueElementDTO,
        Document doc) {

    Element attributeValueElement = doc.createElement(PolicyConstants.ATTRIBUTE_VALUE);

    if (attributeValueElementDTO.getAttributeValue() != null
            && attributeValueElementDTO.getAttributeValue().trim().length() > 0) {

        attributeValueElement.setTextContent(attributeValueElementDTO.getAttributeValue().trim());

        if (attributeValueElementDTO.getAttributeDataType() != null
                && attributeValueElementDTO.getAttributeDataType().trim().length() > 0) {
            attributeValueElement.setAttribute(PolicyConstants.DATA_TYPE,
                    attributeValueElementDTO.getAttributeDataType());
        } else {
            attributeValueElement.setAttribute(PolicyConstants.DATA_TYPE, PolicyConstants.STRING_DATA_TYPE);
        }

    }

    return attributeValueElement;

}

From source file:com.connexta.arbitro.utils.PolicyUtils.java

/**
 * This creates XML representation of Attributes Element using AttributesElementDTO object
 *
 * @param elementDTO  AttributesElementDTO
 * @param doc Document// www . j av a2 s  . c  om
 * @return DOM element
 */
public static Element createAttributesElement(AttributesElementDTO elementDTO, Document doc) {

    Element attributesElement = doc.createElement(PolicyConstants.ATTRIBUTES);

    attributesElement.setAttribute(PolicyConstants.CATEGORY, elementDTO.getCategory());

    List<AttributeElementDTO> attributeElementDTOs = elementDTO.getAttributeElementDTOs();
    if (attributeElementDTOs != null && attributeElementDTOs.size() > 0) {
        for (AttributeElementDTO attributeElementDTO : attributeElementDTOs) {
            Element attributeElement = doc.createElement(PolicyConstants.ATTRIBUTE);
            attributeElement.setAttribute(PolicyConstants.ATTRIBUTE_ID, attributeElementDTO.getAttributeId());
            attributeElement.setAttribute(PolicyConstants.INCLUDE_RESULT,
                    Boolean.toString(attributeElementDTO.isIncludeInResult()));

            if (attributeElementDTO.getIssuer() != null
                    && attributeElementDTO.getIssuer().trim().length() > 0) {
                attributeElement.setAttribute(PolicyConstants.ISSUER, attributeElementDTO.getIssuer());
            }

            List<String> values = attributeElementDTO.getAttributeValues();
            for (String value : values) {
                Element attributeValueElement = doc.createElement(PolicyConstants.ATTRIBUTE_VALUE);
                attributeValueElement.setAttribute(PolicyConstants.DATA_TYPE,
                        attributeElementDTO.getDataType());
                attributeValueElement.setTextContent(value.trim());
                attributeElement.appendChild(attributeValueElement);
            }
            attributesElement.appendChild(attributeElement);
        }
    }
    return attributesElement;
}

From source file:com.connexta.arbitro.utils.PolicyUtils.java

/**
 * This creates XML representation of attribute selector Element using AttributeSelectorDTO object
 *
 * @param attributeSelectorDTO  AttributeSelectorDTO
 * @param doc Document/*from ww w .  j  a  va  2  s  . c  om*/
 * @return DOM element
 */
public static Element createAttributeSelectorElement(AttributeSelectorDTO attributeSelectorDTO, Document doc) {

    Element attributeSelectorElement = doc.createElement(PolicyConstants.ATTRIBUTE_SELECTOR);

    if (attributeSelectorDTO.getAttributeSelectorRequestContextPath() != null
            && attributeSelectorDTO.getAttributeSelectorRequestContextPath().trim().length() > 0) {

        attributeSelectorElement.setAttribute(PolicyConstants.REQUEST_CONTEXT_PATH,
                PolicyConstants.ATTRIBUTE_NAMESPACE
                        + attributeSelectorDTO.getAttributeSelectorRequestContextPath());

        if (attributeSelectorDTO.getAttributeSelectorDataType() != null
                && attributeSelectorDTO.getAttributeSelectorDataType().trim().length() > 0) {
            attributeSelectorElement.setAttribute(PolicyConstants.DATA_TYPE,
                    attributeSelectorDTO.getAttributeSelectorDataType());
        } else {
            attributeSelectorElement.setAttribute(PolicyConstants.DATA_TYPE, PolicyConstants.STRING_DATA_TYPE);
        }

        if (attributeSelectorDTO.getAttributeSelectorMustBePresent() != null
                && attributeSelectorDTO.getAttributeSelectorMustBePresent().trim().length() > 0) {
            attributeSelectorElement.setAttribute(PolicyConstants.MUST_BE_PRESENT,
                    attributeSelectorDTO.getAttributeSelectorMustBePresent());
        }

    }

    return attributeSelectorElement;
}

From source file:com.photon.phresco.framework.commons.QualityUtil.java

private static void appendElementProp(Document document, Node parentNode, String contextType,
        String contextPostData) { // eleme prop
    Node elementProp = document.createElement("elementProp");
    NamedNodeMap attributes = elementProp.getAttributes();

    attributes.setNamedItem(createAttribute(document, "name", "HTTPsampler.Arguments"));
    attributes.setNamedItem(createAttribute(document, "elementType", "Arguments"));
    attributes.setNamedItem(createAttribute(document, "guiclass", "HTTPArgumentsPanel"));
    attributes.setNamedItem(createAttribute(document, "testclass", "Arguments"));
    attributes.setNamedItem(createAttribute(document, "testname", "User Defined Variables"));
    attributes.setNamedItem(createAttribute(document, "enabled", "true"));
    appendCollectionProp(document, elementProp, contextType, contextPostData);

    //parentNode.setTextContent(null);
    parentNode.appendChild(elementProp);
}

From source file:bridge.toolkit.commands.S1000DConverter.java

/**
 * Receive the node, the DOM object and the new name of the node
 * //from   ww w  .j  a v  a 2 s  .  c om
 * @param nodo
 * @param doc
 * @param newname
 */
public static Node changeNodename(Node nodo, org.w3c.dom.Document doc, String newname) {
    // Create an element with the new name
    Node element2 = doc.createElement(newname);

    // Copy the attributes to the new element
    NamedNodeMap attrs = nodo.getAttributes();
    for (int i = 0; i < attrs.getLength(); i++) {
        Attr attr2 = (Attr) doc.importNode(attrs.item(i), true);
        element2.getAttributes().setNamedItem(attr2);
    }

    // Move all the children
    while (nodo.hasChildNodes()) {
        element2.appendChild(nodo.getFirstChild());
    }

    // Replace the old node with the new node
    nodo.getParentNode().replaceChild(element2, nodo);
    return element2;

}

From source file:com.photon.phresco.framework.commons.QualityUtil.java

private static Node appendJdbcSampler(Document document, Node hashTree, String name, String dataSource,
        String queryType, String query) {
    Node jdbcSampler = document.createElement("JDBCSampler");

    NamedNodeMap attributes = jdbcSampler.getAttributes();
    attributes.setNamedItem(createAttribute(document, "guiclass", "TestBeanGUI"));
    attributes.setNamedItem(createAttribute(document, "testclass", "JDBCSampler"));
    attributes.setNamedItem(createAttribute(document, "testname", name)); //url name
    attributes.setNamedItem(createAttribute(document, "enabled", "true"));

    appendTypeProp(document, jdbcSampler, "stringProp", "dataSource", dataSource);
    appendTypeProp(document, jdbcSampler, "stringProp", "queryType", queryType);
    appendTypeProp(document, jdbcSampler, "stringProp", "query", query);
    appendTypeProp(document, jdbcSampler, "stringProp", "queryArguments", null);
    appendTypeProp(document, jdbcSampler, "stringProp", "queryArgumentsTypes", null);
    appendTypeProp(document, jdbcSampler, "stringProp", "variableNames", null);
    appendTypeProp(document, jdbcSampler, "stringProp", "resultVariable", null);

    return jdbcSampler;
}