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

/**
 * Returns the MIME type of the XSLT transformation output taken from the "media-type" attribute of the xsl:output element.
 * If the "media-type" attribute is not used it takes the "method" attribute of the same element. 
 * The following mappings between the "method" attribute values and MIME types are used
 * text -> text/plain/*from w  ww.  jav  a2 s.  co  m*/
 * xml  -> application/xml
 * html -> text/html
 * @param xslName
 * @return
 */
public static String getOutputMimeType(String xslName) {
    String mimeType = "";
    String mediaType = "";
    String method = "";
    getDocument(xslName);
    NodeList nList = xsl.getElementsByTagName("xsl:output");
    for (int i = 0; i < nList.getLength(); i++) {
        Node outputNode = nList.item(i);
        if (outputNode.getNodeType() == Node.ELEMENT_NODE) {
            Element outputElement = (Element) outputNode;
            mediaType = outputElement.getAttribute("media-type");
            method = outputElement.getAttribute("method");
        }
    }

    if ("".equals(mediaType)) {

        mimeType = ("text".equals(method)) ? "text/plain"
                : ("xml".equals(method)) ? "application/xml" : ("html".equals(method)) ? "text/html" : "";

    } else
        mimeType = mediaType;

    return mimeType;
}

From source file:Main.java

public static String resolveNamespacePrefix(String prefix, Element element) {
    String namespace = null;/*from  w  w  w. j  a  v  a  2s. c  om*/

    if ("".equals(prefix)) {
        namespace = element.getAttribute("xmlns");
    } else {
        namespace = element.getAttribute("xmlns:" + prefix);
    }
    if (namespace != null && !"".equals(namespace)) {
        return namespace;
    }

    if (element.getParentNode() instanceof Element) {
        return resolveNamespacePrefix(prefix, (Element) element.getParentNode());
    } else {
        throw new RuntimeException("Cannot resolve prefix " + prefix);
    }
}

From source file:com.bstek.dorado.config.xml.XmlParseException.java

private static String populateXmlAttribute(Element element, String attribute) {
    String value = element.getAttribute(attribute);
    if (StringUtils.isNotEmpty(value)) {
        return attribute + "=\"" + StringEscapeUtils.escapeXml(value) + "\"";
    } else {/*from w w  w .jav  a2 s.c  o  m*/
        return "";
    }
}

From source file:Main.java

/**
 * @see #getDoubleAttributeValue(Element, String)
 *///from  www.j  av  a 2 s.  c om
public static String getStringAttributeValue(final Element element, final String attributeName) {
    if (null == element) {
        return null;
    }
    final String str = element.getAttribute(attributeName);
    return str;
}

From source file:Main.java

public static String getCurrentLevelAttributeTextValue(Element ele, String attribute) {
    if (ele == null || attribute == null) {
        return null;
    } else {//from www .java 2s  .  co m
        return ele.getAttribute(attribute);
    }
}

From source file:com.developmentsprint.spring.breaker.config.BreakerNamespaceHandler.java

public static String extractCircuitManager(Element element) {
    return (element.hasAttribute(BreakerNamespaceHandler.CIRCUIT_MANAGER_ATTRIBUTE)
            ? element.getAttribute(BreakerNamespaceHandler.CIRCUIT_MANAGER_ATTRIBUTE)
            : BreakerNamespaceHandler.DEFAULT_CIRCUIT_MANAGER_BEAN_NAME);
}

From source file:Main.java

/**
 * Returns the attribute value or {@code null} if it does not exist.
 *///from w  ww .  j  av  a  2s  .  co  m
public static String getAttribute(Element element, Enum<?> attribute) {
    final String attName = getXmlName(attribute);
    final String attValue = element.getAttribute(attName);
    return attValue == null || attValue.isEmpty() ? null : attValue;
}

From source file:Main.java

private static void findAllElementsByAttribute(Node node, String tagName, String attrName, String attrValue,
        List<Element> result) {
    if (node == null) {
        return;/*from   w  ww . j a  v a2s.  c  om*/
    }
    NodeList nodeList = node.getChildNodes();
    if (nodeList == null) {
        return;
    }
    for (int i = 0; i < nodeList.getLength(); ++i) {
        Node currNode = nodeList.item(i);
        Element element = checkIfElement(currNode, tagName);
        if (element != null && element.getAttribute(attrName).equals(attrValue)) {
            result.add(element);
            continue;
        }
        findAllElementsByAttribute(currNode, tagName, attrName, attrValue, result);
    }
}

From source file:Main.java

private final static String getElement(String filePath, String key) throws Exception {

    String value = null;/*  w w w  .  jav a  2  s .co m*/

    try {

        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();

        File xmlFile = new File(filePath);

        Document document = documentBuilder.parse(xmlFile);

        Element resourcesElement = document.getDocumentElement();
        resourcesElement.normalize();

        NodeList strings = resourcesElement.getChildNodes();

        int stringsCount = strings.getLength();

        for (int i = 0; i < stringsCount; i++) {

            Node node = strings.item(i);

            short nodeType = node.getNodeType();

            if (nodeType == Node.ELEMENT_NODE) {

                Element element = (Element) node;

                String attribute = element.getAttribute("name");

                if (attribute.equals(key)) {

                    value = element.getTextContent();
                }
            }
        }

    } catch (Exception ex) {

        throw ex;
    }

    return value;
}

From source file:Main.java

public static Element findNodeByAttributeValue(NodeList nodeList, String attributeName, String attributeValue) {
    int len = nodeList.getLength();
    String tmp;/*www  .j av  a  2 s  . co  m*/
    Element property;
    for (int i = 0; i < len; i++) {
        property = (Element) nodeList.item(i);
        tmp = property.getAttribute(attributeName);
        if (attributeValue.equals(tmp)) {
            return property;
        }
    }
    return null;
}