List of usage examples for org.w3c.dom NodeList getLength
public int getLength();
From source file:Main.java
/** * Indicates whether element has any child element. * * @param element the namespace to analyze. * @return true if element has any child element otherwise false. */// ww w . j a v a2s . c om public static boolean hasChildElements(Element element) { NodeList childNodes = element.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node node = childNodes.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { return true; } } return false; }
From source file:Main.java
public static List<Node> getAllChildNodes(Node node) { if (node == null) return null; List<Node> result = new ArrayList<Node>(); NodeList nodelist = node.getChildNodes(); for (int i = 0; i < nodelist.getLength(); i++) { Node curnode = nodelist.item(i); int type = curnode.getNodeType(); if (type != Node.TEXT_NODE) result.add(nodelist.item(i)); List<Node> childlist = getAllChildNodes(curnode); if (childlist != null) result.addAll(childlist);//from w w w . j a v a 2s.co m } return result; }
From source file:Main.java
/** * @see //http://www.java.net/node/667186 *///w w w.j ava2 s. c o m 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 Element) { removeWhitespaceNodes((Element) child); } } }
From source file:Main.java
static void printElement(Element element, String indent) { System.out.println("Element '" + element.getNodeName() + "'"); NodeList children = element.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); switch (child.getNodeType()) { case Node.ELEMENT_NODE: printElement((Element) child, indent + "\t"); break; case Node.ATTRIBUTE_NODE: Attr attr = (Attr) child; System.out.println("\tAttribute: '" + attr.getName() + "' = '" + attr.getValue() + "'"); break; case Node.COMMENT_NODE: Comment comment = (Comment) child; System.out.println("\tComment: '" + comment.getData() + "'"); break; case Node.CDATA_SECTION_NODE: CharacterData cdata = (CharacterData) child; System.out.println("\tCDatat: '" + cdata.getData() + "'"); break; case Node.TEXT_NODE: Text text = (Text) child; System.out.println("\tText: '" + text.getData() + "'"); break; default:// w ww . j a va 2s . c om System.out.println("\tUnknown node type: '" + child.getNodeType() + "'"); break; } } }
From source file:Main.java
/** * Get the first instance of an element by name. * //from ww w .ja va2 s . 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
/** * Convert a {@link NodeList} to a {@link List} * * @param nodelist Node list// ww w . j av a 2s . c o m * @return {@link List} of nodes */ public static List<Node> toList(NodeList nodelist) { ArrayList<Node> list = new ArrayList<Node>(nodelist.getLength()); for (int i = 0; i < nodelist.getLength(); i++) { list.add(nodelist.item(i)); } return list; }
From source file:Main.java
public static String GetStringValueForNode(Node item) throws Exception { if (item instanceof Element) { StringBuilder builder = new StringBuilder(); NodeList children = item.getChildNodes(); for (int index = 0; index < children.getLength(); index++) builder.append(children.item(index).getNodeValue()); // return... return builder.toString(); } else/*from www. j av a 2 s . co m*/ throw new Exception(String.format("Cannot handle '%s'.", item.getClass())); }
From source file:Main.java
public static void setPrefixRecursive(final Node node, final String prefix) { if (node.getNodeType() == Node.ELEMENT_NODE) { node.setPrefix(prefix);/*from www .ja va 2s. com*/ } final NodeList list = node.getChildNodes(); for (int i = 0; i < list.getLength(); ++i) { setPrefixRecursive(list.item(i), prefix); } }
From source file:Main.java
private static String getText(Document doc, String tagName, int idx) { NodeList nodeList = doc.getElementsByTagName(tagName); if (nodeList.getLength() <= 0) return ""; return nodeList.item(idx).getTextContent(); }
From source file:Utils.java
public static Element findElementElseCreateAndSet(Document document, Element parent, String child, String value) {/*from www. ja v a 2s . c o m*/ Element ret = null; NodeList nl = parent.getElementsByTagName(child); if (nl.getLength() == 0) { parent.appendChild(document.createElement(child)); ret = (Element) parent.getElementsByTagName(child).item(0); ret.appendChild(document.createTextNode(value)); } return ret; }