Example usage for org.w3c.dom Element setAttribute

List of usage examples for org.w3c.dom Element setAttribute

Introduction

In this page you can find the example usage for org.w3c.dom Element setAttribute.

Prototype

public void setAttribute(String name, String value) throws DOMException;

Source Link

Document

Adds a new attribute.

Usage

From source file:cz.incad.kramerius.rest.api.k5.client.utils.SOLRUtils.java

public static Element value(Document doc, String attname, String val) {
    synchronized (doc) {
        Element strElm = doc.createElement("str");
        if (attname != null)
            strElm.setAttribute("name", attname);
        strElm.setTextContent(val);
        return strElm;
    }/*from w  w w  .j  a va 2s. c  o  m*/
}

From source file:cz.incad.kramerius.rest.api.k5.client.utils.SOLRUtils.java

public static Element value(Document doc, String attname, Integer val) {
    synchronized (doc) {
        Element strElm = doc.createElement("int");
        if (attname != null)
            strElm.setAttribute("name", attname);
        strElm.setTextContent("" + val);
        return strElm;
    }//www .jav a2  s . c o m
}

From source file:Main.java

public static void setXsdSchema(Node node, String schemaURL) {
    Document doc;//from w ww  .  j  a v  a  2s  .  co m
    if (node.getNodeType() != Node.DOCUMENT_NODE) {
        doc = node.getOwnerDocument();
    } else {
        doc = (Document) node;
    }
    Element root = doc.getDocumentElement();
    if (schemaURL == null) {
        root.removeAttribute("xmlns:xsi");
        root.removeAttribute("xsi:noNamespaceSchemaLocation");
    } else {
        root.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
        root.setAttribute("xsi:noNamespaceSchemaLocation", schemaURL);
    }
}

From source file:Main.java

public static void addAttributes(Element element, Map<String, Object> map) {
    if (map != null) {
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            Object attrValue = entry.getValue();
            element.setAttribute(entry.getKey(), attrValue == null ? null : attrValue.toString());
        }/*from  w  w w  .j  av  a 2  s. c  o m*/
    }
}

From source file:cz.incad.kramerius.rest.api.k5.client.utils.SOLRUtils.java

public static Element arr(Document doc, String attname, List vals) {
    synchronized (doc) {
        Element arrElm = doc.createElement("arr");
        if (attname != null)
            arrElm.setAttribute("name", attname);
        for (Object obj : vals) {
            if (obj instanceof String) {
                arrElm.appendChild(value(doc, (String) obj));
            } else if (obj instanceof Integer) {
                arrElm.appendChild(value(doc, (Integer) obj));
            } else
                throw new IllegalArgumentException("unsupported type " + obj.getClass().getName() + "");
        }//from   ww  w.ja va 2  s  .  c o m
        return arrElm;
    }
}

From source file:com.msopentech.odatajclient.engine.data.atom.AtomSerializer.java

private static Element feed(final AtomFeed feed) throws ParserConfigurationException {
    final DocumentBuilder builder = ODataConstants.DOC_BUILDER_FACTORY.newDocumentBuilder();
    final Document doc = builder.newDocument();

    final Element feedElem = doc.createElement(ODataConstants.ATOM_ELEM_FEED);
    feedElem.setAttribute(XMLConstants.XMLNS_ATTRIBUTE, ODataConstants.NS_ATOM);
    feedElem.setAttribute(ODataConstants.XMLNS_METADATA, ODataConstants.NS_METADATA);
    feedElem.setAttribute(ODataConstants.XMLNS_DATASERVICES, ODataConstants.NS_DATASERVICES);
    feedElem.setAttribute(ODataConstants.XMLNS_GML, ODataConstants.NS_GML);
    feedElem.setAttribute(ODataConstants.XMLNS_GEORSS, ODataConstants.NS_GEORSS);
    if (feed.getBaseURI() != null) {
        feedElem.setAttribute(ODataConstants.ATTR_XMLBASE, feed.getBaseURI().toASCIIString());
    }/*  w w  w.  j  a v a 2 s  . c o m*/
    doc.appendChild(feedElem);

    if (StringUtils.isNotBlank(feed.getTitle())) {
        final Element title = doc.createElement(ODataConstants.ATOM_ELEM_TITLE);
        title.appendChild(doc.createTextNode(feed.getTitle()));
        feedElem.appendChild(title);
    }

    if (StringUtils.isNotBlank(feed.getSummary())) {
        final Element summary = doc.createElement(ODataConstants.ATOM_ELEM_SUMMARY);
        summary.appendChild(doc.createTextNode(feed.getSummary()));
        feedElem.appendChild(summary);
    }

    for (AtomEntry entry : feed.getEntries()) {
        feedElem.appendChild(doc.importNode(entry(entry), true));
    }

    return feedElem;
}

From source file:Main.java

/**
 * Copy elements from one document to another attaching at the specified
 * element and translating the namespace.
 *
 * @param from         copy the children of this element (exclusive)
 * @param to           where to attach the copied elements
 * @param newNamespace destination namespace
 *
 * @since 8.4//from   w  w  w.j a  va 2  s .co  m
 */
public static void copyDocument(Element from, Element to, String newNamespace) {
    Document doc = to.getOwnerDocument();
    NodeList nl = from.getChildNodes();
    int length = nl.getLength();
    for (int i = 0; i < length; i++) {
        Node node = nl.item(i);
        Node newNode = null;
        if (Node.ELEMENT_NODE == node.getNodeType()) {
            Element oldElement = (Element) node;
            newNode = doc.createElementNS(newNamespace, oldElement.getTagName());
            NamedNodeMap m = oldElement.getAttributes();
            Element newElement = (Element) newNode;
            for (int index = 0; index < m.getLength(); index++) {
                Node attr = m.item(index);
                newElement.setAttribute(attr.getNodeName(), attr.getNodeValue());
            }
            copyDocument(oldElement, newElement, newNamespace);
        } else {
            newNode = node.cloneNode(true);
            newNode = to.getOwnerDocument().importNode(newNode, true);
        }
        if (newNode != null) {
            to.appendChild(newNode);
        }
    }
}

From source file:Main.java

public static void createElementAndAppend(String name, String value, Document doc, Element appendeeElement,
        String attributeName, String attributeValue) {
    if (value == null || value.equals("")) {

        log.info("XMLUtil.createElementAndAppend()  value == null for name = " + name + ".");
        value = "";
    }/*  w  w  w . jav a  2 s  .  c  om*/
    Element newElement = doc.createElement(name);
    Text text = doc.createTextNode(value);
    newElement.appendChild(text);
    if (attributeName != null && !attributeName.equals("")) {
        newElement.setAttribute(attributeName, attributeValue);
    }

    appendeeElement.appendChild(newElement);
}

From source file:Main.java

private static Node createChildInternal(Document document, Node parent, String nodeName,
        String... attr_name_and_value) {
    Element newNode = document.createElement(nodeName);

    for (int i = 0; i < attr_name_and_value.length; i += 2) {
        String attrName = attr_name_and_value[i];
        String attrValue = attr_name_and_value[i + 1];
        newNode.setAttribute(attrName, attrValue);
    }//from   w  w w .  j a  va 2 s  . c o m
    parent.appendChild(newNode);
    return newNode;
}

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);/*from w  w w  .  j av  a  2 s . co  m*/
    if (attributeName != null && !attributeName.equals("")) {
        newElement.setAttribute(attributeName, attributeValue);
    }
    appendeeElement.appendChild(newElement);
}