Example usage for org.w3c.dom Element getAttribute

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

Introduction

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

Prototype

public String getAttribute(String name);

Source Link

Document

Retrieves an attribute value by name.

Usage

From source file:Main.java

public static double readDoubleAttr(Element element, String attributeName, double defaultValue) {
    String attributeValue = element.getAttribute(attributeName);
    try {//from ww w. j av  a 2  s.c o m
        return Double.parseDouble(attributeValue);
    } catch (NumberFormatException e) {
        return defaultValue;
    }
}

From source file:Main.java

/**
 * Returns true if the element is of type DITA_TOPICREF_TYPE and has
 * a an empty or null value for its href= attribute.
 * @param elem//from  w ww  .  ja v a 2  s  .  c  om
 * @return
 */
public static boolean isTopicHead(Element elem) {
    if (isDitaType(elem, DITA_TOPICREF_TYPE)) {
        String href = elem.getAttribute(DITA_HREF_ATTNAME);
        return (href == null || href.equals(""));
    }
    return false;
}

From source file:Main.java

/**
 * Returns true if the element is of type DITA_TOPICREF_TYPE and has
 * a non-empty value for its href= attribute.
 * @param elem//  w w  w  .j  a  v  a  2s . com
 * @return
 */
public static boolean isTopicRef(Element elem) {
    if (isDitaType(elem, DITA_TOPICREF_TYPE)) {
        String href = elem.getAttribute(DITA_HREF_ATTNAME);
        return (href != null && !href.equals(""));
    }
    return false;
}

From source file:Main.java

/**
Assume ELEMENT had attribute ATTRNAME, get its value and
return it as a string.  If there is no attribute return the default.
 *//*w  w  w .j  a v  a  2s .co m*/
public static String stringAttribute(Element element, String attrName, String theDefault) {
    String attr = element.getAttribute(attrName);

    if (attr.equals(EmptyString)) {
        /* empty string maybe a valid option */
        Attr attrNode = element.getAttributeNode(attrName);
        if (attrNode != null) {
            return EmptyString;
        }

        return theDefault;
    } else {
        return attr;
    }
}

From source file:Main.java

public static String getArgAttr(final NodeList args, final String strName, final String strAttrName) {
    for (int i = 0; i < args.getLength(); ++i) {
        final Node n = args.item(i);

        if (n instanceof Element) {
            final Element arg = (Element) n;

            if (arg.getAttribute("name").equals(strName)) {
                return ((Element) n).getAttribute(strAttrName);
            }//  w  w w .  j  av a 2s . c om
        }
    } // end for

    return null;
}

From source file:Main.java

/**
 * Get string value for a tag/*from   w ww  . ja  v a  2s.  c  o m*/
 * 
 * @param xmlElement
 *            Root Element of subtree in which required tag is to be
 *            searched
 * @param key
 *            Required tage name
 * @return String value
 */
public static String getStringValueForXMLTagFromAnywhere(Element xmlElement, String key) {

    // Start - testing
    String value = xmlElement.getAttribute(key);
    System.out.println("Value is - " + value);
    // End - testing

    NodeList nl = xmlElement.getElementsByTagName(key);
    String output = "nothing";
    if (nl.getLength() > 0) {
        Node node = nl.item(0).getFirstChild();
        if (node != null) {
            output = node.getNodeValue().trim();
            return output;
        }
    }
    return output;
}

From source file:Main.java

/**
 * @param topicRef//ww w.j  a v a 2  s  . c om
 * @return
 */
public static boolean isMapReference(Element topicRef) {
    return (topicRef.hasAttribute("href") && topicRef.hasAttribute("format")
            && topicRef.getAttribute("format").equals("ditamap"));
}

From source file:Main.java

public static int getInt(Element element, String attributeName) throws NumberFormatException {
    try {//from  w  ww .j  av a2s  .  c o  m
        return Integer.parseInt(element.getAttribute(attributeName));
    } catch (NumberFormatException ex) {
        throw ex;
    }
}

From source file:Main.java

/**
 * Returns attribute as boolean for class
 *
 * @param elem//from   w w w .  ja  v a 2 s  .c  o m
 * @param attribName
 * @param defaultVal
 * @return
 */
public static boolean getAttributeAsBoolean(Element elem, String attribName, boolean defaultVal) {
    String atribVal = elem.getAttribute(attribName);
    boolean retVal = defaultVal;

    try {
        retVal = Boolean.valueOf(atribVal).booleanValue();
    } catch (Exception e) {
    }
    return retVal;
}

From source file:Main.java

/**
 * Update a property of a given configuration file and return a string representation of the
 * xml document/*from   ww  w.ja va  2  s .  c o m*/
 * @param doc
 * @param property
 * @param newValue
 * @throws IllegalAccessException 
 * @throws InstantiationException 
 * @throws ClassNotFoundException 
 */
public static String updateXmlDoc(Document doc, List<String> properties)
        throws ClassNotFoundException, InstantiationException, IllegalAccessException {
    for (String property : properties) {
        String key = property.split(":")[0];
        String value = property.split(":")[1];
        NodeList propertiesNode = doc.getElementsByTagName("esf:property");
        for (int i = 0; i < propertiesNode.getLength(); i++) {
            Node node = propertiesNode.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                Element el = (Element) node;
                if (key.equals(el.getAttribute("name"))) {
                    el.getElementsByTagName("esf:value").item(0).setTextContent(value);
                }
            }
        }
    }
    return parseXmlDocToString(doc);
}