List of usage examples for org.w3c.dom NodeList getLength
public int getLength();
From source file:Main.java
/** Returns the entire value of a node (child nodes and text) as a String * @param node//from ww w.ja v a2 s. co m * @return */ public static String nodeChildrenToString(Node node) { StringWriter sw = new StringWriter(); try { Transformer t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); NodeList nl = node.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) t.transform(new DOMSource(nl.item(i)), new StreamResult(sw)); } catch (TransformerException te) { System.out.println("nodeToString Transformer Exception"); } return sw.toString().trim(); }
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()); }//w w w.j a v a 2 s . co m return sb.toString(); }
From source file:Main.java
/** * Clone all the nodes contains in the provided node list, and return them in a {@link List}. * * @param nodelist Node list//from ww w. j av a 2 s . c o m * @param deep If true, recursively clone the subtree under each node; if false, clone only the node itself (and its attributes, if it is an Element). * @return {@link List} of cloned nodes */ public static List<Node> toClonedList(NodeList nodelist, boolean deep) { ArrayList<Node> list = new ArrayList<Node>(nodelist.getLength()); for (int i = 0; i < nodelist.getLength(); i++) { list.add(nodelist.item(i).cloneNode(deep)); } return list; }
From source file:Main.java
public static String getTextValue(Element valueEle) { StringBuffer value = new StringBuffer(); NodeList nl = valueEle.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node item = nl.item(i);/*from ww w .ja va 2 s .c om*/ if ((item instanceof CharacterData && !(item instanceof Comment)) || item instanceof EntityReference) { value.append(item.getNodeValue()); } } return value.toString().trim(); }
From source file:Main.java
public static List<Element> getElements(Element parent, String tagName) { List<Element> elements = new ArrayList<Element>(); NodeList nodes = parent.getElementsByTagName(tagName); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); if (node instanceof Element) { elements.add(Element.class.cast(node)); }/* w w w.ja va2 s .c o m*/ } return elements; }
From source file:Main.java
static public String getNodeValue(Node node) { NodeList childNodes = node.getChildNodes(); for (int x = 0; x < childNodes.getLength(); x++) { Node data = childNodes.item(x); if (data.getNodeType() == Node.TEXT_NODE) { return data.getNodeValue(); }/* w w w . j a v a 2 s .co m*/ } return ""; }
From source file:Main.java
private static String getNodeName(Node node) { String name = ""; NodeList nodeList = node.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node curNode = nodeList.item(i); if ("name".equalsIgnoreCase(curNode.getNodeName())) { name = curNode.getTextContent().replace("\"", ""); break; }//from www.java2 s.c o m } return name; }
From source file:Main.java
/** * Determines the first child node of the parent with the specified tag * name./*from ww w . j a v a 2 s.co m*/ * * @param parent The parent node of the child node to be determined. * @param nodeName The name of the child node to be determined. * * @return The determined child node with the given name. */ public static Node getChildWithName(Node parent, String nodeName) { if (parent != null) { NodeList childs = parent.getChildNodes(); for (int i = 0; i < childs.getLength(); i++) { Node child = childs.item(i); if ((child.getLocalName() != null) && child.getLocalName().equals(nodeName)) { return childs.item(i); } } } 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 av a 2 s. co m } return null; }
From source file:Main.java
/** * @param node//from w ww . j a v a2 s . c o m * @return */ public static Element getLastElement(Node node) { NodeList children = node.getChildNodes(); if (children == null || children.getLength() == 0) { return null; } int len = children.getLength(); for (int i = len - 1; i >= 0; i--) { Node n = children.item(i); if (n instanceof Element) { return (Element) n; } } return null; }