List of usage examples for org.w3c.dom Node getNodeType
public short getNodeType();
From source file:Main.java
public static Map<String, String> getChildElementNodesMap(Node node) { if (node == null) { return null; }/* w w w . ja v a 2s .com*/ Map<String, String> map = new ConcurrentHashMap<>(); 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 && item.getNodeType() == Node.ELEMENT_NODE) { map.put(item.getNodeName(), item.getTextContent().trim()); } } } return map; }
From source file:Main.java
/** * Checks to see if parent node has more of these nodes looks at tagName * // ww w . j a v a2 s . c o m * @param node * @return */ private static final boolean parentNodeHasMoreOfThese(Element node) { int count = 0; NodeList children = node.getParentNode().getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (child.getNodeType() == Node.ELEMENT_NODE) { if (node.getTagName().equals(((Element) child).getTagName())) { count++; if (count > 1) return true; } } } return false; }
From source file:Main.java
/** * This method returns the owner document of a particular node. * This method is necessary because it <I>always</I> returns a * {@link Document}. {@link Node#getOwnerDocument} returns <CODE>null</CODE> * if the {@link Node} is a {@link Document}. * * @param node// w w w . j av a2 s . c o m * @return the owner document of the node */ public static Document getOwnerDocument(Node node) { if (node.getNodeType() == Node.DOCUMENT_NODE) { return (Document) node; } try { return node.getOwnerDocument(); } catch (NullPointerException npe) { throw new NullPointerException(npe.getMessage()); } }
From source file:Main.java
/** * Extracts all ElementNodes from a NodeList and returns the result as a * list.//from w w w .j a v a 2s .c om * * @param nodeList * the NodeList to be searched for ElementNodes. * @return an array containing all ElementNodes stored in the given node * list or null if the input has been null. */ public static List<Element> elementNodes(NodeList nodeList) { if (nodeList == null) { return null; } List<Element> result = new ArrayList<Element>(); int len = nodeList.getLength(); for (int i = 0; i < len; ++i) { Node node = nodeList.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { result.add((Element) node); } } return result; }
From source file:Main.java
/** * Return all values associated with attributeName inside element specified by tag * @param document doc we work with/* www.j av a 2s . c om*/ * @param elementTag element containing desired attribute * @param attributeName name of attribute * @return List of values specified in XML array */ public static List<String> getListOfTagAttributeValues(Document document, String elementTag, String attributeName) { NodeList usesPermissionList = document.getElementsByTagName(elementTag); List<String> result = new ArrayList<String>(); for (int i = 0; i < usesPermissionList.getLength(); i++) { Node nNode = usesPermissionList.item(i); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; String value = eElement.getAttribute(attributeName); if (value != null && !value.isEmpty()) { result.add(value); } } } return result; }
From source file:Main.java
/** * Returns first DOM node of type element contained within given element. * // w w w .j ava 2s . co m * @param elem element */ public static Element getContainedElement(Element elem) { Node n = elem.getFirstChild(); while (n != null) { if (n.getNodeType() == Node.ELEMENT_NODE) { Element e = (Element) n; return e; } n = n.getNextSibling(); } return null; }
From source file:Main.java
/** * Gets the elements.//from ww w.j av a2 s . co m * * @param topElm * the top elm * @return the elements */ public static List<Element> getElements(Element topElm) { List<Element> retVals = new ArrayList<Element>(); NodeList childNodes = topElm.getChildNodes(); for (int i = 0, ll = childNodes.getLength(); i < ll; i++) { Node n = childNodes.item(i); if (n.getNodeType() == Node.ELEMENT_NODE) { retVals.add((Element) n); } } return retVals; }
From source file:Main.java
/** * Trim all new lines from text nodes.//w w w. j a v a2 s. c o m * * @param node */ public static void normalize(Node node) { if (node.getNodeType() == Node.TEXT_NODE) { String data = ((Text) node).getData(); if (data.length() > 0) { char ch = data.charAt(data.length() - 1); if (ch == '\n' || ch == '\r' || ch == ' ') { String data2 = trim(data); ((Text) node).setData(data2); } } } for (Node currentChild = node.getFirstChild(); currentChild != null; currentChild = currentChild .getNextSibling()) { normalize(currentChild); } }
From source file:Main.java
/** * Returns the first child element that matches the given local name and * namespace. If no child element is present or no child element matches, * <code>null</code> is returned. * * @param parent/*from w w w. ja v a 2 s .com*/ * @param childLocalName * @param childNamespaceURI * @return first child element matching the specified names or <code>null</code>. */ public static Element getChildElement(Node parent, String childLocalName, String childNamespaceURI) { if (parent != null) { NodeList children = parent.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (child.getNodeType() == Node.ELEMENT_NODE && childLocalName.equals(child.getLocalName()) && childNamespaceURI.equals(child.getNamespaceURI())) { return (Element) child; } } } return null; }
From source file:Main.java
public static Element[] getChildElementNodes(Node node) { if (node == null) { return null; }/* w w w .j av a 2 s. co 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 && item.getNodeType() == Node.ELEMENT_NODE) { elements.add((Element) item); } } } return elements.toArray(new Element[elements.size()]); }