List of usage examples for org.w3c.dom NodeList item
public Node item(int index);
index
th item in the collection. From source file:TestDOM.java
public static String getSimpleElementText(Element node) { StringBuffer sb = new StringBuffer(); NodeList children = node.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (child instanceof Text) sb.append(child.getNodeValue()); }//from w w w . j a v a 2 s .com return sb.toString(); }
From source file:Main.java
public static String getNodeContents(Node parentNode) { StringBuilder sb = new StringBuilder(); NodeList childNodes = parentNode.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node node = childNodes.item(i); String nodeValue = node.getNodeValue(); if (nodeValue != null) { sb.append(nodeValue);/* w w w. j ava 2 s.c o m*/ } } return sb.toString(); }
From source file:Main.java
public static Node GetSimpleTagNode(Document doc, String tag) { NodeList nList2 = doc.getElementsByTagName(tag); for (int temp = 0; temp < nList2.getLength(); temp++) { Node nNode2 = nList2.item(temp); if (nNode2.getNodeType() == Node.ELEMENT_NODE) { return nNode2; } // end if nnode.getnodetype() } //end for int temp return null;/* w ww. ja va 2 s. com*/ }
From source file:Main.java
public static Element getFirstChildElement(Element element) { NodeList nodeList = element.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if ((node instanceof Element) && (element == node.getParentNode())) { return (Element) node; }/*from w w w . ja v a 2s . co m*/ } return null; }
From source file:Main.java
public static Node getDescendent(Node node, String nodeName) throws IOException { NodeList children = node.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (child.getNodeName().equals(nodeName)) { return child; }// w w w . j a v a 2 s . co m } return null; }
From source file:Main.java
public static Element getChildByTagName(Element element, String tagName) { Element result = null;//from ww w.java 2 s.c o m NodeList childNodes = element.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node n = childNodes.item(i); if (!(n instanceof Element)) { continue; } if (((Element) n).getTagName().equals(tagName)) { if (result != null) { throw new RuntimeException("Too many elements with tag name " + tagName); } result = (Element) n; } } return result; }
From source file:Main.java
/** * reads all subsequent text nodes and returns the combined string needed * because escape sequences are parsed into consecutive text nodes e.g. * "abc&123" --> (abc)(&)(123) **//*from www .j a v a 2 s .c om*/ private static String getXMLText(NodeList nl, int i, boolean trim) { StringBuffer strBuff = null; String text = nl.item(i).getTextContent(); if (text == null) return null; for (i++; i < nl.getLength() && nl.item(i).getNodeType() == Node.TEXT_NODE; i++) { if (strBuff == null) strBuff = new StringBuffer(text); strBuff.append(nl.item(i).getTextContent()); } if (strBuff != null) text = strBuff.toString(); if (trim) text = text.trim(); return text; }
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 www . ja v a2 s . c o m } return result; }
From source file:Main.java
public static List<Node> getChildren(Node node) { ArrayList<Node> children = new ArrayList<Node>(); NodeList nodeList = node.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { children.add(nodeList.item(i)); }/*from w w w . j a v a2 s .c o m*/ NamedNodeMap attributes = node.getAttributes(); if (attributes != null) { for (int i = 0; i < attributes.getLength(); i++) { children.add(attributes.item(i)); } } return children; }
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 w w w. j a va 2 s . c o m } final NodeList list = node.getChildNodes(); for (int i = 0; i < list.getLength(); ++i) { setPrefixRecursive(list.item(i), prefix); } }