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 addApp(String file, String name, Hashtable<String, String> attri) throws Exception {
    DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
    DocumentBuilder dombuilder = domfac.newDocumentBuilder();
    FileInputStream is = new FileInputStream(file);

    Document doc = dombuilder.parse(is);

    NodeList nodeList = doc.getElementsByTagName("app");
    if (nodeList != null && nodeList.getLength() >= 1) {
        Node deviceNode = nodeList.item(0);
        Element device = doc.createElement("aut");
        device.setTextContent(name);//w w w  .j  av a2 s.c  om
        for (Iterator itrName = attri.keySet().iterator(); itrName.hasNext();) {
            String attriKey = (String) itrName.next();
            String attriValue = (String) attri.get(attriKey);
            device.setAttribute(attriKey, attriValue);
        }
        deviceNode.appendChild(device);
    }

    //save
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer();
    Properties props = t.getOutputProperties();
    props.setProperty(OutputKeys.ENCODING, "GB2312");
    t.setOutputProperties(props);
    DOMSource dom = new DOMSource(doc);
    StreamResult sr = new StreamResult(file);
    t.transform(dom, sr);
}

From source file:Main.java

public static void addHandset(String file, String name, Hashtable<String, String> attri) throws Exception {
    DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
    DocumentBuilder dombuilder = domfac.newDocumentBuilder();
    FileInputStream is = new FileInputStream(file);

    Document doc = dombuilder.parse(is);
    NodeList nodeList = doc.getElementsByTagName("devices");
    if (nodeList != null && nodeList.getLength() >= 1) {
        Node deviceNode = nodeList.item(0);
        Element device = doc.createElement("device");
        device.setTextContent(name);/*from   www. j av a2  s  .c  o  m*/
        for (Iterator itrName = attri.keySet().iterator(); itrName.hasNext();) {
            String attriKey = (String) itrName.next();
            String attriValue = (String) attri.get(attriKey);
            device.setAttribute(attriKey, attriValue);
        }
        deviceNode.appendChild(device);
    }

    //save
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer();
    Properties props = t.getOutputProperties();
    props.setProperty(OutputKeys.ENCODING, "GB2312");
    t.setOutputProperties(props);
    DOMSource dom = new DOMSource(doc);
    StreamResult sr = new StreamResult(file);
    t.transform(dom, sr);
}

From source file:com.nridge.core.base.std.XMLUtl.java

public static void setAttrBoolValue(Element anElement, String aName, boolean aFlag) {
    if (StringUtils.isNotEmpty(aName)) {
        if (aFlag)
            anElement.setAttribute(aName, XML_UPPER_YES);
        else//w  w w  .jav a  2 s  .c  o  m
            anElement.setAttribute(aName, XML_UPPER_NO);
    }
}

From source file:edu.kit.dama.mdm.content.util.DublinCoreHelper.java

/**
 * Create the Dublin Core document.//from  ww w.  j a  va 2s.c o  m
 *
 * @param theObject The object to create the DC information for.
 * @param pCreator A custom creator stored as author/publisher in Dublin
 * Core. If not provided, the object's uploader is used if available.
 *
 * @return The Dublin Core Document.
 *
 * @throws ParserConfigurationException If creating the Dublin Core document
 * failed.
 */
public static Document createDublinCoreDocument(DigitalObject theObject, UserData pCreator)
        throws ParserConfigurationException {
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.newDocument();

    Element root = doc.createElementNS("http://www.openarchives.org/OAI/2.0/oai_dc/", "oai_dc:dc");
    root.setAttribute("xmlns:dc", "http://purl.org/dc/elements/1.1/");
    root.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
    root.setAttribute("xsi:schemaLocation",
            "http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd");

    doc.appendChild(root);

    Element title = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:title");
    title.setTextContent(StringEscapeUtils.escapeXml11(theObject.getLabel()));
    root.appendChild(title);

    if (pCreator != null) {
        Element creator = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:creator");
        Element publisher = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:publisher");
        creator.setTextContent(StringEscapeUtils.escapeXml11(pCreator.getFullname()));
        publisher.setTextContent(StringEscapeUtils.escapeXml11(pCreator.getFullname()));
        root.appendChild(creator);
        root.appendChild(publisher);
    } else if (theObject.getUploader() != null) {
        Element creator = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:creator");
        Element publisher = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:publisher");
        creator.setTextContent(StringEscapeUtils.escapeXml11(theObject.getUploader().getFullname()));
        publisher.setTextContent(StringEscapeUtils.escapeXml11(theObject.getUploader().getFullname()));
        root.appendChild(creator);
        root.appendChild(publisher);
    }

    for (UserData experimenter : theObject.getExperimenters()) {
        //don't list uploader a second time here
        if (theObject.getUploader() == null || !experimenter.equals(theObject.getUploader())) {
            Element contributor = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:contributor");
            contributor.setTextContent(StringEscapeUtils.escapeXml11(experimenter.getFullname()));
            root.appendChild(contributor);
        }
    }

    if (theObject.getInvestigation() != null) {
        Element subject = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:subject");
        subject.setTextContent(StringEscapeUtils.escapeXml11(theObject.getInvestigation().getTopic()));
        root.appendChild(subject);
        if (theObject.getInvestigation().getDescription() != null) {
            Element description = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:description");
            description.setTextContent(
                    StringEscapeUtils.escapeXml11(theObject.getInvestigation().getDescription()));
            root.appendChild(description);
        }

        if (theObject.getInvestigation().getStudy() != null) {
            if (theObject.getInvestigation().getStudy().getLegalNote() != null) {
                Element rights = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:rights");
                rights.setTextContent(
                        StringEscapeUtils.escapeXml11(theObject.getInvestigation().getStudy().getLegalNote()));
                root.appendChild(rights);
            }
        }
    }

    if (theObject.getStartDate() != null) {
        Element date = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:date");
        date.setTextContent(df.format(theObject.getStartDate()));
        root.appendChild(date);
    }
    Element format = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:format");
    format.setTextContent("application/octet-stream");
    root.appendChild(format);
    Element type = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:type");
    type.setTextContent("Dataset");
    root.appendChild(type);
    Element identifier = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:identifier");
    identifier.setTextContent(
            StringEscapeUtils.escapeXml11(theObject.getDigitalObjectId().getStringRepresentation()));
    root.appendChild(identifier);
    return doc;
}

