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
/** * Get the first instance of an element by name. * //from ww w.j av a2s . c o m * @param parent * The parent to get the element from. * @param elementName * The name of the element to look for. * @return The element or null if it is not found. */ public static Element getElement(Element parent, String elementName) { Element retval = null; final NodeList children = parent.getElementsByTagName(elementName); if (children.getLength() > 0) { retval = (Element) children.item(0); } return retval; }
From source file:Main.java
public static void visitRecursively(Node node, BufferedWriter bw) throws Exception { NodeList list = node.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { // get child node Node childNode = list.item(i); if (childNode.getNodeType() == Node.TEXT_NODE) { System.out.println("Found Node: " + childNode.getNodeName() + " - with value: " + childNode.getNodeValue() + " Node type:" + childNode.getNodeType()); String nodeValue = childNode.getNodeValue(); nodeValue = nodeValue.replace("\n", "").replaceAll("\\s", ""); if (!nodeValue.isEmpty()) { System.out.println(nodeValue); bw.write(nodeValue);/*from www . j av a2 s . c om*/ bw.newLine(); } } visitRecursively(childNode, bw); } }
From source file:Main.java
public static boolean hasChild(Element parent, String nodeName) { NodeList childNodes = parent.getChildNodes(); int length = childNodes.getLength(); for (int i = 0; i < length; i++) if (childNodes.item(i).getNodeName().equals(nodeName)) return true; return false; }
From source file:Main.java
private static Node ifNodeExists(Node upper, String path) { NodeList list = upper.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { if (list.item(i).getNodeName().equals(path)) return list.item(i); }/*from w w w . j a v a 2 s. c o m*/ return null; }
From source file:Main.java
public static Element getChildElement(Element p_rootElement, String p_elementName) { NodeList nodeList = p_rootElement.getElementsByTagName(p_elementName); // if (nodeList.getLength() >= 0) return (Element) nodeList.item(0); // return null; }
From source file:Main.java
public static String getNodeContent(Document document, String nodeName) { NodeList nl = document.getElementsByTagName(nodeName); if (0 == nl.getLength()) { return ""; } else {/*from ww w .j a v a2 s. c o m*/ return nl.item(0).getTextContent(); } }
From source file:Main.java
/** * Returns a List of all descendant Element nodes having the specified * [namespace name] property. The elements are listed in document order. * * @param node The node to search from./*from w ww. j av a2s. com*/ * @param namespaceURI An absolute URI denoting a namespace name. * @return A List containing elements in the specified namespace; the list * is empty if there are no elements in the namespace. */ public static List<Element> getElementsByNamespaceURI(Node node, String namespaceURI) { List<Element> list = new ArrayList<Element>(); NodeList children = node.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (child.getNodeType() != Node.ELEMENT_NODE) { continue; } if (child.getNamespaceURI().equals(namespaceURI)) { list.add((Element) child); } } return list; }
From source file:Main.java
/** * Gets <code>CDATASection</code> element for node. * * @param node node with text.// w w w. j a v a 2s. co m * @return text, that contains the specified node. */ public static String getNodeCDATASection(final Node node) { // node.normalize(); final NodeList list = node.getChildNodes(); for (int i = 0, len = list.getLength(); i < len; i++) { final Node child = list.item(i); if (child.getNodeType() == Node.CDATA_SECTION_NODE) return child.getNodeValue(); } return null; }
From source file:Main.java
public static Node findByID(Document doc, String id, String tagName) { NodeList node = doc.getElementsByTagName(tagName); for (int i = 0; i < node.getLength(); i++) { NamedNodeMap attributtes = node.item(i).getAttributes(); for (int j = 0; j < attributtes.getLength(); j++) if (id.equalsIgnoreCase(attributtes.item(j).getNodeValue())) return node.item(i); }/*from ww w . j a v a 2s.com*/ return null; }
From source file:Main.java
/** * Returns the first node that is a direct child of root with the coresponding * name. Does not search children of children. *///w ww. j ava 2 s . c o m public static Element getFirstChild(Element root, String name) { NodeList nl = root.getChildNodes(); 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)) return ele; } return null; }