List of utility methods to do XML Element Get by Name
Element | getElement(Element parent, String tagName) Gets the immediately descendant element from the parent element. List<Element> children = getElements(parent, tagName); if (children.isEmpty()) { return null; } else { return children.get(0); |
Element | getElement(Element parentElement, String nodeName) get Element NodeList list = parentElement.getElementsByTagName(nodeName); if (list == null || list.getLength() == 0) { return null; return (Element) list.item(0); |
Element | getElement(Node parent, int index) get Element int j = 0; NodeList nodes = parent.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); if (node instanceof Element) { if (j == index) { return (Element) node; j++; return null; |
Element | getElement(String elementName, Element parentElement) Get the specified element from a parent element. Element returnElement = null; NodeList nl; if ((nl = parentElement.getElementsByTagName(elementName)) != null) { returnElement = (Element) nl.item(0); return returnElement; |
Element | getElement(String name, int index, Element parent) get Element ArrayList<Element> toReturn = getElementsByTag(name, parent); if (index > toReturn.size() - 1) { return null; return toReturn.get(index); |
String | getElementByName(Document document, String name) get Element By Name NodeList nodes = document.getElementsByTagName(name); String s = null; for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); s = node.getTextContent().trim(); return s; |
Element | getElementByName(Element eParent, String elementName) Get an element from a parent element node NodeList nlParent = eParent.getElementsByTagName(elementName); for (int looper = 0; looper < nlParent.getLength(); looper++) { if (nlParent.item(looper).getNodeType() == Node.ELEMENT_NODE) { return (Element) nlParent.item(looper); return null; |
Element | getElementByName(Element parent, String nodeName) get Element By Name NodeList nodes = parent.getElementsByTagName(nodeName); if (nodes.getLength() == 0) throw new IllegalArgumentException("Cannot find node " + nodeName); return ((Element) nodes.item(0)); |
Element | getElementByTag(HTMLDocument htmlDoc, int offset, Tag tag) get Element By Tag Element e = htmlDoc.getCharacterElement(offset); while (e != null && !e.getAttributes().containsAttribute(StyleConstants.NameAttribute, tag)) { e = e.getParentElement(); return e; |
Element | getElementByTagAndName(Document document, String tagName, String elementName) get Element By Tag And Name NodeList nodeList = document.getElementsByTagName(tagName); Element element = null; if (elementName != null) { Element _element = null; for (int nodeIndex = 0; nodeIndex < nodeList.getLength(); nodeIndex++) { _element = (Element) nodeList.item(nodeIndex); String name = _element.getAttribute("name"); if (name != null && name.equals(elementName)) { ... |