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:Main.java

public static void createElementAndAppend(String name, double value, Document doc, Element appendeeElement,
        String attributeName, String attributeValue) {
    Element newElement = doc.createElement(name);
    Text text = doc.createTextNode(String.valueOf(value));
    newElement.appendChild(text);/*  w  w w . jav  a2s. c  om*/
    if (attributeName != null && !attributeName.equals("")) {
        newElement.setAttribute(attributeName, attributeValue);
    }
    appendeeElement.appendChild(newElement);
}

From source file:it.polimi.diceH2020.plugin.control.JSonReader.java

/**
 * set the attribute named as "name" in the node "n" with a given value
 * //from ww w.  ja va 2  s .c o m
 * @param n
 *            the node in which you want to set the attribute
 * @param name
 *            the name of the attribute
 * @param value
 *            the value of the attribute
 */
private static void setAttribute(Node n, String name, String value) {
    NamedNodeMap attributes = n.getAttributes();
    if (!JSonReader.findAttribute(attributes, name)) {
        Element el = (Element) n;
        el.setAttribute(name, value);
    } else {
        Node nodeAttrType1 = attributes.getNamedItem(name);
        nodeAttrType1.setTextContent(value);
    }
}

From source file:Main.java

public static void copyAttr(Element from, Element to) {
    NamedNodeMap nnm = from.getAttributes();
    for (int i = 0; i < nnm.getLength(); ++i) {
        String name = nnm.item(i).getNodeName();
        if (!to.hasAttribute(name)) {
            String value = nnm.item(i).getNodeValue();
            to.setAttribute(name, value);
        }//  w  ww . ja  va  2s.  c  o  m
    }
}

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

private static void setLinks(final Element entry, final List<AtomLink> links)
        throws ParserConfigurationException {
    for (AtomLink link : links) {
        final Element linkElem = entry.getOwnerDocument().createElement(ODataConstants.ATOM_ELEM_LINK);

        linkElem.setAttribute(ODataConstants.ATTR_REL, link.getRel());
        linkElem.setAttribute(ODataConstants.ATTR_TITLE, link.getTitle());
        linkElem.setAttribute(ODataConstants.ATTR_HREF, link.getHref());

        if (StringUtils.isNotBlank(link.getType())) {
            linkElem.setAttribute(ODataConstants.ATTR_TYPE, link.getType());
        }/*from w w  w.  j  a va2 s.  c  om*/

        if (link.getInlineEntry() != null || link.getInlineFeed() != null) {
            final Element inline = entry.getOwnerDocument().createElement(ODataConstants.ATOM_ELEM_INLINE);
            linkElem.appendChild(inline);

            if (link.getInlineEntry() != null) {
                inline.appendChild(
                        entry.getOwnerDocument().importNode(entry((AtomEntry) link.getInlineEntry()), true));
            }
            if (link.getInlineFeed() != null) {
                inline.appendChild(
                        entry.getOwnerDocument().importNode(feed((AtomFeed) link.getInlineFeed()), true));
            }
        }

        entry.appendChild(linkElem);
    }
}

From source file:Main.java

/**
 * Method to change update date in xml files - used for reloadable issue and auto change on start up.
 * /*from w w  w  .jav a 2  s .  c o  m*/
 * @param target document
 * @return updated document
 * @throws ParseException
 */
private static Document changeDate(Document target) throws ParseException {
    NodeList parents = target.getElementsByTagName("g:options");
    Element parent = (Element) parents.item(0); //g:options - only 1 element
    DateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.ENGLISH);
    Date newT = new Date();
    String time = format.format(newT);
    parent.setAttribute("time", time);
    return target;
}

From source file:fr.ece.epp.tools.Utils.java

