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 boolean attributeBooleanGet(Element e, String name, boolean defaultValue) {
    if (!e.hasAttribute(name)) {
        return defaultValue;
    } else {//from  ww w.j  ava 2  s.co m
        return Boolean.parseBoolean(e.getAttribute(name));
    }
}

From source file:Utils.java

public static Element findContainerWithAttributeValueElseCreate(Document document, Element parent,
        String element, String attributeName, String attributeValue) {

    NodeList nl = parent.getElementsByTagName(element);
    Element e;
    for (int i = 0; i < nl.getLength(); i++) {
        e = (Element) nl.item(i);
        if (e.getAttribute(attributeName).equals(attributeValue)) {
            return e;
        }//from   ww w.ja v a  2 s.c  om
    }

    e = document.createElement(element);
    parent.appendChild(e);
    e.setAttribute(attributeName, attributeValue);

    return e;
}

From source file:Main.java

/**
 * Get the value of an attribute./*  w ww .j a  va  2  s.  com*/
 * @param e Element to use
 * @param attributeName name of the attribute
 * @return the value of the attribute
 */
public static String getAttributeValue(final Element e, final String attributeName) {

    if (e == null || attributeName == null) {
        return null;
    }

    return e.getAttribute(attributeName);
}

From source file:com.joshlong.esb.springintegration.modules.social.twitter.config.TwitterNamespaceHandler.java

private static TwitterMessageType handleParsingMessageType(BeanDefinitionBuilder builder, Element element,
        ParserContext parserContext) {//from   w w  w.j  a  v  a 2  s.c  om
    String typeAttr = element.getAttribute("type");

    if (!StringUtils.isEmpty(typeAttr)) {
        if (typeAttr.equalsIgnoreCase(FRIENDS)) {
            return TwitterMessageType.FRIENDS;
        }

        if (typeAttr.equalsIgnoreCase(MENTIONS)) {
            return TwitterMessageType.MENTIONS;
        }

        if (typeAttr.equalsIgnoreCase(DIRECT_MESSAGES)) {
            return TwitterMessageType.DM;
        }
    }

    return TwitterMessageType.FRIENDS;
}

From source file:com.evolveum.midpoint.util.UglyHacks.java

public static void unfortifyNamespaceDeclarationsSingleElement(Element element) {
    String fortifiedXmlnss = element.getAttribute(FORTIFIED_NAMESPACE_DECLARATIONS_ELEMENT_NAME);
    if (element.hasAttribute(FORTIFIED_NAMESPACE_DECLARATIONS_ELEMENT_NAME)) {
        element.removeAttribute(FORTIFIED_NAMESPACE_DECLARATIONS_ELEMENT_NAME);
    }// w w w.j a  va  2  s.com
    if (StringUtils.isBlank(fortifiedXmlnss)) {
        return;
    }
    String[] xmlnss = StringUtils.split(fortifiedXmlnss, FORTIFIED_NAMESPACE_DECLARATIONS_SEPARATOR);
    for (String xmlns : xmlnss) {
        String[] prefixAndUrl = StringUtils.split(xmlns, "=", 2);
        String prefix = prefixAndUrl[0];
        String url = prefixAndUrl[1];
        if (!DOMUtil.hasNamespaceDeclarationForPrefix(element, prefix)) {
            DOMUtil.setNamespaceDeclaration(element, prefix, url);
        }
    }
}

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.//from  w  w w  .  j a v a2 s .  c  om
 */
public static String[] getElementArrayString(Element root, String name, String attrib) {
    if (root == null)
        return null;

    NodeList nl = root.getChildNodes();
    LinkedList<String> elementCache = new LinkedList<String>();
    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);

        elementCache.addLast(valS);
    }

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

    return retArr;
}

From source file:Main.java

/**
 * This method will find all the parameters under this <code>paramsElement</code> and return them as
 * Map<String, String>. For example,
 * <pre>/*from w w w  . j a va2  s.c o m*/
 *   <result ... >
 *      <param name="param1">value1</param>
 *      <param name="param2">value2</param>
 *      <param name="param3">value3</param>
 *   </result>
 * </pre>
 * will returns a Map<String, String> with the following key, value pairs :-
 * <ul>
 * <li>param1 - value1</li>
 * <li>param2 - value2</li>
 * <li>param3 - value3</li>
 * </ul>
 *
 * @param paramsElement
 * @return
 */
public static Map<String, String> getParams(Element paramsElement) {
    LinkedHashMap<String, String> params = new LinkedHashMap<String, String>();

    if (paramsElement == null) {
        return params;
    }

    NodeList childNodes = paramsElement.getChildNodes();

    for (int i = 0; i < childNodes.getLength(); i++) {
        Node childNode = childNodes.item(i);

        if ((childNode.getNodeType() == Node.ELEMENT_NODE) && "param".equals(childNode.getNodeName())) {
            Element paramElement = (Element) childNode;
            String paramName = paramElement.getAttribute("name");

            String val = getContent(paramElement);
            if (val.length() > 0) {
                params.put(paramName, val);
            }
        }
    }

    return params;
}

From source file:ar.com.zauber.commons.conversion.spring.schema.SimplePropertyFieldDefinitionParser.java

/** agrega el constructor del {@link FieldSetSetterStrategy} */
static void configureSetter(final BeanDefinitionBuilder bean, final Element element) {
    if (element.hasAttribute("setter")) {
        final String s = element.getAttribute("setter");
        if (s.equals("setter")) {
            bean.addConstructorArgValue(FieldSetterStrategies.FIELD_SETTER_STRATEGY);
        } else if (s.equals("collection-add")) {
            bean.addConstructorArgValue(FieldSetterStrategies.COLLECTION_ADD_STRATEGY);
        } else {//  w  w w. ja  va  2  s.  c o  m
            throw new IllegalStateException("Unknown setter named " + s);
        }
    }
}

From source file:Main.java

public static void overrideXmlList(NodeList target, String targetTag, NodeList parent, String parentTag) {

    for (int i = 0; i < target.getLength(); i++) {
        Element targetColumn = (Element) target.item(i);
        Element parentColumn = null;

        String targetTagValue = targetColumn.getAttribute(targetTag);
        for (int j = 0; j < parent.getLength(); j++) {

            if (targetTagValue.equals(((Element) parent.item(j)).getAttribute(parentTag))) {
                parentColumn = (Element) parent.item(j);
                break;
            }/*  ww w.ja v  a 2  s. co m*/
        }

        if (parentColumn != null) {
            overrideXml(targetColumn, parentColumn);
        }
    }
}

From source file:com.opensymphony.xwork2.config.providers.XmlHelper.java

/**
 * Return the value of the "order" attribute from the root element
 *//*from  w w  w . ja  v  a2  s  .  c om*/
public static Integer getLoadOrder(Document doc) {
    Element rootElement = doc.getDocumentElement();
    String number = rootElement.getAttribute("order");
    if (StringUtils.isNotBlank(number)) {
        try {
            return Integer.parseInt(number);
        } catch (NumberFormatException e) {
            return Integer.MAX_VALUE;
        }
    } else {
        //no order specified
        return Integer.MAX_VALUE;
    }
}