Example usage for org.w3c.dom NamedNodeMap setNamedItem

List of usage examples for org.w3c.dom NamedNodeMap setNamedItem

Introduction

In this page you can find the example usage for org.w3c.dom NamedNodeMap setNamedItem.

Prototype

public Node setNamedItem(Node arg) throws DOMException;

Source Link

Document

Adds a node using its nodeName attribute.

Usage

From source file:Main.java

public static void addAttribute(Document document, Node parent, String attrName, String value) {
    Attr attr = document.createAttribute(attrName);
    attr.setValue(value);//from  w ww  . jav a  2  s  .  c om
    NamedNodeMap map = parent.getAttributes();
    map.setNamedItem(attr);
}

From source file:Main.java

public static void addAttribute(Document doc, String name, Element e, String value) {
    Node attrNode = doc.createAttribute(name);
    attrNode.setNodeValue(value);/*from  w  w  w.j av a 2s .co m*/
    NamedNodeMap attrs = e.getAttributes();
    attrs.setNamedItem(attrNode);
}

From source file:Main.java

public static void addAttribute(Document xmlDoc, Node node, String attrName, String attrValue) {
    Attr newAtt = xmlDoc.createAttribute(attrName);
    newAtt.setNodeValue(attrValue);/*  w ww . j a v a 2s  .c o m*/
    NamedNodeMap attrs = node.getAttributes();
    attrs.setNamedItem(newAtt);
}

From source file:Main.java

/**
 * Add an attribute to an Element.//from   w ww  . ja  va 2s  .c om
 * @param doc XML document
 * @param element XML Element
 * @param attribute attribute name
 * @param value attribute value
 */
public static void addAttribute(final Document doc, final Element element, final String attribute,
        final String value) {

    final NamedNodeMap atts = element.getAttributes();
    final Attr newAttr = doc.createAttribute(attribute);
    newAttr.setValue(value);
    atts.setNamedItem(newAttr);
}

From source file:Main.java

/**
 *  Sets a named attribute of a Node//  w w w . jav a 2 s .  c o  m
 *  @param node The node
 *  @param attr The name of the attribute to set
 *  @param value The value to assign to the attribute
 */
public synchronized static void setAttribute(Node node, String attr, String value) {
    if (node == null)
        throw new IllegalArgumentException("Node argument cannot be null");
    if (attr == null)
        throw new IllegalArgumentException("Node attribute argument cannot be null");
    if (value == null)
        throw new IllegalArgumentException("Node attribute value argument cannot be null");

    Node attrN = null;
    NamedNodeMap map = node.getAttributes();
    if (map != null)
        attrN = map.getNamedItem(attr);

    if (attrN == null) {
        attrN = node.getOwnerDocument().createAttribute(attr);
        map.setNamedItem(attrN);
    }

    attrN.setNodeValue(value);
}

From source file:Main.java

/**
 * Adds a new node to a file./*from   w ww . ja  v  a 2 s . c  om*/
 *
 * @param nodeType {@link String} The type of the element to add.
 * @param idField {@link String} The name of the field used to identify this
 * node.
 * @param nodeID {@link String} The identifier for this node, so its data
 * can be later retrieved and modified.
 * @param destFile {@link File} The file where the node must be added.
 * @param attributes {@link ArrayList} of array of String. The arrays must
 * be bidimensional (first index must contain attribute name, second one
 * attribute value). Otherwise, an error will be thrown. However, if
 * <value>null</value>, it is ignored.
 */
public static void addNode(String nodeType, String idField, String nodeID, File destFile,
        ArrayList<String[]> attributes) {
    if (attributes != null) {
        for (Iterator<String[]> it = attributes.iterator(); it.hasNext();) {
            if (it.next().length != 2) {
                throw new IllegalArgumentException("Invalid attribute combination");
            }
        }
    }
    /*
     * XML DATA CREATION - BEGINNING
     */
    DocumentBuilder docBuilder;
    Document doc;
    try {
        docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        doc = docBuilder.parse(destFile);
    } catch (SAXException | IOException | ParserConfigurationException ex) {
        return;
    }

    Node index = doc.getFirstChild(), newElement = doc.createElement(nodeType);
    NamedNodeMap elementAttributes = newElement.getAttributes();

    Attr attrID = doc.createAttribute(idField);
    attrID.setValue(nodeID);
    elementAttributes.setNamedItem(attrID);

    if (attributes != null) {
        for (Iterator<String[]> it = attributes.iterator(); it.hasNext();) {
            String[] x = it.next();
            Attr currAttr = doc.createAttribute(x[0]);
            currAttr.setValue(x[1]);
            elementAttributes.setNamedItem(currAttr);
        }
    }

    index.appendChild(newElement);
    /*
     * XML DATA CREATION - END
     */

    /*
     * XML DATA DUMP - BEGINNING
     */
    Transformer transformer;
    try {
        transformer = TransformerFactory.newInstance().newTransformer();
    } catch (TransformerConfigurationException ex) {
        return;
    }
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    StreamResult result = new StreamResult(new StringWriter());
    DOMSource source = new DOMSource(doc);
    try {
        transformer.transform(source, result);
    } catch (TransformerException ex) {
        return;
    }

    String xmlString = result.getWriter().toString();
    try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(destFile))) {
        bufferedWriter.write(xmlString);
    } catch (IOException ex) {
    }
    /*
     * XML DATA DUMP - END
     */
}

From source file:DomUtil.java

public static void setAttribute(Node node, String attName, String val) {
    NamedNodeMap attributes = node.getAttributes();
    Node attNode = node.getOwnerDocument().createAttribute(attName);
    attNode.setNodeValue(val);
    attributes.setNamedItem(attNode);
}

From source file:com.evolveum.midpoint.cli.common.ToolsUtils.java

public static void setNamespaceDeclaration(Element element, String prefix, String namespaceUri) {
    Document doc = element.getOwnerDocument();
    NamedNodeMap attributes = element.getAttributes();
    Attr attr;/*w ww.java 2s  . c om*/
    if (prefix == null || prefix.isEmpty()) {
        // default namespace
        attr = doc.createAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, XMLConstants.XMLNS_ATTRIBUTE);
    } else {
        attr = doc.createAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
                XMLConstants.XMLNS_ATTRIBUTE + ":" + prefix);
    }
    checkValidXmlChars(namespaceUri);
    attr.setValue(namespaceUri);
    attributes.setNamedItem(attr);
}

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

private static void appendTypeProp(Document document, Node parentProp, String tag, String nameAttr,
        String textContent) {/* ww w  .ja v  a2  s  .co  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:com.photon.phresco.framework.commons.QualityUtil.java

private static Node appendHeaderManager(Document document, Map<String, String> headersMap) {
    Node headerManager = document.createElement("HeaderManager");
    NamedNodeMap attributes = headerManager.getAttributes();
    attributes.setNamedItem(createAttribute(document, "guiclass", "HeaderPanel"));
    attributes.setNamedItem(createAttribute(document, "testclass", "HeaderManager"));
    attributes.setNamedItem(createAttribute(document, "testname", "HTTP Header Manager"));
    attributes.setNamedItem(createAttribute(document, "enabled", "true"));
    appendHeaderManagerCollectionProp(document, headerManager, headersMap);
    return headerManager;
}