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

public static String createNamespace(Element el, String ns) {
    String p = "ns1";
    int i = 1;// w  w  w . jav  a2 s.  co  m
    while (getPrefix(el, ns) != null) {
        p = "ns" + i;
        i++;
    }
    el.setAttribute(XMLNAMESPACE + ":" + p, ns);
    return p;
}

From source file:Main.java

public static void dupAttributes(Document doc) {
    Element root = doc.getDocumentElement();
    Element personOne = (Element) root.getFirstChild();

    Attr deptAttr = personOne.getAttributeNode("dept");
    personOne.removeAttributeNode(deptAttr);
    String deptString = deptAttr.getValue();
    personOne.setAttribute("dept", deptString + "updated");

    String mailString = personOne.getAttribute("mail");
    personOne.setAttribute("mail", mailString + "updated");

    String titleString = personOne.getAttribute("title");
    //personOne.removeAttribute("title");
    personOne.setAttribute("title", titleString + "updated");
}

From source file:Main.java

/**
 * Convenience function for creating elements.
 * /*from  ww  w  .  j  av  a  2s .co m*/
 * @param document
 *            the DOM document we're creating the element in
 * @param name
 *            the tagname for the element
 * @param attributes
 *            a map from attribute names to attributes, or <CODE>null</CODE>
 *            if this element should have no attributes
 * @param text
 *            the text for the element, which will be made into a text node
 *            and added as a child of the created element, or <CODE>null</CODE>
 *            if the element should have no children
 * @return a new element
 */
public static Element createElement(Document doc, String name, Object text, Map<String, Object> attributes) {
    Element e = doc.createElement(name);
    // Set the attributes.
    if (attributes != null) {
        for (Entry<String, Object> entry : attributes.entrySet()) {
            e.setAttribute(entry.getKey(), entry.getValue().toString());
        }
    }
    // Add the text element.
    if (text != null)
        e.appendChild(createTextNode(doc, text.toString()));
    return e;
}

From source file:Main.java

static public boolean addNamespaceURIs(Document doc, Map<String, String> nsMap) {
    Element root = doc.getDocumentElement();
    if (root == null)
        return false;
    // Add Namespace attributes
    for (Entry<String, String> entry : nsMap.entrySet()) {
        root.setAttribute("xmlns:" + entry.getKey(), entry.getValue());
    }/*from   w w  w  . j a va 2 s  .  co  m*/
    return true;
}

From source file:com.bluexml.side.Integration.alfresco.xforms.webscript.XmlBuilder.java

private static Element buildAssociation(Document doc, AssociationBean associationBean) {
    // association
    Element association = doc.createElement(ENTRY_ASSOCIATION_NODE);
    association.setAttribute(ENTRY_QUALIFIEDNAME, associationBean.getAssociationName());
    if (associationBean.getAssoType().equals(AssoType.Simple)) {
        association.setAttribute(ENTRY_ASSOCIATION_TYPE, ENTRY_ASSOCIATION_TYPE_SIMPLE);
    } else {/*from w  w  w. j a v  a 2s  .co m*/
        association.setAttribute(ENTRY_ASSOCIATION_TYPE, ENTRY_ASSOCIATION_TYPE_COMPOSITION);
    }
    // target
    String targetValue = associationBean.getTargetId();
    String targetLabel = associationBean.getTargetLabel();

    String targetNodeType = associationBean.getTargetQualifiedName();
    Element target = doc.createElement(ENTRY_ASSOCIATION_TARGET_NODE);
    target.setAttribute(ENTRY_QUALIFIEDNAME, targetNodeType);
    target.setTextContent(targetValue);
    if (StringUtils.trimToNull(targetLabel) != null) {
        target.setAttribute("label", targetLabel);
    }
    association.appendChild(target);

    return association;
}

From source file:eu.europa.esig.dss.pades.signature.visible.ImageFactory.java

private static void initDpi(IIOMetadata metadata, int dpi) throws IIOInvalidTreeException {
    Element tree = (Element) metadata.getAsTree("javax_imageio_jpeg_image_1.0");
    Element jfif = (Element) tree.getElementsByTagName("app0JFIF").item(0);
    jfif.setAttribute("Xdensity", Integer.toString(dpi));
    jfif.setAttribute("Ydensity", Integer.toString(dpi));
    jfif.setAttribute("resUnits", "1"); // density is dots per inch
    metadata.setFromTree("javax_imageio_jpeg_image_1.0", tree);
}

From source file:com.mirth.connect.server.util.SqlConfig.java

