List of usage examples for org.w3c.dom Element getTagName
public String getTagName();
From source file:Main.java
/** * Get all the direct children elements of an element that have a specific * tag name.//ww w . j a va 2 s.c o m * * @param parent * The parent element. * @param name * The tag name to match. * * @return A list of Element's. */ public static List<Element> getElements(final Element parent, final String name) { final LinkedList<Element> list = new LinkedList<Element>(); Node node = parent.getFirstChild(); while (node != null) { if (node.getNodeType() == Node.ELEMENT_NODE) { final Element element = (Element) node; if (element.getTagName().equals(name)) { list.add(element); } } node = node.getNextSibling(); } return list; }
From source file:Main.java
/** * To elements./*from w w w . j a v a 2s. co m*/ * * @param nodes the nodes * @param filter the filter * @return the list */ private static List<Element> toElements(NodeList nodes, String filter) { List<Element> list = new ArrayList<Element>(); for (int i = 0; i < nodes.getLength(); i++) if (nodes.item(i) instanceof Element) { Element e = (Element) nodes.item(i); if (filter == null || e.getTagName().equals(filter)) { list.add(e); } } return list; }
From source file:Main.java
public static Map<String, String> NodeListToMap(NodeList nList) { Map<String, String> simpleMap = new HashMap<String, String>(); for (int i = 0; i < nList.getLength(); i++) { Node nNode = nList.item(i); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; if (eElement != null) { simpleMap.put(eElement.getTagName(), eElement.getTextContent()); } // end if eelement } // end if nnode.getnodetype() } //end for int temp return simpleMap; }
From source file:Main.java
public static List getChildrenByTagName(Node parent, String tagName) { ArrayList elements = new ArrayList(); NodeList children = parent.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (child instanceof Element) { Element elem = (Element) children.item(i); if (tagName.equals(elem.getTagName())) { elements.add(elem);//from www .j ava 2 s .com } } } return elements; }
From source file:Main.java
private static void outputElement(Element node, String indent) { System.out.print(indent + "<" + node.getTagName()); NamedNodeMap nm = node.getAttributes(); for (int i = 0; i < nm.getLength(); i++) { Attr attr = (Attr) nm.item(i); System.out.print(" " + attr.getName() + "=\"" + attr.getValue() + "\""); }/*w w w. j a v a2 s. com*/ System.out.print(">"); NodeList list = node.getChildNodes(); for (int i = 0; i < list.getLength(); i++) outputloop(list.item(i), indent + TAB); System.out.println(indent + "</" + node.getTagName() + ">"); }
From source file:Main.java
/** * @param element The element whose ancestry we will check * @param tagName The tagName of the element we are searching for in the ancestry * @param limitTagName Stop searching if we hit this limit * @return The first matching element, if found */// w w w .ja va 2s . c o m public static Element getAncestorOrSelf(final Element element, final String tagName, final String limitTagName) { Element result = null; Element next = element; String currentTagName; do { currentTagName = next.getTagName(); if (currentTagName.equals(tagName)) { result = next; break; } else { Node parent = next.getParentNode(); if (parent != null && parent.getNodeType() == Node.ELEMENT_NODE) { next = (Element) parent; } else { break; } } } while (next != null && (limitTagName == null || !tagName.equals(limitTagName))); return result; }
From source file:Main.java
public static Hashtable<String, String> getChildHash(Element elem) { if (elem == null) return null; NodeList nl = elem.getChildNodes(); if (nl == null) return null; Hashtable<String, String> retlist = new Hashtable<String, String>(nl.getLength()); for (int n = 0; n < nl.getLength(); n++) { Node child = nl.item(n);/*w w w . j a v a 2 s. co m*/ if (child instanceof Element) { Element element = (Element) child; String key = element.getTagName(); String value = getSimpleElementText(element); retlist.put(key, value); } } if (retlist.size() == 0) return null; return retlist; }
From source file:Main.java
public static Map<String, String> SimpleDocumentToMap(Document doc) { Map<String, String> simpleMap = new HashMap<String, String>(); //trim off outter layer Node node = doc.getFirstChild(); NodeList nList = node.getChildNodes(); for (int i = 0; i < nList.getLength(); i++) { Node nNode = nList.item(i); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; if (eElement != null) { simpleMap.put(eElement.getTagName(), eElement.getTextContent()); } // end if eelement } // end if nnode.getnodetype() } //end for int temp return simpleMap; }
From source file:Main.java
public static List<Element> getChildrenByTagName(Node parent, String tagName) { ArrayList<Element> elements = new ArrayList<Element>(); NodeList children = parent.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (child instanceof Element) { Element elem = (Element) children.item(i); if (tagName.equals(elem.getTagName())) { elements.add(elem);/* w w w .j a v a 2s . c o m*/ } } } return elements; }
From source file:Main.java
/** * Method getChildTags. Get all direct children of given element which * match the given tag./*w w w . j av a2 s . c o m*/ * This method only looks at the direct children of the given node, and * doesn't descent deeper into the tree. If a '*' is passed as tag, * all elements are returned. * * @param el Element where to get children from * @param tag Tag to match. Use '*' to match all tags. * @return Collection Collection containing all elements found. If * size() returns 0, then no matching elements * were found. All items in the collection can * be safely cast to type * <code>org.w3c.dom.Element</code>. */ public static Collection<Node> getChildTags(Element el, String tag) { Collection<Node> c; NodeList nl; int len; boolean allChildren; c = new LinkedList<Node>(); nl = el.getChildNodes(); len = nl.getLength(); if ("*".equals(tag)) { allChildren = true; } else { allChildren = false; } for (int i = 0; i < len; i++) { Node n = nl.item(i); if (n instanceof Element) { Element e = (Element) n; if (allChildren || e.getTagName().equals(tag)) { c.add(n); } } } return c; }