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 Iterator getElementsByTagName(Element element, String tag) {
    ArrayList<Element> children = new ArrayList<Element>();
    if (element != null && tag != null) {
        NodeList nodes = element.getElementsByTagName(tag);
        for (int i = 0; i < nodes.getLength(); i++) {
            Node child = nodes.item(i);
            //      System.out.println("Name: " + child.getNodeName() + ", Value: " + child.getFirstChild().getNodeValue());
            children.add((Element) child);
        }// w  w  w  .j a  v a2 s . co  m
    }
    return children.iterator();
}

From source file:Utils.java

/**
 * Return a list of named Elements with a specific attribute value.
 * @param element the containing Element
 * @param name the tag name//  w  w w .j a v  a 2  s.co  m
 * @param attribute Attribute name
 * @param value Attribute value
 * @param returnFirst Return only the first matching value?
 * @return List of matching elements
 */
public static List selectElementsByAttributeValue(Element element, String name, String attribute, String value,
        boolean returnFirst) {
    NodeList elementList = element.getElementsByTagName(name);
    List resultList = new ArrayList();

    for (int i = 0; i < elementList.getLength(); i++) {
        if (getAttribute((Element) elementList.item(i), attribute).equals(value)) {
            resultList.add(elementList.item(i));
            if (returnFirst) {
                break;
            }
        }
    }
    return resultList;
}

From source file:Main.java

private static Element fixupAttrs(Element root) { // #140905
    // #6529766/#6531160: some versions of JAXP reject attributes set using setAttribute
    // (rather than setAttributeNS) even though the schema calls for no-NS attrs!
    // JDK 5 is fine; JDK 6 broken; JDK 6u2+ fixed
    // #146081: xml:base attributes mess up validation too.
    Element copy = (Element) root.cloneNode(true);
    fixupAttrsSingle(copy);/*from ww  w  .j  a  va  2s. co m*/
    NodeList nl = copy.getElementsByTagName("*"); // NOI18N
    for (int i = 0; i < nl.getLength(); i++) {
        fixupAttrsSingle((Element) nl.item(i));
    }
    return copy;
}

From source file:com.github.fritaly.svngraph.Utils.java

public static boolean hasChild(Element root, String childName) {
    return (root.getElementsByTagName(childName).getLength() > 0);
}

From source file:Main.java

public static List<Element> getElementsByTagName(Element element, String tagName) {
    ArrayList<Element> children = new ArrayList<Element>();
    if (element != null && tagName != null) {
        NodeList nodes = element.getElementsByTagName(tagName);
        for (int i = 0; i < nodes.getLength(); i++) {
            Node child = nodes.item(i);
            children.add((Element) child);
        }// w ww.  j a  va 2 s . co m
    }
    return children;
}

From source file:Main.java

/**
 * Checks in under a given root element whether it can find a child element
 * which matches the name supplied. Returns {@link Element} if exists.
 * /*ww w  .j  a va2 s. c  om*/
 * @param name the Element name (required)
 * @param root the parent DOM element (required)
 * 
 * @return the Element if discovered
 */
public static Element findFirstElementByName(String name, Element root) {
    //      Assert.hasText(name, "Element name required");
    //      Assert.notNull(root, "Root element required");
    return (Element) root.getElementsByTagName(name).item(0);
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static Iterator getElementsByTagName(Element element, String tag) {
    ArrayList<Element> children = new ArrayList<Element>();
    if (element != null && tag != null) {
        NodeList nodes = element.getElementsByTagName(tag);
        for (int i = 0; i < nodes.getLength(); i++) {
            Node child = nodes.item(i);
            //      System.out.println("Name: " + child.getNodeName() + ", Value: " + child.getFirstChild().getNodeValue());
            children.add((Element) child);
        }/*  ww  w .  j  a  v a 2  s  .  c o m*/
    }
    return children.iterator();
}

From source file:Main.java

public static String SearchForElementText(Element element, String name, String defaultValue) {
    if (element != null && name != null && !name.isEmpty()) {
        NodeList nodes = element.getElementsByTagName(name);

        if (nodes != null && nodes.getLength() > 0) {
            return nodes.item(0).getTextContent();
        }// w  w  w  .j  a  v  a2  s . c om
    }

    return defaultValue;
}

From source file:in.raster.oviyam.util.ReadXMLFile.java

private static String getTagValue(String sTag, Element element) {
    NodeList nodeList = element.getElementsByTagName(sTag).item(0).getChildNodes();
    Node node = (Node) nodeList.item(0);

    if (node != null) {
        return node.getNodeValue();
    } else {//  w ww.jav  a2  s. com
        return "";
    }
}

From source file:Main.java

public static List<Element> getElementsByTagNames(Node node, String... tagName) {
    List<Element> nds = new ArrayList<Element>();
    if (tagName.length == 0)
        tagName = new String[] { "" };
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        Element doc = (Element) node;
        for (String elementName : tagName) {
            nds.addAll(toElementList(doc.getElementsByTagName(elementName)));
        }/*from  ww  w .  j  ava  2  s. c om*/
    } else if (node instanceof Document) {
        Document doc = ((Document) node);
        for (String elementName : tagName) {
            nds.addAll(toElementList(doc.getElementsByTagName(elementName)));
        }
    } else if (node instanceof DocumentFragment) {
        Document doc = ((DocumentFragment) node).getOwnerDocument();
        for (String elementName : tagName) {
            nds.addAll(toElementList(doc.getElementsByTagName(elementName)));
        }
    } else {
        throw new IllegalArgumentException("a node who doesn't support getElementsByTagName operation.");
    }
    return nds;
}