public static void updateProduct(String path, String[] feature, boolean outOrno) {
    Document document = null;/*w w w.j  a v a  2s  . c o  m*/
    try {
        document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(path);
        Element root = document.getDocumentElement();
        Node features = root.getElementsByTagName("features").item(0);
        Element basefeature = document.createElement("feature");
        basefeature.setAttribute("id", "org.eclipse.platform");
        features.appendChild(basefeature);
        for (int i = 0; i < feature.length; i++) {
            if (feature[i] != null && feature[i].trim().length() > 0) {
                Element fea = document.createElement("feature");
                if (feature[i].endsWith(".feature.group")) {
                    int count = feature[i].length() - ".featyre.group".length();
                    String id = feature[i].substring(0, count);
                    fea.setAttribute("id", id);
                } else {
                    fea.setAttribute("id", feature[i]);
                }
                features.appendChild(fea);
            }
        }
        output(root, path);
        if (outOrno) {
            output(root, null);
        }

    } catch (SAXException e) {
    } catch (IOException e) {
    } catch (ParserConfigurationException e) {
    }
}

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

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

    final Element entryElem = doc.createElement(ODataConstants.ATOM_ELEM_ENTRY);
    entryElem.setAttribute(XMLConstants.XMLNS_ATTRIBUTE, ODataConstants.NS_ATOM);
    entryElem.setAttribute(ODataConstants.XMLNS_METADATA, ODataConstants.NS_METADATA);
    entryElem.setAttribute(ODataConstants.XMLNS_DATASERVICES, ODataConstants.NS_DATASERVICES);
    entryElem.setAttribute(ODataConstants.XMLNS_GML, ODataConstants.NS_GML);
    entryElem.setAttribute(ODataConstants.XMLNS_GEORSS, ODataConstants.NS_GEORSS);
    if (entry.getBaseURI() != null) {
        entryElem.setAttribute(ODataConstants.ATTR_XMLBASE, entry.getBaseURI().toASCIIString());
    }/*from   w w  w  .  j  a  va 2 s.  co  m*/
    doc.appendChild(entryElem);

    final Element category = doc.createElement(ODataConstants.ATOM_ELEM_CATEGORY);
    category.setAttribute(ODataConstants.ATOM_ATTR_TERM, entry.getType());
    category.setAttribute(ODataConstants.ATOM_ATTR_SCHEME, ODataConstants.ATOM_CATEGORY_SCHEME);
    entryElem.appendChild(category);

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

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

    setLinks(entryElem, entry.getAssociationLinks());
    setLinks(entryElem, entry.getNavigationLinks());
    setLinks(entryElem, entry.getMediaEditLinks());

    final Element content = doc.createElement(ODataConstants.ATOM_ELEM_CONTENT);
    if (entry.isMediaEntry()) {
        if (StringUtils.isNotBlank(entry.getMediaContentType())) {
            content.setAttribute(ODataConstants.ATTR_TYPE, entry.getMediaContentType());
        }
        if (StringUtils.isNotBlank(entry.getMediaContentSource())) {
            content.setAttribute(ODataConstants.ATOM_ATTR_SRC, entry.getMediaContentSource());
        }
        if (content.getAttributes().getLength() > 0) {
            entryElem.appendChild(content);
        }

        if (entry.getMediaEntryProperties() != null) {
            entryElem.appendChild(doc.importNode(entry.getMediaEntryProperties(), true));
        }
    } else {
        content.setAttribute(ODataConstants.ATTR_TYPE, ContentType.APPLICATION_XML.getMimeType());
        if (entry.getContent() != null) {
            content.appendChild(doc.importNode(entry.getContent(), true));
        }
        entryElem.appendChild(content);
    }

    return entryElem;
}

From source file:Main.java

/**
 * Adds an attribute with the list of integer values encoded as a string.
 * @param element        the element to which the attribute will be added
 * @param attributeName  the name of the attribute
 * @param intValues      the list of Integer values to set
 *//*from   w  w w . j av a  2  s . c  om*/
public static void setIntegerListAttribute(Element element, String attributeName, List<Integer> intValues) {
    StringBuilder sb = new StringBuilder();
    for (Integer intValue : intValues) {
        if (sb.length() == 0) {
            sb.append(" "); //$NON-NLS-1$
        }
        sb.append(intValue);
    }

    element.setAttribute(attributeName, sb.toString());
}

From source file:com.photon.phresco.impl.WindowsApplicationProcessor.java

private static void createNewItemGroup(Document doc, List<ArtifactGroup> artifactGroups) {
    Element project = doc.getDocumentElement();
    Element itemGroup = doc.createElement(ITEMGROUP);
    for (ArtifactGroup artifactGroup : artifactGroups) {
        if (artifactGroup.getType().name().equals(Type.FEATURE.name())) {
            Element reference = doc.createElement(REFERENCE);
            reference.setAttribute(INCLUDE, artifactGroup.getName());
            Element hintPath = doc.createElement(HINTPATH);
            hintPath.setTextContent(DOUBLE_DOT + COMMON + File.separator + artifactGroup.getName() + DLL);
            reference.appendChild(hintPath);
            itemGroup.appendChild(reference);
        }/*ww  w.ja va2  s  . c om*/
    }
    project.appendChild(itemGroup);
}

From source file:Main.java

/**
 * * Convenience method to transfer a node (and all of its children) from one
 * * DOM XML document to another./*from  ww w.j a  v  a 2s . c  o m*/
 * *
 * * Note: this method is recursive.
 * *
 * * @param current the current Element to append the transfer to
 * * @param target the target document for the transfer
 * * @param n the Node to transfer
 * * @return Element the current element.
 */
public static Element transferNode(Element current, Document target, Node n) {
    String name = n.getNodeName();
    String value = n.getNodeValue();
    short type = n.getNodeType();

    if (type == Node.ELEMENT_NODE) {
        // create a new element for this node in the target document
        Element e = target.createElement(name);

        // move all the attributes over to the target document
        NamedNodeMap attrs = n.getAttributes();
        for (int i = 0; i < attrs.getLength(); i++) {
            Node a = attrs.item(i);
            e.setAttribute(a.getNodeName(), a.getNodeValue());
        }

        // get the children for this node
        NodeList children = n.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            // transfer each of the children to the new document
            transferNode(e, target, children.item(i));
        }

        // append the node to the target document element
        current.appendChild(e);
    } else if (type == Node.TEXT_NODE) {
        Text text = target.createTextNode(value);
        current.appendChild(text);
    }

    return current;
}