private static void addPluginSqlMaps(String database, DonkeyElement sqlMapConfigElement) throws Exception {
    ExtensionController extensionController = ControllerFactory.getFactory().createExtensionController();
    Map<String, PluginMetaData> plugins = extensionController.getPluginMetaData();

    if (MapUtils.isNotEmpty(plugins)) {
        for (String pluginName : plugins.keySet()) {
            PluginMetaData pmd = plugins.get(pluginName);

            if (extensionController.isExtensionEnabled(pluginName)) {
                // only add configs for plugins that have some configs defined
                if (pmd.getSqlMapConfigs() != null) {
                    /* get the SQL map for the current database */
                    String pluginSqlMapName = pmd.getSqlMapConfigs().get(database);

                    if (StringUtils.isBlank(pluginSqlMapName)) {
                        /*
                         * if we couldn't find one for the current
                         * database, check for one that works with
                         * all databases
                         */// w  w w.  ja v  a  2  s.com
                        pluginSqlMapName = pmd.getSqlMapConfigs().get("all");
                    }

                    if (StringUtils.isNotBlank(pluginSqlMapName)) {
                        File sqlMapConfigFile = new File(
                                ExtensionController.getExtensionsPath() + pmd.getPath(), pluginSqlMapName);
                        Element sqlMapElement = sqlMapConfigElement.addChildElement("mapper");
                        sqlMapElement.setAttribute("url", sqlMapConfigFile.toURI().toURL().toString());
                    } else {
                        throw new RuntimeException("SQL map file not found for database: " + database);
                    }
                }
            }
        }
    }
}

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

private static void buildXmlNode(JSONObject from, Element parent, Document document) {
    if (from == null) {
        return;/*from  w w  w .j  a va2s  .  c  o  m*/
    }

    Element node = document.createElement(extractTagName(from.optString("type")));
    parent.appendChild(node);

    node.setAttribute("name", from.optString("name"));
    node.setAttribute("label", from.optString("label"));
    node.setAttribute("value", from.optString("value"));
    node.setAttribute("ref", from.optString("ref"));
    node.setAttribute("id", from.optString("id"));
    node.setAttribute("shown", from.optString("shown"));

    JSONObject rect = from.optJSONObject("rect");
    if (rect != null) {
        Element rectNode = document.createElement("rect");
        JSONObject size = rect.optJSONObject("size");
        JSONObject origin = rect.optJSONObject("origin");

        rectNode.setAttribute("x", origin.optString("x"));
        rectNode.setAttribute("y", origin.optString("y"));
        rectNode.setAttribute("height", size.optString("height"));
        rectNode.setAttribute("width", size.optString("width"));

        node.appendChild(rectNode);
    }

    JSONArray array = from.optJSONArray("children");
    if (array != null) {
        for (int i = 0; i < array.length(); i++) {
            JSONObject n = array.optJSONObject(i);
            buildXmlNode(n, node, document);
        }
    }
}

From source file:Utils.java

public static Element findElementElseCreateAndAttribute(Document document, Element parent, String element,
        String attributeName, String attributeValue) {
    NodeList nl = parent.getElementsByTagName(element);
    Element e = null;

    if (nl.getLength() == 0) {
        parent.appendChild(document.createElement(element));
        e = (Element) parent.getElementsByTagName(element).item(0);
        e.setAttribute(attributeName, attributeValue);
    }/*from   w  w  w. ja  va  2 s. co m*/

    return e;
}

From source file:Main.java

public static void addNodeToXml(String nodeParentXpathStr, String xmlFilePath, String nodeName, String value,
        Map<String, String> attr) throws Exception {
    Document doc = null;//from www.j a  v  a 2  s  .  co  m
    if (xmlFilePath == null || nodeParentXpathStr == null || nodeName == null || nodeParentXpathStr == null)
        throw new Exception("some parameters can not be null!");
    doc = dombuilder.parse(new File(xmlFilePath));
    Node pNode = (Node) xpath.compile(nodeParentXpathStr).evaluate(doc, XPathConstants.NODE);
    if (pNode == null)
        throw new Exception("can not find the node specified in nodeParentXpathStr!");
    ;

    Element newNode = doc.createElement(nodeName);
    newNode.setTextContent(value);
    if (attr != null && !attr.isEmpty()) {
        for (String key : attr.keySet())
            newNode.setAttribute(key, attr.get(key));
    }

    pNode.appendChild(newNode);
    writeToXmlFile(doc, xmlFilePath);

}