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

/**
 * Return true if the linking element specifies a, explcitly or implicitly, a
 * DITA map or topic document./*from  www  .  j  a  v a 2s  .  c om*/
 * @param link An element that may exhibit the format attribute
 * @return
 */
public static boolean targetIsADitaFormat(Element link) {
    if (link.hasAttribute(DITA_FORMAT_ATTNAME)) {
        String formatValue = link.getAttribute(DITA_FORMAT_ATTNAME);
        if (!DITA_ELEM_TAGNAME.equalsIgnoreCase(formatValue) && !"ditamap".equalsIgnoreCase(formatValue))
            return false;
    }
    return true; // default format is "dita"

}

From source file:Main.java

public static String getValue(final NodeList nodes, final String strAttrName, final String strAttrValue) {
    for (int i = 0; i < nodes.getLength(); ++i) {
        final Node node = nodes.item(i);

        if (node instanceof Element) {
            final Element elem = (Element) node;

            if (strAttrValue.equals(elem.getAttribute(strAttrName))) {
                return elem.getLastChild().getTextContent().trim();
            }// ww w  .  jav  a 2 s .com
        }
    } // end for

    return null;
}

From source file:Main.java

public static Element GetCustomer(JFrame mainFrame, long lookupValue, Document CustomerDoc) {
    Element Customer = null;//w  w  w  . j  a  va  2  s . co  m
    Element Root = CustomerDoc.getDocumentElement();
    if (Root.hasChildNodes()) {
        NodeList Customers = Root.getChildNodes();
        for (int i = 0; i < Customers.getLength(); i++) {
            if (Customers.item(i).getNodeName() == "#text")
                continue;
            Element Current = (Element) Customers.item(i);
            if (lookupValue == Integer.parseInt(Current.getAttribute("Id"))) {
                Customer = Current;
            }
        }
    }
    return Customer;
}

From source file:Main.java

/**
 * Returns true if the topicref specifies format="ditamap"
 * @param topicref/*from   ww w. j av  a2 s .  c  om*/
 * @return
 */
public static boolean targetIsADitaMap(Element topicref) {
    if (topicref.hasAttribute(DITA_FORMAT_ATTNAME)) {
        String formatValue = topicref.getAttribute(DITA_FORMAT_ATTNAME);
        if ("ditamap".equalsIgnoreCase(formatValue))
            return true;
    }
    return false;
}

From source file:Main.java

public static boolean validProcess(String xml) {
    try {//from  w w w  .  java2 s .c  o  m
        Set<String> classifiers = new HashSet<String>();

        InputStream is = new ByteArrayInputStream(xml.getBytes());
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        Document document = docBuilder.parse(is);

        // iterate over all operators
        NodeList nodes = document.getElementsByTagName("operator");
        for (int i = 0; i < nodes.getLength(); i++) {
            Element element = (Element) nodes.item(i);
            String className = element.getAttribute("class");

            if (classifiers.contains(className)) {
                return false;
            }
            classifiers.add(className);
        }
        return true;
    } catch (Exception e) {
        return false;
    }
}

From source file:Main.java

/**
 * Get the value of the specified attribute as a boolean. If the element has no attribute by the specified name,
 * false is returned.//from   w  ww.  j a  v  a2 s  .  c o  m
 * 
 * @param root The element.
 * @param attrName The attribute name.
 * @return True if the element has the attribute and its value is "true".
 */
public static boolean getAttrBoolean(Element root, String attrName) {
    if (root.hasAttribute(attrName))
        return "true".equals(root.getAttribute(attrName).toLowerCase());
    return false;
}

From source file:Main.java

/**
 * Get the value of the specified attribute as an integer. If the element has no attribute by the specified name, 0
 * is returned.//from ww w  . j a  v  a  2 s  . c  om
 * 
 * @param root The element.
 * @param attrName The attribute name.
 * @return The value of the attribute, if it exists, 0 otherwise.
 */
public static int getAttrInt(Element root, String attrName) {
    if (root.hasAttribute(attrName))
        return Integer.valueOf(root.getAttribute(attrName)).intValue();
    return 0;
}

From source file:Main.java

public static boolean readBooleanAttribute(Element elem, String name, boolean defaultValue) {
    boolean back = defaultValue;

    String str = elem.getAttribute(name);
    if (str != null) {
        if (str.length() > 0) {
            int hash = str.hashCode();
            if ((hash == "yes".hashCode()) || (hash == "true".hashCode())) {
                back = true;/*from  ww w.  jav  a2s. c o m*/
            } else {
                back = false;
            }
        }
    }

    return back;
}

From source file:Main.java

/**
 * Extract the parameters specified in the XML document for the Element
 * specified by parent.//ww w. j  ava 2s  .c  om
 * 
 * @param parent
 *            - Parent Node.
 * @return
 * @throws Exception
 */
public static HashMap<String, Object> parseParams(Element parent) throws Exception {
    XPath xpath = XPathFactory.newInstance().newXPath();
    // XPath Query for showing all nodes value
    XPathExpression expr = xpath.compile(_PARAM_NODE_PARAMS_);

    NodeList nodes = (NodeList) expr.evaluate(parent, XPathConstants.NODESET);
    if (nodes != null && nodes.getLength() > 0) {
        HashMap<String, Object> params = new HashMap<String, Object>();
        for (int ii = 0; ii < nodes.getLength(); ii++) {
            Element elm = (Element) nodes.item(ii);
            String name = elm.getAttribute(_PARAM_ATTR_NAME_);
            String value = elm.getAttribute(_PARAM_ATTR_VALUE_);
            if (name != null && !name.isEmpty()) {
                params.put(name, value);
            }
        }
        return params;
    }
    return null;
}

From source file:Main.java

public static Map<String, Element> getMBeanAttributes(Document document, String mbeanCode) {
    Map<String, Element> attrs = new HashMap<String, Element>();

    NodeList nlist = document.getElementsByTagName("mbean");

    Element beanNode = null;//from ww  w. j  av  a  2s .  c o m

    for (int i = 0; i < nlist.getLength(); i++) {
        Element elem = (Element) nlist.item(i);
        String attr = elem.getAttribute("code");
        if (mbeanCode.equals(attr)) {
            beanNode = elem;
            break;
        }
    }

    if (beanNode != null) {
        nlist = beanNode.getElementsByTagName("attribute");
        for (int i = 0; i < nlist.getLength(); i++) {
            Element attrElem = (Element) nlist.item(i);
            String attrName = attrElem.getAttribute("name");

            attrs.put(attrName, attrElem);
        }
    }

    return attrs;
}