1 33 package groovy.xml.dom; 34 35 import org.w3c.dom.Element ; 36 import org.w3c.dom.NodeList ; 37 38 41 public class DOMCategory { 42 43 public static Object get(Element element, String elementName) { 44 return getAt(element, elementName); 45 } 46 47 public static Object getAt(Element element, int i) { 48 if (element.hasChildNodes()) { 49 NodeList nodeList = element.getChildNodes(); 50 return nodeList.item(i); 51 } 52 return null; 53 } 54 55 public static Object getAt(Element element, String elementName) { 56 if (elementName.startsWith("@")) { 57 String attrName = elementName.substring(1); 58 return element.getAttribute(attrName); 59 } 60 if (element.hasChildNodes()) { 61 NodeList nodeList = element.getChildNodes(); 62 for (int i = 0; i < nodeList.getLength(); i++) { 63 Object node = nodeList.item(i); 64 if (node instanceof Element ) { 65 Element child = (Element ) node; 66 child.hasChildNodes(); 67 if(child.getTagName().equals(elementName)) { 68 return child; 69 } 70 } 71 } 72 } 73 return null; 74 } 75 76 } | Popular Tags |