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 HashMap<String, String> convertXmlAttributeToHashMap(NamedNodeMap inputNodeMap) {
    HashMap<String, String> outputHashMap = new HashMap<String, String>();

    for (int i = 0; i < inputNodeMap.getLength(); i++) {
        Attr inputNodeMapAttribute = (Attr) inputNodeMap.item(i);
        outputHashMap.put(inputNodeMapAttribute.getName().trim(), inputNodeMapAttribute.getValue().trim());
    }/*from   w w w . j a va  2  s.co m*/

    return outputHashMap;
}

From source file:Main.java

/**
 * Returns the String value of the unqualified attribute with the given
 * local name belonging to the given element, or null if the attribute is
 * not present./* www  . j  av  a 2 s.  c o m*/
 * 
 * @param elem an element
 * @param localName an unqualified attribute name
 * @return the String value of the attribute, or null if the attribute is
 *         not present
 */
public static String getAttrString(Element elem, String localName) {
    Attr attr = elem.getAttributeNodeNS(null, localName);
    String value = (attr != null) ? attr.getValue() : null;
    return value;
}

From source file:Main.java

public static String nodeToString(Node node) {
    String strNode = "";
    Node nodeIt = node;//from ww w . j a va 2s .  com

    if (nodeIt instanceof Element) {
        Element elem = (Element) nodeIt;
        String strElem = "";
        strElem += "<" + elem.getTagName();
        NamedNodeMap attribs = elem.getAttributes();
        int len = attribs.getLength();
        for (int i = 0; i < len; i++) {
            Attr attr = (Attr) attribs.item(i);
            strElem += " " + attr.getName() + "=\"" + attr.getValue() + "\"";
        }
        strElem += ">";
        strNode = strElem + strNode;
    } else if (nodeIt instanceof CharacterData) {
        CharacterData charNode = (CharacterData) nodeIt;
        strNode = charNode.getData() + strNode;
    } else if (nodeIt instanceof Document)
        strNode = strNode; // no hacemos nada
    else
        strNode = nodeIt.getNodeValue() + strNode;

    return strNode;
}

From source file:Main.java

public static String getNodeAttribute(Node node, String name) {

    if (node.hasAttributes()) {

        NamedNodeMap attrs = node.getAttributes();

        for (int i = 0; i < attrs.getLength(); i++) {
            Attr attribute = (Attr) attrs.item(i);

            if (attribute.getName().equals(name)) {
                return attribute.getValue();
            }/*from  w  ww.  j  av a  2  s. c  o m*/

        }
    }

    return null;

}

From source file:Main.java

/**
 * The latest version of XercesJ 2.9 returns an empty string for non existing
 * attributes. To differentiate between empty attributes and non-existing
 * attributes, this method returns a default value for non existing
 * attributes.//from w w w.j a  v  a  2s .  com
 *
 * @param aElement
 *        the source element to get the attribute from. May not be
 *        <code>null</code>.
 * @param sAttrName
 *        the name of the attribute to query. May not be <code>null</code>.
 * @param sDefault
 *        the value to be returned if the attribute is not present.
 * @return the default value if the attribute does not exists, the string
 *         value otherwise
 */
@Nullable
public static String getAttributeValue(@Nonnull final Element aElement, @Nonnull final String sAttrName,
        @Nullable final String sDefault) {
    final Attr aAttr = aElement.getAttributeNode(sAttrName);
    return aAttr == null ? sDefault : aAttr.getValue();
}

From source file:Main.java

private static void outputElement(Element node, String indent) {
    System.out.print(indent + "<" + node.getTagName());
    NamedNodeMap nm = node.getAttributes();
    for (int i = 0; i < nm.getLength(); i++) {
        Attr attr = (Attr) nm.item(i);
        System.out.print(" " + attr.getName() + "=\"" + attr.getValue() + "\"");
    }/*from  w  w  w .ja v a 2  s  .  c o  m*/
    System.out.print(">");
    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++)
        outputloop(list.item(i), indent + TAB);
    System.out.println(indent + "</" + node.getTagName() + ">");
}

From source file:Main.java

/**
 * Parses the attribute's value. If the value is 0 or "false" then false is returned, if the value is 1 or "true"
 * then true is returned, if the value is anything else then null returned.
 * /*from  w w  w .  ja  va2  s .  c  om*/
 * @param attribute attribute whose value will be converted to a boolean
 * 
 * @return boolean value of the attribute or null
 */
public static Boolean getAttributeValueAsBoolean(Attr attribute) {
    if (attribute == null) {
        return null;
    }

    String valueStr = attribute.getValue();
    if (valueStr.equals("0") || valueStr.equals("false")) {
        return Boolean.FALSE;
    } else if (valueStr.equals("1") || valueStr.equals("true")) {
        return Boolean.TRUE;
    } else {
        return null;
    }
}

From source file:Main.java

/**
 * Print all the attributes of the given node
 *
 * @param parent//  ww  w . j  a va2 s . c  o  m
 */
public static void printNode(Node parent) {
    if (parent == null) {
        System.out.println("null node!");
        return;
    }
    System.out.println(parent.getNodeName() + " node:");
    NamedNodeMap attrs = parent.getAttributes();
    for (int i = 0; i < attrs.getLength(); i++) {
        Attr attribute = (Attr) attrs.item(i);
        System.out.println("  " + attribute.getName() + " = " + attribute.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   w ww  .  ja v  a  2s.c o m*/
 * @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();
    }
}

From source file:Main.java

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