List of usage examples for org.w3c.dom Element getTagName
public String getTagName();
From source file:Main.java
/** * Get the elements who's tags match those passed in. * @param nodeList - the node list in which to look for elements * @param tags - the tags to match (empty returns all elements). * @return The elements that matched.//from ww w . ja v a2 s . co m */ public static List<Element> getElements(NodeList nodeList, String... tags) { int nNodes = nodeList.getLength(); List<Element> elements = new ArrayList<Element>(nNodes); Set<String> tagSet = new HashSet<String>(tags.length); for (String tag : tags) { tagSet.add(tag); } for (int i = 0; i < nNodes; ++i) { Node node = nodeList.item(i); if (node instanceof Element) { Element element = (Element) node; String tagName = element.getTagName(); if (tagSet.isEmpty() || tagSet.contains(tagName)) { elements.add(element); } } } return elements; }
From source file:Main.java
public static String elementToString(Element e) { StringBuilder buf = new StringBuilder(); buf.append(e.getTagName()).append("{"); for (int i = 0; i < e.getAttributes().getLength(); i++) { if (i > 0) { buf.append(", "); }/* w ww .j a va 2 s . c o m*/ Node n = e.getAttributes().item(i); buf.append(attributeName((Attr) n)).append("=").append(n.getNodeValue()); } buf.append("}"); return buf.toString(); }
From source file:Main.java
/** * Gets a child node by the given name.//from w w w .ja v a 2s . c om * * @param node the node * @param name the name of the child node to find and return * @return the child node, or <tt>null</tt> if none found */ public static Node getChildNodeByTagName(Node node, String name) { NodeList children = node.getChildNodes(); if (children != null && children.getLength() > 0) { for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (child instanceof Element) { Element element = (Element) child; if (name.equals(element.getTagName())) { return element; } } } } return null; }
From source file:Main.java
/** * Returns a list of children (class Element) with the given tag name *//*w w w .j a v a 2s .co m*/ public static List<Element> getChilden(final Element element, final String tagName) { final List<Element> children = new ArrayList<Element>(); final NodeList nodes = element.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { final Node item = nodes.item(i); if (!(item instanceof Element)) { continue; } final Element elem = (Element) item; if (tagName.equals(elem.getTagName())) { children.add(elem); } } return children; }
From source file:Main.java
/** * Returns the first child element for the given name *//*w ww .j a v a 2 s . c o m*/ public static Element firstChild(Element element, String name) { NodeList nodes = element.getChildNodes(); if (nodes != null) { for (int i = 0, size = nodes.getLength(); i < size; i++) { Node item = nodes.item(i); if (item instanceof Element) { Element childElement = (Element) item; if (name.equals(childElement.getTagName())) { return childElement; } } } } return null; }
From source file:Main.java
/** * Search all child nodes of the given for the first element that has the * specified tag name./*from w w w . jav a 2s . c om*/ * * @param aStartNode * The parent element to be searched. May not be <code>null</code>. * @param sName * The tag name to search. * @return <code>null</code> if the parent element has no such child element. */ @Nullable public static Element getFirstChildElementOfName(@Nonnull final Node aStartNode, @Nullable final String sName) { final NodeList aNodeList = aStartNode.getChildNodes(); final int nLen = aNodeList.getLength(); for (int i = 0; i < nLen; ++i) { final Node aNode = aNodeList.item(i); if (aNode.getNodeType() == Node.ELEMENT_NODE) { final Element aElement = (Element) aNode; if (aElement.getTagName().equals(sName)) return aElement; } } return null; }
From source file:Main.java
/** * Retrieves the given DOM element's child elements with the specified name. * If name is null, all children are retrieved. *//*from www . j av a 2 s . c om*/ public static Element[] getChildren(final Element el, final String name) { final Vector v = new Vector(); final NodeList nodes = el.getChildNodes(); final int len = nodes.getLength(); for (int i = 0; i < len; i++) { final Node node = nodes.item(i); if (!(node instanceof Element)) continue; final Element e = (Element) node; if (name == null || e.getTagName().equals(name)) v.add(e); } final Element[] els = new Element[v.size()]; v.copyInto(els); return els; }
From source file:Main.java
public static final Element checkIfElement(Node node, String tag) { Element result = null;/* w w w .j a v a2s. co m*/ if (isElement(node)) { Element tmp = (Element) node; if (tag == null || tmp.getTagName().equals(tag)) { result = tmp; } } return result; }
From source file:Main.java
public static String nodeToString(Object node) { String result = ""; if (node instanceof String) { String str = ((String) node).replaceAll("\\s+", " ").trim(); result += str;// w ww .ja v a 2s. co m } else if (node instanceof Text) { String str = ((Text) node).getTextContent().replaceAll("\\s+", " ").trim(); result += str; } else if (node instanceof Element) { Element en = (Element) node; result += "<" + en.getTagName(); for (int j = 0; j < en.getAttributes().getLength(); j++) { Attr attr = (Attr) en.getAttributes().item(j); //if (!(attr.getNamespaceURI() != null && attr.getNamespaceURI().equals("http://www.w3.org/2000/xmlns/") && (attr.getLocalName().equals("xmlns") || attr.getLocalName().equals("xsi")))) { result += " " + attr.getName() + "=\"" + attr.getValue() + "\""; //} } if (en.getChildNodes().getLength() == 0) { result += "/>"; } else { result += ">"; ArrayList<Object> children = new ArrayList<Object>(); for (int i = 0; i < en.getChildNodes().getLength(); i++) { children.add(en.getChildNodes().item(i)); } result += nodesToString(children); result += "</" + en.getTagName() + ">"; } } return result; }
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 www . ja va2s . co m*/ */ public static int[] getElementArrayInt(Element root, String name, String attrib) { if (root == null) return null; NodeList nl = root.getChildNodes(); LinkedList<Integer> elementCache = new LinkedList<Integer>(); 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); int eleVal = 0; try { eleVal = Integer.parseInt(valS); } catch (Exception e) { } elementCache.addLast(new Integer(eleVal)); } int[] retArr = new int[elementCache.size()]; Iterator<Integer> it = elementCache.iterator(); int idx = 0; while (it.hasNext()) { retArr[idx++] = it.next().intValue(); } return retArr; }