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

public static List<Element> getElementCollectionByTagName(Element element, String tagName) {
    List<Element> result = new LinkedList<Element>();
    NodeList nodeList = element.getElementsByTagName(tagName);
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            result.add((Element) node);
        }/*ww  w .  j  a v  a 2 s  . c o m*/
    }
    return result;
}

From source file:Main.java

/**
 * Gets the immediately child elements list from the parent element.
 * //from  w  ww  .j a  va2 s .  co  m
 * @param parent the parent element in the element tree
 * @param tagName the specified tag name
 * @return the NOT NULL immediately child elements list
 */
public static List<Element> getChildElements(Element parent, String tagName) {
    NodeList nodes = parent.getElementsByTagName(tagName);
    List<Element> elements = new ArrayList<Element>();

    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node instanceof Element && node.getParentNode() == parent) {
            elements.add((Element) node);
        }
    }

    return elements;
}

From source file:Main.java

/**
 * Helper function for quickly retrieving an boolean value of a given
 * XML element./*w w w  . j a va 2  s  .com*/
 * @param ele The document element from which to pull the boolean value.
 * @param tagName The name of the node.
 * @return The boolean value of the given node in the element passed in.
 */
public static boolean getBooleanValue(Element ele, String tagName) {

    boolean boolVal = false;
    NodeList nl = ele.getElementsByTagName(tagName);

    if (nl != null && nl.getLength() > 0) {
        Element el = (Element) nl.item(0);
        boolVal = el.getFirstChild().getNodeValue().equals("true");
    }

    return boolVal;
}

From source file:Main.java

public static Element getSingleOptionalChildElement(Element parent, Enum<?> child) {
    final String elName = getXmlName(child);
    final NodeList list = parent.getElementsByTagName(elName);
    if (list.getLength() == 1) {
        return (Element) list.item(0);
    }//from w ww  .j  a v a  2  s . co m
    if (list.getLength() == 0) {
        return null;
    }
    throw new IllegalArgumentException("expected a single " + elName + " child element of XML element "
            + parent.getNodeName() + ", but found " + list.getLength());
}

From source file:Main.java

public static String getStringFromParagraphElement(Element element) {

    StringBuffer value = new StringBuffer();

    NodeList nl = element.getElementsByTagName("p");

    for (int i = 0; i < nl.getLength(); i++) {

        NodeList children = nl.item(i).getChildNodes();
        for (int c = 0; c < children.getLength(); c++) {
            if (children.item(c) instanceof Text) {
                value.append(((Text) children.item(c)).getData());
            }/*from w ww . j  a va2 s  .  c  om*/
        }
        if (i != nl.getLength() - 1) {
            value.append("\n");
        }
    }

    return value.toString();
}

From source file:Main.java

public static String getElementValue(Element element, String tagName, Integer n) {
    NodeList elementLst = element.getElementsByTagName(tagName);
    if (elementLst.item(n) != null) {
        NodeList elementData = ((Element) elementLst.item(n)).getChildNodes();
        if (elementData.item(0) != null) {
            return ((Node) elementData.item(0)).getNodeValue();
        }/*from  w  ww .  j  a va2  s  .c  om*/
    }
    return "";
}

From source file:Main.java

static public String getNodeValue(Element parent, String nodeName) {
    NodeList nodes = parent.getElementsByTagName(nodeName);
    if (nodes.getLength() == 0)
        return null;
    Element curElem = (Element) nodes.item(0);
    Node textNode = curElem.getFirstChild();
    if (textNode != null) {
        String encVal = curElem.getAttribute("enc");
        String charSetVal = curElem.getAttribute("charSet");
        String returnVal = textNode.getNodeValue();
        if (encVal != null && encVal.equals("t")) {
            if (charSetVal == null)
                return (URLDecoder.decode(returnVal));
            else/*  ww w  .  j a  v a 2s .  co m*/
                try {
                    return (URLDecoder.decode(returnVal, charSetVal));
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }
        return (returnVal);
    } else
        return ("");
}

From source file:Main.java

/**
 * Gets the descendant elements list from the parent element.
 * /*from ww w  . j av a 2  s .  c  o  m*/
 * @param parent the parent element in the element tree
 * @param tagName the specified tag name
 * @return the NOT NULL descendant elements list
 */
public static List<Element> getElements(Element parent, String tagName) {
    NodeList nodes = parent.getElementsByTagName(tagName);
    List<Element> elements = new ArrayList<Element>();

    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node instanceof Element) {
            elements.add((Element) node);
        }
    }

    return elements;
}

From source file:Main.java

public static Element getElementByTagNameAndAttributeValue(Element element, String tagName, String attrName,
        String attrValue) {/*from ww w. ja v  a2s  . c  o m*/
    NodeList nodeList = element.getElementsByTagName(tagName);
    if (nodeList.getLength() == 0) {
        return null;
    }
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element nodeElement = (Element) node;
            if (attrValue.equals(getAttributeValueByName(nodeElement, attrName))) {
                return nodeElement;
            }
        }
    }
    return null;
}

From source file:Main.java

private static String getTextValue(String def, Element doc, String tag) {
    String value = def;//from   w w  w  .j a  v  a  2s.  c o m
    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");
                }
                //               System.out.println("First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent());
                //               System.out.println("Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent());                 
            } else {
                System.out.println("not element");
            }
        }

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