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:it.cilea.osd.common.utils.XMLUtils.java

public static String getElementAttribute(Element dataRoot, String name, String attr) {
    NodeList nodeList = dataRoot.getElementsByTagName(name);
    Element element = null;
    if (nodeList != null && nodeList.getLength() > 0) {
        element = (Element) nodeList.item(0);
    }/*from  w w w . ja v a2 s .co  m*/

    String attrValue = null;
    if (element != null) {
        attrValue = element.getAttribute(attr);
        if (StringUtils.isNotBlank(attrValue)) {
            attrValue = attrValue.trim();
        } else
            attrValue = null;
    }
    return attrValue;
}

From source file:com.moadbus.banking.iso.core.data.DataSetConfigParser.java

/**
 * Reads the XML from the stream and configures the dataset factory with its
 * values.//from  ww  w. j a  va  2 s  . co  m
 *
 * @param mfact
 *            The dataset factory to be configured with the values read from
 *            the XML.
 * @param stream
 *            The InputStream containing the XML configuration.
 */
protected static void parse(DataSetFactory mfact, InputStream stream) throws IOException {
    final DocumentBuilderFactory docfact = DocumentBuilderFactory.newInstance();
    DocumentBuilder docb = null;
    Document doc = null;
    try {
        docb = docfact.newDocumentBuilder();
        doc = docb.parse(stream);
    } catch (ParserConfigurationException ex) {
        log.error("parse: Cannot parse XML configuration", ex);
        return;
    } catch (SAXException ex) {
        log.error("parse: Parsing XML configuration", ex);
        return;
    }
    final Element root = doc.getDocumentElement();

    // Read the ISO headers
    NodeList nodes = null;

    // Read the parsing guides
    nodes = root.getElementsByTagName("parse");
    for (int i = 0; i < nodes.getLength(); i++) {
        Element elem = (Element) nodes.item(i);
        String msgtype = elem.getAttribute("msgtype");
        MessageType messageType = MessageType.valueOf(msgtype);
        if (messageType == null) {
            throw new IOException("Invalid type for parse guide: " + elem.getAttribute("msgtype"));
        }
        NodeList fields = elem.getElementsByTagName("field");
        /*
         * HashMap<Integer, DataElementParseInfo> parseMap = new
         * HashMap<Integer, DataElementParseInfo>(); for (int j = 0; j <
         * fields.getLength(); j++) { Element f = (Element) fields.item(j);
         * int num = Integer.parseInt(f.getAttribute("num"));
         * DataElementType datatype =
         * DataElementType.valueOf(f.getAttribute("datatype")); int length =
         * 0, decimallength = 0; if (f.getAttribute("length").length() > 0)
         * { length = Integer.parseInt(f.getAttribute("length")); } if
         * (f.getAttribute("decimallength").length() > 0) { decimallength =
         * Integer.parseInt(f.getAttribute("decimallength")); } String
         * description = elem.getAttribute("description"); parseMap.put(num,
         * new DataElementParseInfo(datatype, length, description,
         * decimallength)); } mfact.setParseMap(messageType, parseMap);
         */

        HashMap<Integer, DataElementParseInfoList> parseMap = new HashMap<Integer, DataElementParseInfoList>();
        for (int j = 0; j < fields.getLength(); j++) {
            Element f = (Element) fields.item(j);
            DataElementParseInfoList fieldList = new DataElementParseInfoList();
            int num = 0;
            if (f.getAttribute("num").length() > 0) {
                num = Integer.parseInt(f.getAttribute("num"));
            }
            DataElementType datatype = DataElementType.valueOf(f.getAttribute("datatype"));
            int length = 0, decimallength = 0;
            if (f.getAttribute("length").length() > 0) {
                length = Integer.parseInt(f.getAttribute("length"));
            }
            if (f.getAttribute("decimallength").length() > 0) {
                decimallength = Integer.parseInt(f.getAttribute("decimallength"));
            }
            String description = f.getAttribute("description");
            String repeatsStr = f.getAttribute("repeats");
            if (repeatsStr != null && repeatsStr.length() > 0) {
                int repeats = -1;
                try {
                    repeats = Integer.parseInt(repeatsStr);
                } catch (Exception e) {
                    log.error("parse: " + e.getMessage(), e);
                }
                fieldList.setRepeats(repeats);
            }

            NodeList subfields = f.getElementsByTagName("subfield");

            if (subfields != null && subfields.getLength() == 0) {
                // System.out.println(f+" have subfields "+subfields);
                fieldList.add(new DataElementParseInfo(datatype, length, description, decimallength));
            } else {
                for (int k = 0; k < subfields.getLength(); k++) {
                    Element subfield = (Element) subfields.item(k);
                    /*int snum = 0;
                    if(subfield.getAttribute("num").length()>0)
                    {
                       snum = Integer.parseInt(subfield
                          .getAttribute("num"));
                    }*/
                    DataElementType sdatatype = DataElementType.valueOf(subfield.getAttribute("datatype"));
                    int slength = 0, sdecimallength = 0;
                    if (subfield.getAttribute("length").length() > 0) {
                        slength = Integer.parseInt(subfield.getAttribute("length"));
                    }
                    if (subfield.getAttribute("decimallength").length() > 0) {
                        sdecimallength = Integer.parseInt(subfield.getAttribute("decimallength"));
                    }
                    String sdescription = subfield.getAttribute("description");
                    fieldList.add(new DataElementParseInfo(sdatatype, slength, sdescription, sdecimallength));
                }

            }
            parseMap.put(num, fieldList);
        }
        mfact.setParseMap(messageType, parseMap);
    }

}

