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

/**
 * Get all attributes of the specified element
 *//*w  w  w .  j a v a 2  s .  c o  m*/
public static ArrayList<Attr> getAllAttributes(Element element) {
    final NamedNodeMap nodeMap = element.getAttributes();
    final ArrayList<Attr> result = new ArrayList<Attr>();

    for (int i = 0; i < nodeMap.getLength(); i++)
        result.add((Attr) nodeMap.item(i));

    return result;
}

From source file:Main.java

public static Element getPropertyNode(Element properties, String name) {
    NodeList nodeList = properties.getElementsByTagName(PROPERTY_NODE);
    for (int i = 0; i < nodeList.getLength(); i++) {
        Element element = (Element) nodeList.item(i);
        if (name.equals(element.getAttributes().getNamedItem(NAME_ATTR).getNodeValue()))
            return element;
    }//from w  w  w .j  a  v  a  2  s . com
    return null;
}

From source file:Main.java

public static Element getPropertyNode(Element properties, String name) {
    NodeList nodeList = properties.getElementsByTagName(PROPERTY_NODE);
    for (int i = 0; i < nodeList.getLength(); i++) {
        Element element = (Element) nodeList.item(i);
        if (name.equals(element.getAttributes().getNamedItem(NAME_ATTR).getNodeValue())) {
            return element;
        }/*  w  ww.j a va2  s  .  com*/
    }
    return null;
}

From source file:Main.java

private static void serializeAttributeOf(XmlSerializer serializer, Element element) throws IOException {
    NamedNodeMap map = element.getAttributes();
    for (int i = 0; i < map.getLength(); i++) {
        Node attr = map.item(i);/*from  w w w  . j  ava2 s .c om*/
        serializer.attribute(null, attr.getNodeName(), attr.getNodeValue());
    }
}

From source file:Main.java

public static Object getPropertyValue(Element element) {
    NamedNodeMap map = element.getAttributes();
    map.removeNamedItem(NAME_ATTR);//from   w w  w .ja v  a  2  s.co m

    if (map.item(0).getNodeName().equals(STRING_ATTR))
        return map.item(0).getNodeValue();
    else if (map.item(0).getNodeName().equals(INTEGER_ATTR))
        return new Integer(map.item(0).getNodeValue());
    else if (map.item(0).getNodeName().equals(DOUBLE_ATTR))
        return new Double(map.item(0).getNodeValue());
    else if (map.item(0).getNodeName().equals(FLOAT_ATTR))
        return new Float(map.item(0).getNodeValue());
    else if (map.item(0).getNodeName().equals(BOOLEAN_ATTR))
        return Boolean.valueOf(map.item(0).getNodeValue());
    else if (map.item(0).getNodeName().equals(COLOR_ATTR)) {
        String[] rgb = map.item(0).getNodeValue().split(",");
        return new Color(new Integer(rgb[0]), new Integer(rgb[1]), new Integer(rgb[2]));
    } else if (map.item(0).getNodeName().equals(FONT_ATTR)) {
        String[] font = map.item(0).getNodeValue().split(",");
        return new Font(font[0], new Integer(font[1]), new Integer(font[2]));
    } else {
        return null;
    }
}

From source file:Main.java

/**
 * //from  w w w.j a  v  a 2  s .com
 * @param el
 * @return depth of an XML node tree
 */
public static int getDepth(Element el) {
    int depth = 1;
    if (el.getAttributes().getLength() > 0)
        depth = 2;

    int max = 0;
    Vector<Element> childElements = childElements(el);
    for (int i = 0; i < childElements.size(); i++) {
        int childDepth = getDepth(childElements.get(i));
        if (childDepth > max)
            max = childDepth;
    }

    return depth + max;
}

From source file:Main.java

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

    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

public static Object getPropertyValue(Element element) {
    NamedNodeMap map = element.getAttributes();
    map.removeNamedItem(NAME_ATTR);//from  w ww  .j  a v  a  2s  . c  o m

    if (map.item(0).getNodeName().equals(STRING_ATTR)) {
        return map.item(0).getNodeValue();
    } else if (map.item(0).getNodeName().equals(INTEGER_ATTR)) {
        return new Integer(map.item(0).getNodeValue());
    } else if (map.item(0).getNodeName().equals(DOUBLE_ATTR)) {
        return new Double(map.item(0).getNodeValue());
    } else if (map.item(0).getNodeName().equals(FLOAT_ATTR)) {
        return new Float(map.item(0).getNodeValue());
    } else if (map.item(0).getNodeName().equals(BOOLEAN_ATTR)) {
        return Boolean.valueOf(map.item(0).getNodeValue());
    } else if (map.item(0).getNodeName().equals(COLOR_ATTR)) {
        String[] rgb = map.item(0).getNodeValue().split(",");
        return new Color(new Integer(rgb[0]), new Integer(rgb[1]), new Integer(rgb[2]));
    } else if (map.item(0).getNodeName().equals(FONT_ATTR)) {
        String[] font = map.item(0).getNodeValue().split(",");
        return new Font(font[0], new Integer(font[1]), new Integer(font[2]));
    } else {
        return null;
    }
}

From source file:Main.java

/**
 * Check if the element contains just a text/cdata, in which case return
 * that value. Else return null;//w w w. j av  a  2  s . c o m
 *
 * @param element
 * @return null if this is not a textElement as we see it. Value of single
 *         text/CData child otherwise
 */
private static String getElementValue(Element element) {
    if (element.getAttributes().getLength() > 0) {
        return null;
    }

    NodeList children = element.getChildNodes();
    String value = null;
    int nbrChildren = children.getLength();
    for (int i = 0; i < nbrChildren; i++) {
        Node child = children.item(i);
        short childType = child.getNodeType();
        if (childType == Node.ELEMENT_NODE) {
            return null;
        }
        if (childType == Node.CDATA_SECTION_NODE || childType == Node.TEXT_NODE) {
            if (value != null) {
                return null;
            }
            value = child.getNodeValue();
        }
    }
    return value;
}

From source file:Main.java

static public List<Node> getAttributeNodeList(Element element, Pattern name) {
    NamedNodeMap nodes = element.getAttributes();
    List<Node> attributes = new ArrayList<>();
    int i, size = nodes.getLength();
    Node node;//from  w  ww  .  j  a  v a2s  .  c o  m

    for (i = 0; i < size; i++) {
        node = nodes.item(i);

        if (name.matcher(node.getNodeName()).find())
            attributes.add(node);
    }

    return attributes;
}