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

/**
 *  The attrs parameter is an array of [name1, value1, name2, value2, etc].
 *  Set these attributes  on the given node.
 *
 *  @param node The  xml element to set attributes on.
 *  @param attrs The array of attribute name/value  pairs.
 *///from   w  ww  .j  av a  2 s. c  o m
public static void setAttributes(Element node, String[] attrs) {
    for (int i = 0; i < attrs.length; i += 2) {
        node.setAttribute(attrs[i], attrs[i + 1]);
    }
}

From source file:Main.java

public static void createAttribute(Document doc, String nodeName, Map<String, String> map) {
    Node node = doc.getElementsByTagName(nodeName).item(0);
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        Element element = (Element) node;
        for (Map.Entry<String, String> entry : map.entrySet()) {
            element.setAttribute(entry.getKey(), entry.getValue());
        }/* w ww  . ja va  2 s  .c  o m*/
        //TransformerFactory factory = TransformerFactory.newInstance();
        //Transformer former = factory.newTransformer();
        //former.transform(new DOMSource(doc), new StreamResult(new File("src/shuiguo.xml")));
    }
}

From source file:Main.java

public static void addHook(String hookName, String interfaceName, String identity, boolean superOverride,
        String superOverrideClass) {
    try {/*from  ww w .  j a  v a 2 s .c om*/
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBldr = dFactory.newDocumentBuilder();
        Document doc = dBldr.parse(new File("hooks.xml"));

        Element hooks = doc.getDocumentElement();

        Element hook = doc.createElement("hook");
        hook.setAttribute("name", hookName);

        Element eIdentity = doc.createElement("identity");
        eIdentity.appendChild(doc.createTextNode(identity));
        hook.appendChild(eIdentity);

        Element eInterface = doc.createElement("interface");
        eInterface.appendChild(doc.createTextNode(interfaceName));
        hook.appendChild(eInterface);

        Element eSuper = doc.createElement("super");
        if (superOverride) {
            eSuper.appendChild(doc.createTextNode(superOverrideClass));
        } else {
            eSuper.appendChild(doc.createTextNode("null"));
        }
        hook.appendChild(eSuper);

        hooks.insertBefore(hook, hooks.getLastChild());
        write(doc);
    } catch (SAXException | IOException | ParserConfigurationException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void fixSize(final Element element) {
    final int len = element.getChildNodes().getLength();

    if (len > 0) {
        element.setAttribute("size", "" + len);
    } else {/* w ww  .j a va  2  s .co m*/
        element.getParentNode().removeChild(element);
    }
}

From source file:Main.java

/**
 * Adds an attribute to an element of a document in memory <br>
 * (It's supposed that the element is one of the document)
 * /*ww  w. j  ava  2  s . c  o m*/
 * @param element
 *            The xml elemnet where the attibute will be added
 * @param attributeName
 *            The name of the attribute
 * @param attributeValue
 *            The value of the attribute
 */
public static void setAttribute(Element element, String attributeName, String attributeValue) {
    // Adds the atribute to the element
    element.setAttribute(attributeName, attributeValue);
}

From source file:Main.java

private static void addEntry(String file_path, String operation, String key, String value) {

    Document doc = getDocument(file_path);
    if (doc == null) {
        return;//from w w w.  j a va 2s  .  c  om
    }

    Element entry_elem = doc.createElement("Entry");
    entry_elem.setAttribute("Type", operation);
    entry_elem.setAttribute("Key", key);
    if (operation.equals("STORE")) {
        entry_elem.appendChild(doc.createTextNode(value));
    }

    Element log_element = (Element) doc.getElementsByTagName("Log").item(0);
    log_element.appendChild(entry_elem);

    saveFile(file_path, doc);

}

From source file:Main.java

/**
 * Sarches for ressources that have to be downloaded and creates a list with all download links.
 *
 * @param node to check for download links
 * @return list with all found download links
 *//*from www .  j a  v  a 2 s. c  om*/
private static ArrayList<String> findUrls(Node node) {

    int type = node.getNodeType();
    ArrayList<String> result = new ArrayList<>();
    String temp = null;
    NamedNodeMap atts = node.getAttributes();
    Log.i(TAG, "parsing for ressources.  node: " + node.getNodeName() + " value: " + node.getNodeValue()
            + " atts length: " + atts.getLength() + "type: " + type);
    switch (type) {
    //Element
    case Node.ELEMENT_NODE:
        //TODO: This method is stupid. It just looks for
        // attributes named ressourcepath. What if we have to download several ressources?

        for (int j = 0; j < atts.getLength(); j++) {
            Log.i(TAG, "atts: " + atts.item(j).getNodeName() + " value: " + atts.item(j).getNodeValue());
            temp = atts.item(j).getNodeValue();

            if (temp != null) {
                result.add(temp);
                Log.i(TAG, "added path: " + temp);
            }

            Log.i(TAG, "parent node:" + node.getParentNode().getNodeName());
            Element parent = (Element) node.getParentNode();
            parent.setAttribute("TESTITEST", "dllink");

        }
        // get the pages, means the children
        for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
            ArrayList<String> childres = findUrls(child);
            if (childres.size() > 0) {
                result.addAll(childres);
            }
        }
        break;
    }
    return result;
}

From source file:Main.java

public static <K, V> String mapToXml(Map<K, V> map, String rootName, String childName, String keyName,
        String valueName) throws TransformerException, ParserConfigurationException {
    Document document = DOCUMENT_BUILDER_FACTORY.newDocumentBuilder().newDocument();

    Element root = document.createElement(rootName);
    document.appendChild(root);/* w  w  w  . jav  a  2s . c o  m*/

    for (Map.Entry<K, V> errorCodeReport : map.entrySet()) {
        Element s = document.createElement(childName);
        root.appendChild(s);

        s.setAttribute(keyName, errorCodeReport.getKey().toString());
        s.setAttribute(valueName, errorCodeReport.getValue().toString());
    }

    return getStringFromDocument(document);
}

From source file:Main.java

/**
 * create an element from a document, that has some text content in it
 * @param doc - document/* w w  w. j  a v  a  2  s.  co  m*/
 * @param name - element name
 * @param attributes - attribute map
 * @return element object
 */
public static Element createElement(Document doc, String name, Map<String, String> attributes) {
    Element e = doc.createElement(name);
    for (String attribute : attributes.keySet())
        e.setAttribute(attribute, attributes.get(attribute));
    return e;
}

From source file:com.marklogic.client.test.DeleteSearchTest.java

public static void writeDoc() throws Exception {
    Document domDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    Element root = domDocument.createElement("root");
    root.setAttribute("xml:lang", "en");
    root.setAttribute("foo", "bar");
    root.appendChild(domDocument.createElement("child"));
    root.appendChild(domDocument.createTextNode("mixed"));
    domDocument.appendChild(root);//  ww  w  .j a v  a2s.c o m

    @SuppressWarnings("unused")
    String domString = ((DOMImplementationLS) DocumentBuilderFactory.newInstance().newDocumentBuilder()
            .getDOMImplementation()).createLSSerializer().writeToString(domDocument);

    XMLDocumentManager docMgr = Common.client.newXMLDocumentManager();
    docMgr.write(docId, new DOMHandle().with(domDocument));
}