Return to Snippet

Revision: 54632
at January 5, 2012 12:59 by vikiyou


Updated Code
/**
  * Get Nodes by element tag name from a Document
  * 
  * @param parent - the document root element
  * @param tagname - the tag name
  * @param - list store the Elements
  * @throws IllegalArgumentException
  */
public void getNodesByTagName(Element parent, String name, List<Element> nodesList) throws IllegalArgumentException {
  if (parent == null) {
    throw new IllegalArgumentException("The input node can not be null");
  }

  NodeList children = parent.getChildNodes();
  Node node = null;
  for (int i = 0; i < children.getLength(); i++) {
    node = children.item(i);
    if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(name) {
      nodeList.add((Element) node);
    }
    if (node.hasChildNodes()) {
      getNodesByTagName((Element) node, name, nodesList); 
    }
}

Revision: 54631
at January 5, 2012 12:57 by vikiyou


Initial Code
/**
  * Get Nodes by element tag name from a Document
  * 
  * @param parent - the document's root element
  * @param tagname - the tag name
  * @param - list store the Elements
  * @throws IllegalArgumentException
  */
public void getNodesByTagName(Element parent, String name, List<Element> nodesList) throws IllegalArgumentException {
  if (parent == null) {
    throw new IllegalArgumentException("The input node can not be null");
  }

  NodeList children = parent.getChildNodes();
  Node node = null;
  for (int i = 0; i < children.getLength(); i++) {
    node = children.item(i);
    if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(name) {
      nodeList.add((Element) node);
    }
    if (node.hasChildNodes()) {
      getNodesByTagName((Element) node, name, nodesList); 
    }
}

Initial URL


Initial Description
Get the all nodes list which's element tag name specified from a document.

Initial Title
getNodesByTagName

Initial Tags


Initial Language
Java