From source file:com.mtgi.analytics.aop.config.TemplateBeanDefinitionParser.java

/**
 * Convenience method to update a template bean definition from overriding XML data.  
 * If <code>overrides</code> contains attribute <code>attribute</code>, transfer that
 * attribute onto <code>template</code>, overwriting the default value.
 *///from w  ww  .  j ava  2 s . c  om
public static String overrideAttribute(String attribute, BeanDefinition template, Element overrides) {
    String value = (String) template.getAttribute(attribute);
    if (overrides.hasAttribute(attribute)) {
        value = overrides.getAttribute(attribute);
        template.setAttribute(attribute, value);
    }
    return value;
}

From source file:Main.java

/**
 * I take a xml element and the tag name, look for the tag and get
 * the text content/*www.ja v  a  2 s .  c om*/
 * i.e for <employee><name>John</name></employee> xml snippet if
 * the Element points to employee node and tagName is 'name' I will return John
 */
public static String getTextValue(Element ele, String tagName) {

    // Look for elements first
    NodeList nl = ele.getElementsByTagName(tagName);
    if (nl != null && nl.getLength() > 0) {
        Element el = (Element) nl.item(0);
        return el.getFirstChild().getNodeValue();
    }

    // If there are no elements look for an attribute
    return ele.getAttribute(tagName);
}

From source file:Main.java

/**
 * Returns a set with all attribute values for the specified attributes within the element.
 * @param expectedChild/*from www  . ja v  a2s  . c om*/
 * @param identifyingAttributes
 * @return
 */
private static String[] getAttributeValues(Element element, String[] identifyingAttributes) {
    ArrayList<String> values = new ArrayList<String>();
    for (int i = 0; i < identifyingAttributes.length; i++) {
        values.add(element.getAttribute(identifyingAttributes[i]));
    }
    return values.toArray(new String[values.size()]);
}

From source file:com.joyveb.dbpimpl.cass.prepare.util.ParsingUtils.java

public static void setPropertyValue(BeanDefinitionBuilder builder, Element element, String attrName,
        String propertyName) {//from www .ja  v  a2 s.  co  m

    Assert.notNull(builder, "BeanDefinitionBuilder must not be null");
    Assert.notNull(element, "Element must not be null");
    Assert.hasText(attrName, "Attribute name must not be null");
    Assert.hasText(propertyName, "Property name must not be null");

    String attr = element.getAttribute(attrName);

    if (StringUtils.hasText(attr)) {
        builder.addPropertyValue(propertyName, attr);
    }
}

From source file:com.evolveum.midpoint.prism.util.PrismUtil.java

public static void unfortifyNamespaceDeclarations(Element definitionElement) {
    Map<String, String> namespaces = new HashMap<String, String>();
    for (Element childElement : DOMUtil.listChildElements(definitionElement)) {
        if (PrismConstants.A_NAMESPACE.equals(DOMUtil.getQName(childElement))) {
            String prefix = childElement.getAttribute(PrismConstants.A_NAMESPACE_PREFIX);
            String namespace = childElement.getAttribute(PrismConstants.A_NAMESPACE_URL);
            namespaces.put(prefix, namespace);
            definitionElement.removeChild(childElement);
        } else {/*from   www . ja  va  2s .  c  o  m*/
            unfortifyNamespaceDeclarations(definitionElement, childElement, namespaces);
            namespaces = new HashMap<String, String>();
        }
    }
}

From source file:com.hdsfed.cometapi.XMLHelper.java

public static Map<String, String> DocumentToMap(Document doc) {
    Map<String, String> taglistMap = new HashMap<String, String>();
    NodeList nList = doc.getElementsByTagName("tag");
    for (int temp = 0; temp < nList.getLength(); temp++) {
        Node nNode = nList.item(temp);
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            Element eElement = (Element) nNode;
            if (eElement != null) {
                taglistMap.put(eElement.getAttribute("id"), XMLHelper.getTagValue("name", eElement) + "="
                        + XMLHelper.getTagValue("value", eElement));
            } // end if eelement
        } // end if nnode.getnodetype()
    } //end for int temp
    return taglistMap;
}

From source file:com.mtgi.analytics.aop.config.TemplateBeanDefinitionParser.java

