List of usage examples for org.w3c.dom Node getChildNodes
public NodeList getChildNodes();
NodeList
that contains all children of this node. From source file:XmlUtil.java
/** * Returns value of first available child text node or <code>null</code> if not found. */// w ww. ja v a2 s . c o m public static String getFirstChildTextNodeValue(Node node) { NodeList children = node.getChildNodes(); int len = children.getLength(); for (int i = 0; i < len; i++) { Node n = children.item(i); if (n.getNodeType() == Node.TEXT_NODE) { return n.getNodeValue(); } } return null; }
From source file:Main.java
public static Collection<Node> search_nodes_by_name(Node root, String name) { Collection<Node> result = new LinkedList<Node>(); if (root.getNodeName().equals(name)) result.add(root);//from w ww .j a v a 2s . com NodeList list = root.getChildNodes(); for (int i = 0; i < list.getLength(); ++i) { Node child = list.item(i); Collection<Node> ret = search_nodes_by_name(child, name); result.addAll(ret); } return result; }
From source file:Main.java
/** * Method to get the value of "Value" node * If <class>unescape<class> is set to false, xml escaped chars will not * be unescaped.//from ww w .j a v a2s. co m */ public static String getValueOfValueNodeNoTrim(Node n, boolean unescape) { NodeList textNodes = n.getChildNodes(); Node textNode; StringBuffer value = new StringBuffer(""); for (int j = 0; j < textNodes.getLength(); j++) { textNode = textNodes.item(j); String text = null; if (textNode.getNodeType() == Node.TEXT_NODE) { text = textNode.getNodeValue(); } else if (textNode.getNodeType() == Node.ELEMENT_NODE) { text = print(textNode); } if (text != null && unescape) { value.append(unescapeSpecialCharacters(text)); } else { value.append(text); } } return value.toString(); }
From source file:Main.java
/** * The method extractChildNodes extracts child nodes of a specified type. * * @param aParentNode// w ww . j ava2s . c o m * a parent node * @param aRequiredType * a required node type * * @return a list of child nodes */ static List<Node> extractChildNodes(Node aParentNode, short aRequiredType) { List<Node> elementNodes = new ArrayList<Node>(); NodeList childNodeList = aParentNode.getChildNodes(); for (int a = 0; a < childNodeList.getLength(); a++) { Node node = childNodeList.item(a); short nodeType = node.getNodeType(); if (nodeType == aRequiredType) { elementNodes.add(node); } } return elementNodes; }
From source file:Main.java
/** * Gets the text for a node by concatenating child TEXT elements. *///from w ww. ja va 2s . co m public synchronized static String getText(Node n) { if (n == null) throw new IllegalArgumentException("Node argument cannot be null"); StringBuilder b = new StringBuilder(); NodeList nl = n.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { if (nl.item(i).getNodeType() == Node.TEXT_NODE) b.append(nl.item(i).getNodeValue()); else if (nl.item(i).getNodeType() == Node.CDATA_SECTION_NODE) b.append(nl.item(i).getNodeValue()); } return b.toString().trim(); }
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()); }// w w w .ja va2 s . c o m 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(); 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()); } 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
private static void findNodesNamed(Node node, String lookForName, Collection<Node> ret) { if (node.getNodeName().equals(lookForName)) { ret.add(node);// w ww . j av a 2 s .c om } else { NodeList list = node.getChildNodes(); for (int i = 0; i < list.getLength(); ++i) { findNodesNamed(list.item(i), lookForName, ret); } } }
From source file:Main.java
/** * Creates a map of tag name to dirty header XML from the SOAP header node. * * @param headerNode the header node to extract all XML from * @return a map of tag name to dirty header XML *//* w ww.ja va 2 s. co m*/ private static Map<String, String> createTagToDirtyXmlMap(Node headerNode) { Map<String, String> dirtyXmlMap = new HashMap<String, String>(); for (int i = 0; i < headerNode.getChildNodes().getLength(); i++) { Node headerChildNode = headerNode.getChildNodes().item(i); if (Arrays.asList(SENSITIVE_HEADER_TAGS).contains(headerChildNode.getLocalName())) { dirtyXmlMap.put(headerChildNode.getLocalName(), headerChildNode.toString()); } } return dirtyXmlMap; }
From source file:Main.java
/** * The method inserts end-of-line+indentation Text nodes where indentation is necessary. * * @param node - node to be pretty formatted * @param identLevel - initial indentation level of the node * @param ident - additional indentation inside the node *//*ww w . jav a 2 s .com*/ private static void prettyFormat(Node node, String identLevel, String ident) { NodeList nodelist = node.getChildNodes(); int iStart = 0; Node item = nodelist.item(0); if (item != null) { short type = item.getNodeType(); if (type == Node.ELEMENT_NODE || type == Node.COMMENT_NODE) { Node newChild = node.getOwnerDocument().createTextNode(EOL_XML + identLevel + ident); node.insertBefore(newChild, item); iStart = 1; } } for (int i = iStart; i < nodelist.getLength(); i++) { item = nodelist.item(i); if (item != null) { short type = item.getNodeType(); if (type == Node.TEXT_NODE && item.getNodeValue().trim().length() == 0) { if (i + 1 < nodelist.getLength()) { item.setNodeValue(EOL_XML + identLevel + ident); } else { item.setNodeValue(EOL_XML + identLevel); } } else if (type == Node.ELEMENT_NODE) { prettyFormat(item, identLevel + ident, ident); if (i + 1 < nodelist.getLength()) { Node nextItem = nodelist.item(i + 1); if (nextItem != null) { short nextType = nextItem.getNodeType(); if (nextType == Node.ELEMENT_NODE || nextType == Node.COMMENT_NODE) { Node newChild = node.getOwnerDocument() .createTextNode(EOL_XML + identLevel + ident); node.insertBefore(newChild, nextItem); i++; continue; } } } else { Node newChild = node.getOwnerDocument().createTextNode(EOL_XML + identLevel); node.appendChild(newChild); i++; continue; } } } } }
From source file:Main.java
public static Element[] getChildElementNodesByName(Node node, String name) { if (node == null) { return null; }// w w w .ja va 2s. c o m ArrayList<Element> elements = new ArrayList<>(); NodeList nodeList = node.getChildNodes(); if (nodeList != null && nodeList.getLength() > 0) { for (int i = 0; i < nodeList.getLength(); i++) { Node item = nodeList.item(i); if (item != null && (node.getNodeType() == Node.ELEMENT_NODE) && name.equalsIgnoreCase(item.getNodeName())) { elements.add((Element) item); } } } return elements.toArray(new Element[elements.size()]); }