List of usage examples for org.w3c.dom NodeList item
public Node item(int index);
index
th item in the collection. From source file:Main.java
public static List<String> parseXmlFile(String nodeName, Document file) { List<String> nodeTexts = new ArrayList<String>(); NodeList nodes = file.getElementsByTagName(nodeName); for (int index = 0; index < nodes.getLength(); ++index) { Node node = nodes.item(index).getFirstChild(); String nodeText = node.getNodeValue(); nodeTexts.add(nodeText);/*from ww w .j av a2 s. c om*/ } return nodeTexts; }
From source file:Main.java
public static Iterator getElementsByTagName(Element element, String tag) { ArrayList<Element> children = new ArrayList<Element>(); if (element != null && tag != null) { NodeList nodes = element.getElementsByTagName(tag); for (int i = 0; i < nodes.getLength(); i++) { Node child = nodes.item(i); // System.out.println("Name: " + child.getNodeName() + ", Value: " + child.getFirstChild().getNodeValue()); children.add((Element) child); }/* w ww . ja v a2s. c o m*/ } return children.iterator(); }
From source file:Main.java
public static void removeWhitespaceNodes(Element e) { NodeList children = e.getChildNodes(); for (int i = children.getLength() - 1; i >= 0; i--) { Node child = children.item(i); if (child instanceof Text && ((Text) child).getData().trim().length() == 0) { e.removeChild(child);//w ww. ja va 2 s . com } else if (child instanceof Element) { removeWhitespaceNodes((Element) child); } } }
From source file:Main.java
public static List<Element> getElements(final Element element, final String tagName) { final ArrayList<Element> elements = new ArrayList<>(); final NodeList children = element.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { final Node node = children.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { final String nodeName = node.getNodeName(); if ((nodeName != null) && nodeName.equals(tagName)) { elements.add((Element) node); }/*ww w . j ava 2s . co m*/ } } return elements; }
From source file:Main.java
/** * Checks if a <code>Node</code> has any descendant <code>Element</code> nodes. * // w w w.j av a 2 s . c om * @param n <code>Node</code> to be examined * * @return boolean */ public static boolean containsElements(Node n) { NodeList children = n.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { if (children.item(i) instanceof Element) { return true; } } return false; }
From source file:Main.java
/** * This will get the text value of an element. * * @param node The node to get the text value for. * @return The text of the node.//from ww w . ja va2 s . c o m */ public static String getNodeValue(Element node) { String retval = ""; NodeList children = node.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node next = children.item(i); if (next instanceof Text) { retval = next.getNodeValue(); } } return retval; }
From source file:Main.java
public static Element[] filterChildElements(Element parent, String ns, String lname) { List<Element> elems = new ArrayList<Element>(); NodeList nlst = parent.getChildNodes(); for (int i = 0, nlen = nlst.getLength(); i < nlen; i++) { Node n = nlst.item(i); if (n instanceof Element) { Element e = (Element) n; if ((ns == null || ns.equals(e.getNamespaceURI())) && (lname == null || lname.equals(e.getLocalName()))) { elems.add(e);//w w w . j a v a 2 s . co m } } } return elems.toArray(new Element[elems.size()]); }
From source file:Main.java
/** * Gets the first child Element with a given name * /*from w w w .ja v a 2 s. c om*/ * @param n * the node get the children from * @param elementName * the name of the child elements * @return the first child Element with a given name */ public static Element getElementByTagName(Element n, String elementName) { NodeList subNodes = n.getElementsByTagName(elementName); int sz = subNodes.getLength(); if (sz > 0) return (Element) subNodes.item(0); return null; }
From source file:Main.java
public static List<Element> getChildElements(final Element element) { final List<Element> elements = new ArrayList<Element>(); final NodeList childs = element.getChildNodes(); for (int i = 0; i < childs.getLength(); i++) { Node child = childs.item(i); if (child instanceof Element) { elements.add((Element) child); }//from w ww .ja v a 2 s .c o m } return elements; }