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 Element getFirstChild(Element paramElement, String paramString1, String paramString2,
        String paramString3) {/*from  ww w . j a va2 s  .c om*/
    NodeList localNodeList = paramElement.getChildNodes();
    for (int i = 0; i < localNodeList.getLength(); i++) {
        Node localNode = localNodeList.item(i);
        if ((localNode != null) && (localNode.getNodeType() == 1)
                && (((Element) localNode).getTagName().equals(paramString1))) {
            Element localElement = (Element) localNode;
            if ((localElement.hasAttribute(paramString2))
                    && (paramString3.equals(localElement.getAttribute(paramString2))))
                return localElement;
        }
    }
    return null;
}

From source file:edu.indiana.lib.twinpeaks.search.PreferredUrlHandler.java

/**
 * Fetch the preferred URL.  This method is invoked for every result record.
 *
 * @param connector Connector name// w  ww.j a  v  a  2 s.  co m
 * @param dataElement The result record (DATA element) to inspect
 * @return The preferred URL (null if none)
 */
public static String getUrl(String connector, Element dataElement) {
    /*
     * Stop now if this connector can't provide a preferred URL
     */
    if (!_preferredUrlConnectors.contains(connector)) {
        return null;
    }
    /*
     * American Social History Online
     *
     * An example:
     *
     *    <DATA>
     *      ...
     *      <IDENTIFIER scheme="URL>http://preferred/url/here</IDENTIFIER>
     *      ...
     *    </DATA>
     *
     * We know each result record will have only one of these.
     */
    if (connector.equals(ASHO_CONNECTOR)) {
        NodeList nodeList = DomUtils.getElementList(dataElement, "IDENTIFIER");
        String url = null;

        for (int i = 0; i < nodeList.getLength(); i++) {
            Element element = (Element) nodeList.item(i);

            if (element.getAttribute("scheme").equals("URL")) {
                url = DomUtils.getText(element);

                if (StringUtils.isNull(url)) {
                    url = null;
                }
                break;
            }
        }
        return url;
    }
    /*
     * A known connector and we didn't handle it?
     */
    return null;
}

From source file:Main.java

/**
 * Takes a number of tags of name 'name' that are children of 'root', and
 * looks for attributes of 'attrib' on them.  Converts attributes to an
 * int and returns in an array./* w  w w . ja va 2s.c o m*/
 */
public static int[] getElementArrayInt(Element root, String name, String attrib) {
    if (root == null)
        return null;

    NodeList nl = root.getChildNodes();
    LinkedList<Integer> elementCache = new LinkedList<Integer>();
    int size = nl.getLength();

    for (int i = 0; i < size; i++) {
        Node node = nl.item(i);
        if (!(node instanceof Element))
            continue;
        Element ele = (Element) node;
        if (!ele.getTagName().equals(name))
            continue;

        String valS = ele.getAttribute(attrib);
        int eleVal = 0;
        try {
            eleVal = Integer.parseInt(valS);
        } catch (Exception e) {
        }

        elementCache.addLast(new Integer(eleVal));
    }

    int[] retArr = new int[elementCache.size()];
    Iterator<Integer> it = elementCache.iterator();
    int idx = 0;
    while (it.hasNext()) {
        retArr[idx++] = it.next().intValue();
    }

    return retArr;
}

From source file:com.samknows.measurement.schedule.datacollection.BaseDataCollector.java

public static BaseDataCollector parseXml(Element node) {
    BaseDataCollector c = null;// w  w w. ja v a 2  s . c o  m
    try {
        Type type = Type.valueOf(node.getAttribute("type"));
        switch (type) {
        case Location: {
            c = LocationDataCollector.parseXml(node);
            break;
        }
        case Environment: {
            c = new EnvironmentDataCollector();
            break;
        }
        default:
            Logger.e(BaseDataCollector.class, "not such data collector: " + type);
        }
    } catch (Exception e) {
        Logger.e(BaseDataCollector.class, "Error in parsing data collector type: " + e.getMessage());
    }

    if (c != null) {
        c.isEnabled = true;
        if (!node.getAttribute("enabled").equals("")) {
            c.isEnabled = Boolean.valueOf(node.getAttribute("enabled"));
        }

    }

    return c;
}

From source file:XmlUtil.java