/**
 * Convenience method to update a template bean definition from overriding XML data.  
 * If <code>overrides</code> contains attribute <code>attribute</code> or a child element
 * with name <code>attribute</code>, transfer that
 * attribute as a bean property onto <code>template</code>, overwriting the default value.
 * @param reference if true, the value of the attribute is to be interpreted as a runtime bean name reference; otherwise it is interpreted as a literal value
 *//*from   w w  w .j a  v a2 s.co  m*/
public static boolean overrideProperty(String attribute, BeanDefinition template, Element overrides,
        boolean reference) {
    Object value = null;
    if (overrides.hasAttribute(attribute)) {
        value = overrides.getAttribute(attribute);
    } else {
        NodeList children = overrides.getElementsByTagNameNS("*", attribute);
        if (children.getLength() == 1) {
            Element child = (Element) children.item(0);
            value = child.getTextContent();
        }
    }

    if (value != null) {
        if (reference)
            value = new RuntimeBeanReference(value.toString());

        String propName = Conventions.attributeNameToPropertyName(attribute);
        MutablePropertyValues props = template.getPropertyValues();
        props.removePropertyValue(propName);
        props.addPropertyValue(propName, value);
        return true;
    }

    return false;
}

From source file:flink.iso8583.parse.ConfigParser.java

/** Reads the XML from the stream and configures the message factory with its values.
 * @param mfact The message factory to be configured with the values read from the XML.
 * @param stream The InputStream containing the XML configuration. */
protected static void parse(MessageFactory mfact, InputStream stream) throws IOException {
    final DocumentBuilderFactory docfact = DocumentBuilderFactory.newInstance();
    DocumentBuilder docb = null;// w ww . ja  v a  2s .  c o m
    Document doc = null;
    try {
        docb = docfact.newDocumentBuilder();
        doc = docb.parse(stream);
    } catch (ParserConfigurationException ex) {
        log.error("Cannot parse XML configuration", ex);
        return;
    } catch (SAXException ex) {
        log.error("Parsing XML configuration", ex);
        return;
    }
    final Element root = doc.getDocumentElement();

    //Read the ISO headers
    NodeList nodes = root.getElementsByTagName("header");
    Element elem = null;
    for (int i = 0; i < nodes.getLength(); i++) {
        elem = (Element) nodes.item(i);
        int type = parseType(elem.getAttribute("type"));
        if (type == -1) {
            throw new IOException("Invalid type for header: " + elem.getAttribute("type"));
        }
        if (elem.getChildNodes() == null || elem.getChildNodes().getLength() == 0) {
            throw new IOException("Invalid header element");
        }
        String header = elem.getChildNodes().item(0).getNodeValue();
        if (log.isTraceEnabled()) {
            log.trace("Adding ISO header for type " + elem.getAttribute("type") + ": " + header);
        }
        mfact.setIsoHeader(type, header);
    }

    //Read the message templates
    nodes = root.getElementsByTagName("template");
    for (int i = 0; i < nodes.getLength(); i++) {
        elem = (Element) nodes.item(i);
        int type = parseType(elem.getAttribute("type"));
        if (type == -1) {
            throw new IOException("Invalid type for template: " + elem.getAttribute("type"));
        }
        NodeList fields = elem.getElementsByTagName("field");
        IsoMessage m = new IsoMessage();
        m.setType(type);
        for (int j = 0; j < fields.getLength(); j++) {
            Element f = (Element) fields.item(j);
            int num = Integer.parseInt(f.getAttribute("num"));
            IsoType itype = IsoType.valueOf(f.getAttribute("type"));
            int length = 0;
            if (f.getAttribute("length").length() > 0) {
                length = Integer.parseInt(f.getAttribute("length"));
            }
            String v = f.getChildNodes().item(0).getNodeValue();
            m.setValue(num, v, itype, length);
        }
        mfact.addMessageTemplate(m);
    }

    //Read the parsing guides
    nodes = root.getElementsByTagName("parse");
    for (int i = 0; i < nodes.getLength(); i++) {
        elem = (Element) nodes.item(i);
        int type = parseType(elem.getAttribute("type"));
        if (type == -1) {
            throw new IOException("Invalid type for parse guide: " + elem.getAttribute("type"));
        }
        NodeList fields = elem.getElementsByTagName("field");
        HashMap parseMap = new HashMap();
        for (int j = 0; j < fields.getLength(); j++) {
            Element f = (Element) fields.item(j);
            Integer num = new Integer(f.getAttribute("num"));
            IsoType itype = IsoType.valueOf(f.getAttribute("type"));
            int length = 0;
            if (f.getAttribute("length").length() > 0) {
                length = Integer.parseInt(f.getAttribute("length"));
            }
            parseMap.put(num, new FieldParseInfo(itype, length));
        }
        mfact.setParseMap(new Integer(type), parseMap);
    }

}