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:Utils.java

public static void copyAttributes(Element from, Element to) {
    NamedNodeMap attributes = from.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
        Attr node = (Attr) attributes.item(i);
        to.setAttributeNS(node.getNamespaceURI(), node.getName(), node.getValue());
    }//ww  w  .jav a  2  s.com
}

From source file:Main.java

public static Map<String, String> getAttrList(Node node) {
    Map<String, String> map = new HashMap<String, String>();

    NamedNodeMap list = node.getAttributes();
    if (list != null)
        for (int i = 0; i < list.getLength(); i++) {
            Attr attr = (Attr) list.item(i);
            map.put(attr.getName(), attr.getValue());
        }//from   w w w .j  av  a 2  s.co m

    return map;
}

From source file:Main.java

/**
 * Returns the attribute value for the attribute with the specified name.
 * Returns null if there is no such attribute, or
 * the empty string if the attribute value is empty.
 *
 * <p>This works around a limitation of the DOM
 * <code>Element.getAttributeNode</code> method, which does not distinguish
 * between an unspecified attribute and an attribute with a value of
 * "" (it returns "" for both cases).//from w ww  .j  av a2 s  .  c o  m
 *
 * @param elem the element containing the attribute
 * @param name the name of the attribute
 * @return the attribute value (may be null if unspecified)
 */
public static String getAttributeValue(Element elem, String name) {
    Attr attr = elem.getAttributeNodeNS(null, name);
    return (attr == null) ? null : attr.getValue();
}

From source file:Main.java

protected static Map<String, String> createAttributeTable(Element e) {
    Map<String, String> attributeTable = new HashMap<>();
    NamedNodeMap attrs = e.getAttributes();
    if (attrs != null) {
        int numAttrs = attrs.getLength();
        for (int i = 0; i < numAttrs; i++) {
            Node na = attrs.item(i);
            if (na.getNodeType() != Node.ATTRIBUTE_NODE) {
                continue;
            }//  w w w.j ava  2 s.c o  m
            Attr a = (Attr) na;
            attributeTable.put(a.getName(), a.getValue());
        }
    }
    return attributeTable;
}

From source file:Main.java

/**
 * Returns a map of the passed node's attributes
 * @param node The nopde to get an attribute map for
 * @return a [possibly empty] map of the node's attributes
 *//*from w  w  w. ja va2  s .  co  m*/
public static Map<String, String> getAttributeMap(final Node node) {
    if (node == null)
        throw new IllegalArgumentException("The passed node was null");
    final NamedNodeMap nnm = node.getAttributes();
    final int size = nnm.getLength();
    if (size == 0)
        return Collections.emptyMap();
    final Map<String, String> map = new LinkedHashMap<String, String>(size);
    for (int i = 0; i < size; i++) {
        final Attr attr = (Attr) nnm.item(i);
        map.put(attr.getName(), attr.getValue());
    }
    return map;
}

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:Main.java

static void printElement(Element element, String indent) {
    System.out.println("Element '" + element.getNodeName() + "'");
    NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        switch (child.getNodeType()) {
        case Node.ELEMENT_NODE:
            printElement((Element) child, indent + "\t");
            break;
        case Node.ATTRIBUTE_NODE:
            Attr attr = (Attr) child;
            System.out.println("\tAttribute: '" + attr.getName() + "' = '" + attr.getValue() + "'");
            break;
        case Node.COMMENT_NODE:
            Comment comment = (Comment) child;
            System.out.println("\tComment: '" + comment.getData() + "'");
            break;
        case Node.CDATA_SECTION_NODE:
            CharacterData cdata = (CharacterData) child;
            System.out.println("\tCDatat: '" + cdata.getData() + "'");
            break;
        case Node.TEXT_NODE:
            Text text = (Text) child;
            System.out.println("\tText: '" + text.getData() + "'");
            break;
        default://  ww w  .  ja v a2s .  c  o m
            System.out.println("\tUnknown node type: '" + child.getNodeType() + "'");
            break;
        }
    }
}

From source file:Utils.java

/**
 * Returns null, not "", for a nonexistent attribute.
 * //  w  w  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

public static void dupAttributes(Document doc) {
    Element root = doc.getDocumentElement();
    Element personOne = (Element) root.getFirstChild();

    Attr deptAttr = personOne.getAttributeNode("dept");
    personOne.removeAttributeNode(deptAttr);
    String deptString = deptAttr.getValue();
    personOne.setAttribute("dept", deptString + "updated");

    String mailString = personOne.getAttribute("mail");
    personOne.setAttribute("mail", mailString + "updated");

    String titleString = personOne.getAttribute("title");
    //personOne.removeAttribute("title");
    personOne.setAttribute("title", titleString + "updated");
}

From source file:Main.java

@Nullable
public static Map<String, String> getAllAttributesAsMap(@Nullable final Element aSrcNode) {
    if (aSrcNode != null) {
        final NamedNodeMap aNNM = aSrcNode.getAttributes();
        if (aNNM != null) {
            final Map<String, String> aMap = new LinkedHashMap<String, String>(aNNM.getLength());
            final int nMax = aNNM.getLength();
            for (int i = 0; i < nMax; ++i) {
                final Attr aAttr = (Attr) aNNM.item(i);
                aMap.put(aAttr.getName(), aAttr.getValue());
            }//from  w  ww.jav  a 2  s . co  m
            return aMap;
        }
    }
    return null;
}