Example usage for org.w3c.dom Element getAttributeNode

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

Introduction

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

Prototype

public Attr getAttributeNode(String name);

Source Link

Document

Retrieves an attribute node by name.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);/*from  w  w w . ja va  2 s . c om*/
    factory.setExpandEntityReferences(false);

    Document doc = factory.newDocumentBuilder().parse(new File("filename"));
    Element element = doc.getElementById("key1");
    Attr attr = (Attr) element.getAttributeNode("attrName");
    boolean wasSpecified = attr != null && attr.getSpecified();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder loader = factory.newDocumentBuilder();

    Document document = loader.parse("sample.xml");

    Element purchaseOrder = document.getDocumentElement();

    Attr orderDate = purchaseOrder.getAttributeNode("date");
    System.out.println(orderDate.getValue());

    NamedNodeMap attrs = purchaseOrder.getAttributes();
    int attrsCount = attrs.getLength();

    for (int i = 0; i < attrsCount; i++) {
        Attr item = (Attr) attrs.item(i);
        System.out.println("'" + item.getName() + "' = '" + item.getValue() + "'");
    }/*  w w  w . j a va 2 s .  c o m*/
}

From source file:Main.java

public static Attr getAttr(Element elem, String name) {
    return elem.getAttributeNode(name);
}

From source file:Main.java

public static String getAttribute(Element element, String name) {
    Attr attr = element.getAttributeNode(name);
    return (attr != null) ? attr.getValue() : null;
}

From source file:Main.java

public static Attr getAttribute(Element el, String attrName) {
    return el.getAttributeNode(attrName);
}

From source file:Main.java

/**
 * Return the attribute value./*from  ww  w  .ja v a2 s . c  om*/
 * 
 * @return java.lang.String
 * @param element
 *            org.w3c.dom.Element
 * @param attr
 *            java.lang.String
 */
public static String getAttributeValue(Element element, String attr) {
    return element.getAttributeNode(attr).getValue();
}

From source file:Utils.java

/**
 * Returns null, not "", for a nonexistent attribute.
 * //from  ww  w . j av  a  2  s.  c  o m
 * @param e
 * @param attributeName
 * @return
 */
public static String getAttributeValueEmptyNull(Element e, String attributeName) {
    Attr node = e.getAttributeNode(attributeName);
    if (node == null) {
        return null;
    }
    return node.getValue();
}

From source file:Main.java

private static Attr getOrCreateAttribute(Element parent, String name) {
    Attr a = parent.getAttributeNode(name);
    if (a == null) {
        Document doc = parent.getOwnerDocument();
        a = doc.createAttribute(name);/* ww  w . ja  va2s  . c  om*/
        parent.setAttributeNode(a);
    }
    return a;
}

From source file:Main.java

public static String getAttributeOrNull(Element e, String name) {
    Attr a = e.getAttributeNode(name);
    if (a == null)
        return null;
    return a.getValue();
}

From source file:Utils.java

/**
 * <p>Retutns the value of the named attribute of the given
 * element. If there is no such attribute, returns null.</p>
 *
 * @param element element//from  ww w . jav  a 2 s  .  c om
 * @param name name
 * @return value
 */
public static String getAttributeValue(Element element, String name) {
    Attr attribute = element.getAttributeNode(name);
    if (attribute == null) {
        return null;
    } else {
        return attribute.getValue();
    }
}