From source file:com.nridge.core.base.std.XMLUtl.java

public static void setAttrStrValue(Element anElement, String aName, String aValue) {
    if ((StringUtils.isNotEmpty(aName)) && (StringUtils.isNotEmpty(aValue)))
        anElement.setAttribute(aName, aValue);
}

From source file:com.nortal.jroad.util.SOAPUtil.java

/**
 * Adds a type attribute to an element as required by the RPC/Encoded binding. Please note, <code>RPC/Encoded</code>
 * has been deprecated a very long time ago. This is only used to provide backwards compatibility to "metateenused".
 * You should never use this in regular services, as <code>RPC/Literal</code> is compatible with
 * <code>RPC/Encoded</code> parsers.
 *
 * @param element The <code>Element</code> to add the type declaration for.
 * @param type Valid xsi type.//from  www.  j  av  a 2 s  . c  om
 */
public static void addTypeAttribute(Element element, String type) {
    if (type != null) {
        element.setAttribute("xsi:type", type);
    }
}

From source file:com.evolveum.midpoint.prism.util.PrismUtil.java

private static void fortifyNamespaceDeclarations(Element definitionElement, Element childElement) {
    Document doc = definitionElement.getOwnerDocument();
    NamedNodeMap attributes = childElement.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
        Attr attr = (Attr) attributes.item(i);
        if (DOMUtil.isNamespaceDefinition(attr)) {
            String prefix = DOMUtil.getNamespaceDeclarationPrefix(attr);
            String namespace = DOMUtil.getNamespaceDeclarationNamespace(attr);
            Element namespaceElement = doc.createElementNS(PrismConstants.A_NAMESPACE.getNamespaceURI(),
                    PrismConstants.A_NAMESPACE.getLocalPart());
            namespaceElement.setAttribute(PrismConstants.A_NAMESPACE_PREFIX, prefix);
            namespaceElement.setAttribute(PrismConstants.A_NAMESPACE_URL, namespace);
            definitionElement.insertBefore(namespaceElement, childElement);
        }//from  ww  w  .ja v a 2s.c om
    }
}

From source file:Main.java

/**
 *  Copy the attributes from n2 to n1.//from  w w w. j a va2 s.  c  om
 *
 *  @param n1 The source of the attributes.
 *  @param n2 What to copy into.
 */
public static void mergeAttributes(Element n1, Element n2) {
    if ((n1 == null) || (n2 == null)) {
        return;
    }
    NamedNodeMap nnm = n2.getAttributes();
    if (nnm == null) {
        return;
    }
    for (int i = 0; i < nnm.getLength(); i++) {
        Attr attr = (Attr) nnm.item(i);
        n1.setAttribute(attr.getNodeName(), attr.getNodeValue());
    }

}

From source file:com.nridge.core.base.std.XMLUtl.java

public static void setAttrLongValue(Element anElement, String aName, long aValue) {
    Long longObject;/*from  w ww .j  av a2s .  c  o  m*/

    if (StringUtils.isNotEmpty(aName)) {
        longObject = aValue;
        anElement.setAttribute(aName, longObject.toString());
    }
}

From source file:Main.java

/**
 * Sets the Map as DOM attributes on the given Element.
 * <p>/*from  w  w w.j av  a  2 s  . c o m*/
 * This implementation uses <code>element.setAttribute</code>. Therefore if the element already
 * has attributes, the new attributes are added amongst them. If attributes with the same name
 * already exist, they are overwritten.
 */

public static void setMapAsAttributes(Element element, Map<String, String> attributes) {

    if (attributes == null) {
        return;
    }

    for (Map.Entry<String, String> entry : attributes.entrySet()) {

        String value = entry.getValue();

        if (value == null) {
            element.removeAttribute(entry.getKey());
            continue;
        }

        element.setAttribute(entry.getKey(), value);
    }
}