List of usage examples for org.w3c.dom Element getTagName
public String getTagName();
From source file:Main.java
public static Element fetchSubElement(Element e, String subElementName) { if (e == null || subElementName == null) { return null; }//from w w w. j a v a 2 s .com subElementName = subElementName.toUpperCase(); NodeList list = e.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { if (list.item(i) instanceof Element) { Element element = (Element) list.item(i); if (element.getTagName().toUpperCase().equals(subElementName)) { return element; } } } return null; }
From source file:Main.java
/** * Given an XML element, this method returns nested elements which are direct * children of the given element - but only those that have the element-name "childrenName" (the second parameter). * @param element//from ww w . ja v a 2 s .c o m * @param childrenName * @return */ public static List<Element> getChildElements(Element element, String childrenName) { List<Element> ret = new LinkedList<Element>(); NodeList nodeList = element.getChildNodes(); for (int index = 0; index < nodeList.getLength(); ++index) { Node node = nodeList.item(index); if (node instanceof Element) { Element elementOfNode = (Element) node; if (elementOfNode.getTagName().equals(childrenName)) { ret.add(elementOfNode); } } } return ret; }
From source file:Utils.java
/** * <p>Returns a list of child elements with the given * name. Returns an empty list if there are no such child * elements.</p>/*from w ww . j a v a 2 s .c o m*/ * * @param parent parent element * @param name name of the child element * @return child elements */ public static List getChildElementsByName(Element parent, String name) { List elements = new ArrayList(); NodeList children = parent.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node node = children.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) node; if (element.getTagName().equals(name)) { elements.add(element); } } } return elements; }
From source file:Main.java
/** * Parses an XML file and returns the name of the document element. * @param file the file containing the XML to parse. * @return the name of the document element, or null if the file does not parse. */// w ww. j av a 2 s .c om public static String getDocumentElementName(File file) { try { DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document d = db.parse(file); Element root = d.getDocumentElement(); return root.getTagName(); } catch (Exception e) { return null; } }
From source file:Main.java
public static void genericRemoveAll(Element parent, String tagname) { NodeList nl = parent.getChildNodes(); List<Element> parts = new ArrayList<Element>(); for (int i = 0; i < nl.getLength(); i++) { if (nl.item(i) instanceof Element) { Element elem = (Element) nl.item(i); if (elem.getTagName().equals(tagname)) parts.add(elem);/* ww w. j ava 2 s . c o m*/ } } for (Element part : parts) parent.removeChild(part); }
From source file:Main.java
private static void traverseNodes(Element parent, String tagName, List<Element> lst) { if (parent.getTagName().indexOf(tagName) != -1) { lst.add(parent);//from w w w . ja va2 s.co m return; } else { NodeList nodeList = parent.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node item = nodeList.item(i); if (item.getNodeType() == Node.ELEMENT_NODE) { traverseNodes((Element) item, tagName, lst); } } } }
From source file:Main.java
public static List<Element> elements(Element root, String tagName) { List<Element> L = new ArrayList<Element>(); if (root == null) return L; for (Node n1 = root.getFirstChild(); n1 != null; n1 = n1.getNextSibling()) { if (n1.getNodeType() != Node.ELEMENT_NODE) continue; Element e2 = Element.class.cast(n1); if (e2.getTagName().equals(tagName)) { L.add(e2);//from w w w. java 2s . co m } } return L; }
From source file:Main.java
public static Vector<Element> getChildList(Element elem, String elementName) { if (elem == null) return null; NodeList nl = elem.getChildNodes(); if (nl == null) return null; Vector<Element> retlist = new Vector<Element>(100); for (int n = 0; n < nl.getLength(); n++) { Element element = (Element) nl.item(n); if (elementName.equals(element.getTagName())) { retlist.addElement(element); }/* w w w. j a va 2 s .c om*/ } if (retlist.size() == 0) return null; return retlist; }
From source file:Main.java
/** * Returns a list of all the tags in an XML document * /*from w ww .ja v a 2 s. c o m*/ * @param doc * @return list of all tags */ public static List<String> getAllTags(Document doc) { List<String> tags = new ArrayList<String>(); NodeList ns = doc.getElementsByTagName("*"); for (int i = 0; i < ns.getLength(); i++) { Element element = (Element) ns.item(i); tags.add(element.getTagName()); } return tags; }
From source file:Main.java
/** * Takes a number of tags of name 'name' that are children of 'root', and * looks for attributes of 'attrib' on them. Converts attributes to an * int and returns in an array.//from w ww . j a v a 2 s . c o m */ public static String[] getElementArrayString(Element root, String name, String attrib) { if (root == null) return null; NodeList nl = root.getChildNodes(); LinkedList<String> elementCache = new LinkedList<String>(); int size = nl.getLength(); for (int i = 0; i < size; i++) { Node node = nl.item(i); if (!(node instanceof Element)) continue; Element ele = (Element) node; if (!ele.getTagName().equals(name)) continue; String valS = ele.getAttribute(attrib); elementCache.addLast(valS); } String[] retArr = new String[elementCache.size()]; Iterator<String> it = elementCache.iterator(); int idx = 0; while (it.hasNext()) { retArr[idx++] = it.next(); } return retArr; }