Example usage for org.w3c.dom Element getElementsByTagName

List of usage examples for org.w3c.dom Element getElementsByTagName

Introduction

In this page you can find the example usage for org.w3c.dom Element getElementsByTagName.

Prototype

public NodeList getElementsByTagName(String name);

Source Link

Document

Returns a NodeList of all descendant Elements with a given tag name, in document order.

Usage

From source file:Main.java

private static String getTextValue(String def, Element doc, String tag) {
    String value = def;//from  www .j a  v a  2  s  .c om
    NodeList noteList = doc.getElementsByTagName(tag);
    if (noteList.getLength() > 0) {
        for (int i = 0; i < noteList.getLength(); i++) {
            Node note = noteList.item(i);

            Element noteElement = (Element) note;
            if (noteElement.getNodeType() == Node.ELEMENT_NODE) {
                if (noteElement.getElementsByTagName("pitch").getLength() > 0) {
                    Element pitchElement = (Element) noteElement.getElementsByTagName("pitch").item(0);
                    System.out.println("Staff idX : "
                            + pitchElement.getElementsByTagName("step").item(0).getTextContent());
                } else {
                    System.out.println("yoktir");
                }
            } else {
                System.out.println("not element");
            }
        }

        //          value = nl.item(0).getFirstChild().getNodeValue();
    }
    return value;
}

From source file:Main.java

/**
 * This methods extracts the text for a named element under the
 * <code>root</code>. For the moment it uses only the first text node,
 * and only the first element of that type.
 *
 * @param root    The element is contained within this root.
 * @param tagname The elemet name to search for
 * @return The text under the tag, or <code>null</code> if it is not found
 *         or an error occured./*from   ww w.  ja  va 2  s . com*/
 */
public static String getElementText(Element root, String tagname) {
    NodeList nameList = root.getElementsByTagName(tagname);

    if (nameList.getLength() > 0) {
        Node text = nameList.item(0).getFirstChild();

        // Find the first text node
        while ((text != null) && !(text instanceof Text)) {
            text = text.getNextSibling();
        }

        if (text == null) {
            return null;
        } else {
            try {
                return text.getNodeValue();
            } catch (DOMException de) {
                return null;
            }
        }
    } else {
        return null;
    }
}

From source file:Main.java

/**
 * Return list of content Strings of all child elements with given tag name.
 * @param elem//ww w.  java  2 s. c om
 * @param childTagName
 * @return List 
 */
public static List getChildTextList(Element elem, String childTagName) {
    NodeList nodeList = elem.getElementsByTagName(childTagName);
    int len = nodeList.getLength();
    if (len == 0) {
        return Collections.EMPTY_LIST;
    } else {
        List list = new ArrayList(len);
        for (int i = 0; i < len; i++) {
            list.add(getElementText((Element) nodeList.item(i)));
        }
        return list;
    }
}

From source file:Main.java

public static String getElementValue(String elementName, Element element) throws Exception {
    String value = null;//from   w  w  w . j a v  a  2 s .  co m
    NodeList childNodes = element.getElementsByTagName(elementName);
    Element cn = (Element) childNodes.item(0);
    value = cn.getFirstChild().getNodeValue().trim();
    //value = childNodes.item(0).getNodeValue().trim();
    if (value == null) {
        throw new Exception("No element found with given name:" + elementName);
    }
    return value;
}

From source file:Main.java

/**
 * @deprecated use getXPath instead!/*from   w w  w. ja  v a2s.c o  m*/
 */
@Deprecated
public static Element getElementByAttribute(Element root, String tagname, String attribute, String att_value) {
    NodeList nl = root.getElementsByTagName(tagname);
    for (int i = 0; i < nl.getLength(); i++) {
        Element elem = (Element) nl.item(i);
        if (elem.getAttribute(attribute).equals(att_value)) {
            return elem;
        }
    }
    return null;
}

From source file:Main.java

/**
 * Gets the text value of the specified element.
 * //from w ww . j ava2s .c o  m
 * @param root
 *            the root of the element whose text is to be retrieved; assumed
 *            not to be <code>null</code>.
 * @param elementName
 *            the name of the element whose text is to be retrieved.
 * 
 * @return the retrieved text value of the specified element or null if root
 *         has no given element.
 */
public static String getElementData(final Element root, final String elementName) {
    NodeList nodes = root.getElementsByTagName(elementName);
    if (nodes.getLength() < 1) {
        return null;
    }

    return getElementData(nodes.item(0));
}

From source file:Main.java

/** Helper method - gets a named element's values as an array of <I>String</I>
objects.//from   w  w  w .  ja  v a 2 s. c o  m
   @param pElement The parent <I>Element</I>.
   @param strName Name of the element.
   @return Array of <I>String</I> objects representing the values
  of the child elements.
 */
public static String[] getElementStrings(Element pElement, String strName) {
    NodeList pNodeList = pElement.getElementsByTagName(strName);

    if (null == pNodeList)
        return null;

    int nLength = pNodeList.getLength();

    if (0 == nLength)
        return null;

    String[] strReturn = new String[nLength];

    for (int i = 0; i < nLength; i++)
        strReturn[i] = getNodeText(pNodeList.item(i));

    return strReturn;
}

From source file:Main.java

public static String getValue(Element item, String tag) {
    String textValue = null;//from ww  w .j a  v a 2 s .  c  o  m

    NodeList nodes = item.getElementsByTagName(tag);
    if (nodes != null && nodes.getLength() > 0) {
        Element element = (Element) nodes.item(0);

        Node node = element.getFirstChild();

        if (node != null) {
            textValue = node.getNodeValue();
        }
    }

    return textValue;
}

From source file:Main.java

/** Returns all 'subElement' of 'node'. */
public static List<Element> findAll(Element node, String subElement) {
    if (node != null) {
        NodeList nl = node.getElementsByTagName(subElement);
        if (nl != null && nl.getLength() > 0) {
            int n = nl.getLength();
            ArrayList<Element> ans = new ArrayList<Element>(n);
            for (int i = 0; i < n; ++i)
                ans.add((Element) nl.item(i));
            return ans;
        }/*w  w w .  j  av  a 2s .c  o  m*/
    }
    return java.util.Collections.<Element>emptyList();
}

From source file:Main.java

/**
 * Helper function for quickly retrieving a String value of a given
 * XML element./*from   w w w  . j  a  va  2 s .c om*/
 * @param ele The document element from which to pull the String value.
 * @param tagName The name of the node.
 * @return The String value of the given node in the element passed in.
 */
public static String getTextValue(Element ele, String tagName) {
    String textVal = null;
    NodeList nl = ele.getElementsByTagName(tagName);
    if (nl != null && nl.getLength() > 0) {
        Element el = (Element) nl.item(0);
        if (el.getFirstChild() != null) {
            textVal = el.getFirstChild().getNodeValue();
        } else {
            textVal = "";
        }
    }
    return decode(textVal);
}