Posted By


vikiyou on 01/05/12

Tagged


Statistics


Viewed 178 times
Favorited by 0 user(s)

getNodesByTagName


/ Published in: Java
Save to your folder(s)

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


Copy this code and paste it in your HTML
  1. /**
  2.   * Get Nodes by element tag name from a Document
  3.   *
  4.   * @param parent - the document root element
  5.   * @param tagname - the tag name
  6.   * @param - list store the Elements
  7.   * @throws IllegalArgumentException
  8.   */
  9. public void getNodesByTagName(Element parent, String name, List<Element> nodesList) throws IllegalArgumentException {
  10. if (parent == null) {
  11. throw new IllegalArgumentException("The input node can not be null");
  12. }
  13.  
  14. NodeList children = parent.getChildNodes();
  15. Node node = null;
  16. for (int i = 0; i < children.getLength(); i++) {
  17. node = children.item(i);
  18. if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(name) {
  19. nodeList.add((Element) node);
  20. }
  21. if (node.hasChildNodes()) {
  22. getNodesByTagName((Element) node, name, nodesList);
  23. }
  24. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.