Example usage for org.w3c.dom Attr getValue

List of usage examples for org.w3c.dom Attr getValue

Introduction

In this page you can find the example usage for org.w3c.dom Attr getValue.

Prototype

public String getValue();

Source Link

Document

On retrieval, the value of the attribute is returned as a string.

Usage

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() + "'");
    }//from  www . java 2  s .c om
}

From source file:GetNameAsAttr.java

public static void main(String[] args) throws Exception {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xPath = factory.newXPath();

    Attr result = (Attr) xPath.evaluate("/schedule/@name", new InputSource(new FileReader("tds.xml")),
            XPathConstants.NODE);
    System.out.println(result.getValue());

    result.setValue("The Colbert Report");

}

From source file:Main.java

public static String getValue(Attr attribute) {
    return attribute.getValue();
}

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 Map<String, String> getMapOfAttributes(Node elem) {
    Map<String, String> attrs = new HashMap<String, String>();
    NamedNodeMap m = elem.getAttributes();
    if (m != null) {
        for (int i = 0; i < m.getLength(); ++i) {
            Attr a = (Attr) m.item(i);
            attrs.put(a.getName(), a.getValue());
        }// w  w w. j a  v  a 2s .c o m
    }
    return attrs;
}

From source file:Main.java

/** Return the attributes from the specified element as a Map */
public static Map<String, String> getAttributesAsMap(Element e) {
    Map<String, String> result = new HashMap<String, String>();
    NamedNodeMap attrs = e.getAttributes();
    if (attrs != null) {
        int len = attrs.getLength();
        for (int i = 0; i < len; i++) {
            Node n = attrs.item(i);
            if (n instanceof Attr) {
                Attr a = (Attr) n;
                result.put(a.getName(), a.getValue());
            }/*ww w.jav a 2  s.co m*/
        }
    }
    return result;
}

From source file:Main.java

public static ImmutableMap<String, String> attributesOf(Element element) {
    NamedNodeMap map = element.getAttributes();
    ImmutableMap.Builder<String, String> result = ImmutableMap.builder();
    for (int i = 0, size = map.getLength(); i < size; i++) {
        Attr attr = (Attr) map.item(i);
        result.put(attr.getName(), attr.getValue());
    }// www  .j a v  a2 s. c o m
    return result.build();
}

From source file:Main.java

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

From source file:Main.java

/**
 * copy all attributes form one element to another
 * @param fromEl/*from  w w w  . j a v a  2s.  c o m*/
 * @param toEl
 */
public static void copyAttributes(Element fromEl, Element toEl) {
    for (int a = 0; a < fromEl.getAttributes().getLength(); a++) {
        Attr at = (Attr) fromEl.getAttributes().item(a);
        toEl.setAttribute(at.getName(), at.getValue());
    }
}

From source file:Main.java

/**
 * Gets an attribute value of the given element.
 * @param ownerElem the element that owns the attribute
 * @param attrName the name of the attribute to retrieve
 * @return the attribute value as a string, or <code>null</code> if that attribute does not have
 * a specified value/*from w  ww . j  av a 2s.  c o m*/
 */
public static String getAttribute(Element ownerElem, String attrName) {
    Attr attribute = ownerElem.getAttributeNode(attrName);
    return attribute != null ? attribute.getValue() : null;
}