Example usage for org.w3c.dom Element getAttributes

List of usage examples for org.w3c.dom Element getAttributes

Introduction

In this page you can find the example usage for org.w3c.dom Element getAttributes.

Prototype

public NamedNodeMap getAttributes();

Source Link

Document

A NamedNodeMap containing the attributes of this node (if it is an Element) or null otherwise.

Usage

From source file:Main.java

private static void attributize(Element root) {
    NamedNodeMap attributeMap = root.getAttributes();
    for (int i = 0; i < attributeMap.getLength(); i++) {
        org.w3c.dom.Attr attr = (Attr) attributeMap.item(i);

        Element attrElement = root.getOwnerDocument().createElement(attr.getName());
        attrElement.setTextContent(attr.getValue());
        root.appendChild(attrElement);/*w  w w  .j  a v a  2  s .  co  m*/
    }

    NodeList children = root.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        if (children.item(i) instanceof Element) {
            attributize((Element) children.item(i));
        }
    }
}

From source file:Main.java

public static Properties getElementAttributes(Element ele) {
    Properties ht = new Properties();
    NamedNodeMap nnm = ele.getAttributes();
    int len = nnm.getLength();
    Node tmpn = null;/*from  ww w . ja v a  2 s  .  co m*/
    for (int k = 0; k < len; k++) {
        tmpn = nnm.item(k);
        String tmps = tmpn.getNodeValue();
        if (tmps == null) {
            tmps = "";
        }
        ht.put(tmpn.getNodeName(), tmps);
    }
    return ht;
}

From source file:Main.java

public static List<Attr> attributes(final Element element) {
    final NamedNodeMap attributeMap = element.getAttributes();
    if (attributeMap == null || attributeMap.getLength() == 0) {
        return null;
    }/*w  w  w  .  j  a  va 2  s .  c o  m*/

    final List<Attr> attributes = new ArrayList<Attr>();
    for (int i = 0; i < attributeMap.getLength(); i++) {
        attributes.add((Attr) attributeMap.item(i));
    }

    return attributes;
}

From source file:Main.java

/**
 * @param el//from   w ww  .j  a va  2  s.  c o m
 * @return the number of element and attribute nodes in an XML tree
 */
public static int nodeValueCount(Element el) {
    int count = el.getAttributes().getLength(); //  attributes of this node

    // text value of this node
    String text = getText(el);
    if ((text != null) && (!text.equals("")))
        count++;

    // contributions from child elements
    Vector<Element> childElements = childElements(el);
    for (int i = 0; i < childElements.size(); i++)
        count = count + nodeValueCount(childElements.get(i));

    return count;
}

From source file:Main.java

public static boolean setAttributeValue(final Element target, final String attributeName, final String value) {
    final NamedNodeMap map = target.getAttributes();
    final Node attributeValueHolder = map.getNamedItem(attributeName);
    if (attributeValueHolder != null) {
        attributeValueHolder.setNodeValue(value);
        return true;
    }/*  w w  w. j  a v  a2 s.com*/
    return false;
}

From source file:Main.java

/**
 * Method get all attributes in a xml element.
 * @param el xml element on input.//w ww.  j  a  v  a 2s . c o  m
 * @return array list of attributes.
 */
public static List<Attr> getAllAttributes(Element el) {
    List<Attr> listAttr = new ArrayList<>();
    NamedNodeMap attributes = el.getAttributes();//get map containing the attributes of this node
    int numAttrs = attributes.getLength(); //get the number of nodes in this map
    for (int i = 0; i < numAttrs; i++) {
        Attr attr = (Attr) attributes.item(i);
        listAttr.add(attr);
    }
    return listAttr;
}

From source file:Main.java

public static void mergeElement(Element from, Element to) {
    // attrs//from  www.java2  s . c om
    for (int idx = 0; idx < from.getAttributes().getLength(); idx++) {
        Node node = from.getAttributes().item(idx);
        to.getAttributes().setNamedItem(node.cloneNode(false));
    }

    // children
    for (int idx = 0; idx < from.getChildNodes().getLength(); idx++) {
        Node node = from.getChildNodes().item(idx);

        if (!Element.class.isInstance(node)) {
            continue;
        }

        to.appendChild(node.cloneNode(true));
    }
}

From source file:Main.java

public static String getAttributeValue(final Element base, final String name) {

    // get attribute of this element...
    NamedNodeMap mapAttributes = base.getAttributes();
    Node node = mapAttributes.getNamedItem(name);
    if (node != null) {
        return node.getNodeValue();
    }/*from  w  ww . j a va 2 s . co  m*/
    return null;
}

From source file:Utils.java

public static String getPrefix(Element el, String ns) {
    NamedNodeMap atts = el.getAttributes();
    for (int i = 0; i < atts.getLength(); i++) {
        Node node = atts.item(i);
        String name = node.getNodeName();
        if (ns.equals(node.getNodeValue())
                && (name != null && (XMLNAMESPACE.equals(name) || name.startsWith(XMLNAMESPACE + ":")))) {
            return node.getPrefix();
        }/*from w  w w.java 2 s  . c  om*/
    }
    return null;
}

From source file:Main.java

public static void removeAttributes(Element elem) {
    if (elem.hasAttributes()) {
        NamedNodeMap attributeMap = elem.getAttributes();
        // since node maps are live, hold attributes in a separate collection
        int n = attributeMap.getLength();
        Attr[] attributes = new Attr[n];
        for (int i = 0; i < n; i++)
            attributes[i] = (Attr) attributeMap.item(i);
        // now remove each attribute from the element
        for (int i = 0; i < n; i++)
            elem.removeAttributeNode(attributes[i]);
    }/*from   www.j a  va 2 s.  com*/
}