List of usage examples for org.w3c.dom Node getChildNodes
public NodeList getChildNodes();
NodeList
that contains all children of this node. From source file:Main.java
/** Return a list of all the children of a given node *//* w w w . j a v a2 s .c om*/ static public Vector<Node> findAllChildren(Node node) { if (node == null) return null; Vector<Node> found_children = new Vector<Node>(); NodeList children = node.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (child.getNodeType() == Node.ELEMENT_NODE) found_children.add(child); } return found_children; }
From source file:Main.java
public static String getTextContent(Node node, boolean trim) { if (node == null) { return ""; }// www .jav a 2 s. c o m String textContent = ""; NodeList childNodes = node.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node childNode = childNodes.item(i); if (childNode.getNodeType() == Node.TEXT_NODE) { textContent += childNode.getNodeValue().trim(); } } textContent = textContent.replace("\r", ""); if (textContent.startsWith("\n")) { textContent = textContent.substring(1); } if (trim) { textContent = textContent.trim(); } return textContent; }
From source file:Main.java
/** * Get the index'th child node of parent that implements/subclasses the * given type./*w ww . j av a 2 s . com*/ * * @param index * Child node index * @param parent * Parent to search * @param nodeType * Type of child to search for * @return index'th child of parent that conforms to the given nodeType */ public static <N extends Node> N getNthChildImplementing(int index, Node parent, Class<N> nodeType) { if (parent == null) { return null; } NodeList children = parent.getChildNodes(); if (children == null) { return null; } int count = 0; for (int i = 0; i < children.getLength(); i++) { Node node = children.item(i); if (nodeType.isAssignableFrom(node.getClass())) { if (count++ == index) { @SuppressWarnings("unchecked") N n = (N) node; return n; } } } return null; }
From source file:Main.java
public static Element getElement(Document document, String[] path, String value, String translate, String module) {/*from ww w .j a v a 2 s . c om*/ Node node = document; NodeList list; for (int i = 0; i < path.length; ++i) { list = node.getChildNodes(); boolean found = false; for (int j = 0; j < list.getLength(); ++j) { Node testNode = list.item(j); if (testNode.getNodeName().equals(path[i])) { found = true; node = testNode; break; } } if (found == false) { Element element = document.createElement(path[i]); node.appendChild(element); node = element; } } if (value != null) { Text text = document.createTextNode(value); list = node.getChildNodes(); boolean found = false; for (int j = 0; j < list.getLength(); ++j) { Node testNode = list.item(j); if (testNode instanceof Text) { node.replaceChild(text, testNode); found = true; break; } } if (!found) node.appendChild(text); } if (node instanceof Element) { Element element = (Element) node; if (translate != null && element.getAttribute("translate").equals("")) { element.setAttribute("translate", translate); } if (module != null && element.getAttribute("module").equals("")) { element.setAttribute("module", module); } } return (Element) node; }
From source file:net.mumie.coursecreator.xml.GraphXMLizer.java
public static void recodeUmlauts(Node node) { NodeList nl = node.getChildNodes(); if (node instanceof Text) { throw new IllegalArgumentException("Whoops! using the non-Text variant for a Text node!"); } // end of if () // If 'nl' is empty, the body of the following for loop (i.e. the // recursion) will simply be skipped. in that case we return with // 'node' unchanged. for (int i = 0; i < nl.getLength(); i++) { Node curr = nl.item(i);//from w w w . j ava 2s. co m if (curr.getNodeType() == Node.TEXT_NODE) { recodeUmlauts((Text) curr); } // end of if (curr.getNodeType() == Node.TEXT_NODE) else { recodeUmlauts(curr); } // end of if (curr.getNodeType() == Node.TEXT_NODE) else } // end of for (int i = 0; i < nl.getLength() ; i++) }
From source file:Main.java
public static Node[] getChildNodes(final Node node, final String attributeName, final String value) { final ArrayList nodeList = new ArrayList(); final NodeList childs = node.getChildNodes(); String readValue = null;// www.j ava 2 s . c o m for (int i = 0; i < childs.getLength(); i++) { readValue = getAttribute(childs.item(i), attributeName); if (value.equals(readValue)) { nodeList.add(childs.item(i)); } } //next child node final Node[] result = new Node[nodeList.size()]; nodeList.toArray(result); return result; }
From source file:Main.java
/** * Searches parent node for matching child nodes, then collects and returns * the first match node's value. Useful when caller knows that there is only * one child node.//from w w w. jav a 2s .com * @param node The parent node * @return The child node's value */ public static String getNodeValue(Node node) { // sometimes jdom use more than one children nodes to represent text node NodeList children = node.getChildNodes(); String value = ""; for (int i = 0; i < children.getLength(); i++) { value += children.item(i).getNodeValue(); } return value; }
From source file:Main.java
private static void getByName(Node node, String name, List<Node> result) { if (node.getNodeName().equalsIgnoreCase(name)) { result.add(node);//from w w w. j av a 2 s . c o m } else { NodeList list = node.getChildNodes(); for (int i = 0, len = list.getLength(); i < len; i++) { getByName(list.item(i), name, result); } } }
From source file:Main.java
public static Object transformXmlNodesIntoMap(Node node) { Map<String, Object> nodeMap = new HashMap<String, Object>(); NodeList subNodes = node.getChildNodes(); NamedNodeMap nodeAttrs = node.getAttributes(); for (int nodeAttrIdx = 0; nodeAttrIdx < nodeAttrs.getLength(); nodeAttrIdx++) { Node attrNode = nodeAttrs.item(nodeAttrIdx); // nodeMap.put("@"+attrNode.getNodeName(), // attrNode.getTextContent()); nodeMap.put("@" + attrNode.getNodeName(), attrNode.getNodeValue()); }/*w ww. ja v a2 s . c om*/ if (nodeAttrs.getLength() == 0) if (subNodes.getLength() == 0) return ""; else if (subNodes.getLength() == 1 && subNodes.item(0).getNodeType() == Node.TEXT_NODE) // return subNodes.item(0).getTextContent(); return subNodes.item(0).getNodeValue(); for (int subNodeIdx = 0; subNodeIdx < subNodes.getLength(); subNodeIdx++) { Node subNode = subNodes.item(subNodeIdx); if (subNode.getNodeType() == Node.TEXT_NODE) { // nodeMap.put(subNode.getNodeName(), subNode.getTextContent()); nodeMap.put(subNode.getNodeName(), subNode.getNodeValue()); } else { if (nodeMap.containsKey(subNode.getNodeName())) { Object subObject = nodeMap.get(subNode.getNodeName()); if (subObject instanceof List<?>) { ((List<Object>) subObject).add(transformXmlNodesIntoMap(subNode)); } else { List<Object> subObjectList = new ArrayList<Object>(); subObjectList.add(subObject); subObjectList.add(transformXmlNodesIntoMap(subNode)); nodeMap.put(subNode.getNodeName(), subObjectList); } } else { nodeMap.put(subNode.getNodeName(), transformXmlNodesIntoMap(subNode)); } } } return nodeMap; }
From source file:Main.java
/** * //from w w w . ja v a 2 s . c om * @param currentNode * @param tagName * @param attributeValue * @return */ public static String getTextContentByElementNameANDAttributeValue(Node currentNode, String tagName, String attributeValue) { String result = ""; NodeList childNodeList = currentNode.getChildNodes(); for (int i = 0; i < childNodeList.getLength(); i++) { Node childNode = childNodeList.item(i); switch (childNode.getNodeType()) { case Node.DOCUMENT_NODE: break; case Node.ELEMENT_NODE: Element childElement = (Element) childNodeList.item(i); // logger.debug("childElement name : " + childElement.getTagName()); if (childElement != null && childElement.getNodeName().equals(tagName)) { NamedNodeMap attributes = childElement.getAttributes(); for (int j = 0; j < attributes.getLength(); j++) { Node current = attributes.item(j); if (current.getNodeName().equals("type") && current.getNodeValue().equals(attributeValue)) { result = childElement.getTextContent(); break; } } } case Node.TEXT_NODE: // logger.debug("textElement name : " + currentNode.getNodeValue()); break; case Node.COMMENT_NODE: break; case Node.PROCESSING_INSTRUCTION_NODE: break; case Node.ENTITY_REFERENCE_NODE: break; case Node.DOCUMENT_TYPE_NODE: break; } } return result; }