/**
 * Get element's attribute value or <code>null</code> if attribute not found or empty.
 *///from w w w.j a v a 2  s.c om
public static String getAttributeValue(Element element, String name) {
    String value = element.getAttribute(name);
    if (value.length() == 0) {
        value = null;
    }
    return value;
}

From source file:io.cloudslang.schema.WorkerBeanDefinitionParser.java

private static void registerSpecialBeans(Element element, ParserContext parserContext) {
    if (!"false".equalsIgnoreCase(element.getAttribute("register"))) {
        new BeanRegistrator(parserContext).CLASS(WorkerRegistration.class).register();
    }/* w ww  . ja  v  a2s .  c  o m*/

    registerWorkerVersionService(element, parserContext);
}

From source file:Main.java

public static Element getChildWithAttributeValue(Element element, String attributeName, String attributeValue) {

    if (element == null) {
        return null;
    }/* w w  w  .ja  va2s .c om*/

    NodeList children;

    try {
        children = element.getChildNodes();
    } catch (IllegalArgumentException e) {
        // We've seen this throw a IllegalArgumentException from
        // com.sun.org.apache.xerces.internal.dom.DeferredDocumentImpl.getNodeObject(DeferredDocumentImpl.java:1081)
        // under GWT 1.7

        return null;
    }

    int length;

    try {
        length = children.getLength();
    } catch (NullPointerException e) {
        // We've seen this throw a NullPointerException from
        // com.sun.org.apache.xerces.internal.dom.ParentNode.nodeListGetLength(ParentNode.java:696)
        // under GWT 1.7

        return null;
    }

    for (int loop = 0; loop < length; loop++) {
        Node node;

        try {
            node = children.item(loop);
        } catch (NullPointerException e) {
            // We've seen this throw a NullPointerException from
            // com.sun.org.apache.xerces.internal.dom.ParentNode.nodeListItem(ParentNode.java:780)
            // under GWT 1.7

            continue;
        }

        if (!(node instanceof Element)) {
            continue;
        }

        Element child = (Element) node;

        try {
            if (attributeValue.equals(child.getAttribute(attributeName))) {
                return child;
            }
        } catch (NullPointerException e) {
            // We've seen this throw a NullPointerException from
            // com.sun.org.apache.xerces.internal.dom.DeferredAttrNSImpl.synchronizeData(DeferredAttrNSImpl.java:97)
            // under GWT 1.7

            continue;
        }
    }

    return null;
}

From source file:biz.webgate.tools.urlfetcher.URLFetcher.java

private static String calculateImageHeighBased(NodeList ndlImage, int maxPictureHeigh, Element elCurrent) {
    String strImage = elCurrent.getAttribute("src");
    if (ndlImage.getLength() > 20 && elCurrent.hasAttribute("height")) {
        String strHeight = elCurrent.getAttribute("height");
        strHeight = strHeight.replace("px", "");
        try {//from   w w w  . j  av  a  2  s.com
            int nHeight = Integer.parseInt(strHeight);
            if (nHeight > maxPictureHeigh) {
                strImage = null;
            }
        } catch (Exception e) {
            // TODO:handle exception
        }
    }
    return strImage;
}

From source file:org.xacml4j.spring.FunctionProvidersDefinitionParser.java

private static BeanDefinitionBuilder parseComponent(Element element) {
    BeanDefinitionBuilder component = BeanDefinitionBuilder.rootBeanDefinition(FunctionProvider.class);
    String clazz = element.getAttribute("class");
    if (StringUtils.hasText(clazz)) {
        component.addPropertyValue("providerClass", clazz);
    }//from  w  w w. ja va2  s .  c o m
    String ref = element.getAttribute("ref");
    if (StringUtils.hasText(ref)) {
        component.addPropertyReference("providerInstance", ref);
    }
    return component;
}

From source file:Main.java

public static Integer getCurrentLevelAttributeIntValue(Element ele, String attribute) {
    Integer attributeInt = null;/*from  w  w w  .  j  a v a2  s  .c  om*/
    if (ele == null || attribute == null) {
        return attributeInt;
    }
    String attributeString = ele.getAttribute(attribute);
    if (attributeString != null) {
        attributeInt = new Integer(attributeString);
    }
    return attributeInt;
}