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
private static void registerIDs(Document doc, Node node, Map<String, Node> idMap) { if (node.getNodeType() == Node.ELEMENT_NODE) { if (node.getAttributes().getNamedItem("id") != null) { final String id = node.getAttributes().getNamedItem("id").getNodeValue(); idMap.put(id, (Element) node); }//from ww w .j a v a2s . c om } NodeList children = node.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { registerIDs(doc, children.item(i), idMap); } }
From source file:Main.java
public static String getTextValue(Element ele, String tagName) { String textVal = null;/* www . j ava 2 s .co m*/ NodeList nl = ele.getElementsByTagName(tagName); if (nl != null && nl.getLength() > 0) { Element el = (Element) nl.item(0); if (el == null) return null; if (el.getFirstChild() == null) return null; textVal = el.getFirstChild().getNodeValue(); } return textVal; }
From source file:Main.java
public static List<Node> listize(final NodeList list) { return new AbstractList<Node>() { public Node get(int i) { return list.item(i); }/*from w w w. j av a 2 s. c o m*/ public int size() { return list.getLength(); } }; }
From source file:Main.java
private static void findNodesNamed(Node node, String lookForName, Collection<Node> ret) { if (node.getNodeName().equals(lookForName)) { ret.add(node);/*from w ww . j av a 2 s .c o m*/ } else { NodeList list = node.getChildNodes(); for (int i = 0; i < list.getLength(); ++i) { findNodesNamed(list.item(i), lookForName, ret); } } }
From source file:Main.java
public static List<Element> getChildElementsNS(Element parent, String uri) { List<Element> ret = new ArrayList<Element>(); NodeList childList = parent.getChildNodes(); for (int i = 0; i < childList.getLength(); i++) { if (childList.item(i).getNodeType() != Node.ELEMENT_NODE) continue; Element child = (Element) childList.item(i); if (child.getNamespaceURI().equals(uri)) ret.add(child);/*from ww w . j a v a2 s . co m*/ } return ret; }
From source file:Main.java
/** * Recursively removes all text nodes containing whitespace only from a document element. Also * trims leading and trailing whitespace from text nodes. * /*from w ww. ja v a2s . co m*/ * @param e The root document element. */ 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); else if (child instanceof Text) child.setTextContent(((Text) child).getData().trim()); else if (child instanceof Element) removeWhitespaceNodes((Element) child); } }
From source file:Main.java
/** * Preskoci uvodni komentare apod./*from w ww. j a v a 2 s. c o m*/ * * @param node * @return prvni subelement (jediny - korenovy - element v dokumentu) */ public static Element getFirstElement(Node node) { NodeList childNodes = node.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node childNode = childNodes.item(i); if (childNode instanceof Element) { return (Element) childNode; } } return null; }
From source file:Main.java
/** * Retrieves the value of a child node given the name of the child node. * /*from www . j av a 2 s.co m*/ * @param node * @param childNodeName * @return */ public static String getChildValue(Node node, String childNodeName) { NodeList children = node.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node n = children.item(i); if (n.getNodeName().equals(childNodeName)) { return getNodeValue(n); } } return null; }
From source file:Main.java
public static Node getChildNode(Node node, String childNodeName) { final NodeList children = node.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { final Node childNode = children.item(i); if (childNode.getNodeName().equals(childNodeName)) { return childNode; }/*from w ww. j a va2s .c om*/ } return null; }
From source file:Main.java
private static List<String> getFemaleEmployeesName(Document doc, XPath xpath) throws Exception { List<String> list = new ArrayList<>(); XPathExpression expr = xpath.compile("/Employees/Employee[gender='Female']/name/text()"); // evaluate expression result on XML document NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); i++) list.add(nodes.item(i).getNodeValue()); return list;/*from w w w . j av a 2s . c o m*/ }