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 String getAttribute(Element element, String attributeName) {
    if (element == null) {
        return null;
    }/*w  ww .j a va 2  s.c  o m*/

    String attributeValue = element.getAttribute(attributeName);
    return attributeValue;
}

From source file:Main.java

public static void testx() {
    try {/*from   ww  w  .  j  a  v a2  s.  c o  m*/
        File fXmlFile = new File("C:\\Users\\is96092\\Desktop\\music\\Megaman2.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);

        //optional, but recommended
        //read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
        doc.getDocumentElement().normalize();

        System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

        NodeList nList = doc.getElementsByTagName("staff");

        System.out.println("----------------------------");

        for (int temp = 0; temp < nList.getLength(); temp++) {

            Node nNode = nList.item(temp);

            System.out.println("\nCurrent Element :" + nNode.getNodeName());

            if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                Element eElement = (Element) nNode;

                System.out.println("Staff id : " + eElement.getAttribute("id"));
                System.out.println(
                        "First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent());
                System.out.println(
                        "Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent());
                System.out.println(
                        "Nick Name : " + eElement.getElementsByTagName("nickname").item(0).getTextContent());
                System.out.println(
                        "Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent());

            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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>//  w ww .ja v  a  2s  .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 getParams(Element paramsElement) {
    LinkedHashMap params = new LinkedHashMap();

    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:Main.java

public static List<Element> findElementsByAttribute(Node node, String tagName, String attrName,
        String attrValue) {//from  w w  w.  j  av a 2  s . c o m
    List<Element> result = new ArrayList<Element>();
    NodeList nodeList = node.getChildNodes();
    if (nodeList == null) {
        return result;
    }
    for (int i = 0; i < nodeList.getLength(); ++i) {
        Element element = checkIfElement(nodeList.item(i), tagName);
        if (element != null && element.getAttribute(attrName).equals(attrValue)) {
            result.add(element);
        }
    }
    return result;
}

From source file:Main.java

/**
 * A convenience method for return a string from an element.  First, this
 * method checks to see if the the specified name exists as an attribute
 * in the element.  If it does, simply returns the attribute value.
 * Then this method checks to see if the specified element has a child
 * element that matches the given name.  If it does, attempts to read
 * the text from the child node.  Otherwise, returns <code>null</code>.
 * @param element/*from w w w.j  ava2s . co m*/
 * @param name
 * @return String
 */
public static String getAttributeOrChildText(Element element, String name) {
    if (element.hasAttribute(name)) {
        return element.getAttribute(name);
    }
    Element childElem = getChildElement(element, name);
    return childElem == null ? null : getChildText(childElem);
}

From source file:Main.java

/**
 * Get a double value from an attribute.
 * //  ww w  .j a va2s . co m
 * @param element the element to get the attribute from, may be null
 * @param attributeName the attribute name to get
 * @return the value, null if element is null or the attribute value is null
 *         or empty
 */
public static Double getDoubleAttributeValue(final Element element, final String attributeName) {
    if (null == element) {
        return null;
    }
    final String str = element.getAttribute(attributeName);
    if (str.isEmpty()) {
        return null;
    } else {
        return Double.valueOf(str);
    }
}

From source file:org.jboss.windup.config.spring.namespace.java.SpringNamespaceHandlerUtil.java

public static void parseNamespaceMap(BeanDefinitionBuilder builder, Element element) {

    List<Element> elements = XmlElementUtil.getChildrenByTagName(element, "namespace");
    if (elements == null) {
        return; // do nothing.
    }//w  w  w . j a  v  a  2s  .c  om

    ManagedMap<String, String> namespaceMap = new ManagedMap<String, String>(elements.size());
    for (Element bean : elements) {
        String prefix = bean.getAttribute("prefix");
        String namespace = bean.getAttribute("uri");

        namespaceMap.put(prefix, namespace);
    }
    LOG.debug("Adding namespaces: " + namespaceMap.size());
    builder.addPropertyValue("namespaces", namespaceMap);
}

From source file:com.samknows.measurement.schedule.condition.CpuActivityCondition.java

public static CpuActivityCondition parseXml(Element node) {
    CpuActivityCondition c = new CpuActivityCondition();
    c.maxAvg = Integer.valueOf(node.getAttribute("maxAvg"));
    String time = node.getAttribute("time");
    c.time = XmlUtils.convertTime(time);
    return c;//from  w  w w .  j a v a2  s  .  c  o m
}

From source file:Main.java

public static void ReadXMLFile()

{

    try {//from www .j  a  v  a2  s .  co  m

        File fXmlFile = new File("D:\\FAR_Documents\\__Startamap\\Home.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);

        //optional, but recommended
        //read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
        doc.getDocumentElement().normalize();

        System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

        NodeList nList = doc.getElementsByTagName("staff");

        System.out.println("----------------------------");

        for (int temp = 0; temp < nList.getLength(); temp++) {

            Node nNode = nList.item(temp);

            System.out.println("\nCurrent Element :" + nNode.getNodeName());

            if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                Element eElement = (Element) nNode;

                System.out.println("Staff id : " + eElement.getAttribute("id"));
                System.out.println(
                        "First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent());
                System.out.println(
                        "Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent());
                System.out.println(
                        "Nick Name : " + eElement.getElementsByTagName("nickname").item(0).getTextContent());
                System.out.println(
                        "Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent());

            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static synchronized String getAttribute(Element e, String name, String defvalue) {
    if (e == null || name == null || name.length() == 0) {
        return defvalue;
    } else {/*from w  w w  .j  ava  2 s .c  o m*/
        return e.getAttribute(name);
    }
}