List of usage examples for org.w3c.dom Element getChildNodes
public NodeList getChildNodes();
NodeList
that contains all children of this node. From source file:Main.java
/** * Gets all facets of sourceElement. If facet has a few children the method * will return first one.//ww w . jav a 2s. c o m * * @param sourceElement the source element * @param facetName * * param returnTextNode return child text node if facet doesn't have * any child elements; * @param returnTextNode the return text node * * @return the facets */ public static ArrayList<Node> getFacets(Element sourceElement, boolean returnTextNode) { ArrayList<Node> facets = new ArrayList<Node>(); NodeList children = sourceElement.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node node = children.item(i); if (node instanceof Element && "f:facet".equals(node.getNodeName())) { //$NON-NLS-1$ Element element = (Element) node; NodeList childNodes = element.getChildNodes(); Text textNode = null; for (int j = 0; j < childNodes.getLength(); j++) { Node child = childNodes.item(j); if (child instanceof Element) { facets.add(child); break; } else if (child instanceof Text) { textNode = (Text) child; } } if (returnTextNode && facets.isEmpty()) { facets.add(textNode); } } } return facets; }
From source file:Main.java
/** * Looks through all child elements of the specified root (recursively) * and returns the first element that corresponds to all parameters. * * @param root the Element where the search should begin * @param tagName the name of the node we're looking for * @param keyAttributeName the name of an attribute that the node has to * have// w w w.j a v a 2 s . c o m * @param keyAttributeValue the value that attribute must have * @return the Element in the tree under root that matches the specified * paameters. * @throws NullPointerException if any of the arguments is null. */ public static Element locateElement(Element root, String tagName, String keyAttributeName, String keyAttributeValue) { NodeList nodes = root.getChildNodes(); Node node; int len = nodes.getLength(); for (int i = 0; i < len; i++) { node = nodes.item(i); if (node.getNodeType() != Node.ELEMENT_NODE) continue; // is this the node we're looking for? if (node.getNodeName().equals(tagName)) { String attr = ((Element) node).getAttribute(keyAttributeName); if (attr != null && attr.equals(keyAttributeValue)) return (Element) node; } //look inside. Element child = locateElement((Element) node, tagName, keyAttributeName, keyAttributeValue); if (child != null) return child; } return null; }
From source file:Main.java
/** * returns a XML element value.//from w w w . j a v a2 s. c o m * * @param pElement Document XML element * @return String XML node value */ public static String getValue(Element pElement) throws Exception { String s = null; try { NodeList nodes = pElement.getChildNodes(); //find a value whose value is non-whitespace for (int i = 0; i < nodes.getLength(); i++) { s = ((Node) nodes.item(i)).getNodeValue().trim(); if (s.equals("") || s.equals("\r")) continue; } } catch (Exception ex) { throw new Exception(ex.getMessage()); } return s; }
From source file:Main.java
/** * Get first child element with the provided node name and attribute that * are direct child the provided element. * /*from w w w . j av a 2 s .c om*/ * @param parent * @param name * @param attributeName * @param attributeValue * @return element if found, otherwise null */ public static Element getChildElementByNameAndAttribute(Element parent, String name, String attributeName, String attributeValue) { assertNotNull(parent); NodeList children = parent.getChildNodes(); Node node; for (int i = 0; i < children.getLength(); i++) { node = children.item(i); if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(name)) { Element element = (Element) node; if (element.getAttribute(attributeName).equals(attributeValue)) { return element; } } } return null; }
From source file:Main.java
/** * Concatenates the values of all child nodes of type 'Text' or 'CDATA'/ * * @param element/* www . j a va2s . co m*/ * @return String representing the value of all Text and CDATA child nodes or * <code>null</code> if the length of the resulting String is 0. * @see #isText(org.w3c.dom.Node) */ public static String getText(Element element) { StringBuilder content = new StringBuilder(); if (element != null) { NodeList nodes = element.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { Node child = nodes.item(i); if (isText(child)) { // cast to super class that contains Text and CData content.append(((CharacterData) child).getData()); } } } return (content.length() == 0) ? null : content.toString(); }
From source file:Main.java
public static String GetChildElementText(Element element, String name, String defaultValue) { if (element != null && name != null && !name.isEmpty()) { NodeList nodes = element.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); if (node instanceof Element && name.equals(node.getNodeName())) { return node.getTextContent(); }/*w ww. j av a 2 s. c o m*/ } } return defaultValue; }
From source file:Main.java
/** * Looks through all child elements of the specified root (recursively) * and returns the first element that corresponds to all parameters. * * @param root the Element where the search should begin * @param tagName the name of the node we're looking for * @param keyAttributeName the name of an attribute that the node has to * have/*from www.ja v a 2 s. c om*/ * @param keyAttributeValue the value that attribute must have * @return the Element in the tree under root that matches the specified * parameters. * @throws NullPointerException if any of the arguments is null. */ public static Element locateElement(Element root, String tagName, String keyAttributeName, String keyAttributeValue) { NodeList nodes = root.getChildNodes(); int len = nodes.getLength(); for (int i = 0; i < len; i++) { Node node = nodes.item(i); if (node.getNodeType() != Node.ELEMENT_NODE) continue; Element element = (Element) node; // is this the node we're looking for? if (node.getNodeName().equals(tagName)) { String attr = element.getAttribute(keyAttributeName); if ((attr != null) && attr.equals(keyAttributeValue)) return element; } //look inside. Element child = locateElement(element, tagName, keyAttributeName, keyAttributeValue); if (child != null) return child; } return null; }
From source file:Main.java
public static Hashtable<String, Element> getChildHash(Element elem, String elementName, String attrName) { if (elem == null) return null; NodeList nl = elem.getChildNodes(); if (nl == null) return null; Hashtable<String, Element> retlist = new Hashtable<String, Element>(100); for (int n = 0; n < nl.getLength(); n++) { Node child = nl.item(n);// w w w. j a v a 2s . c o m if (child instanceof Element) { Element element = (Element) child; if (!elementName.equals(element.getTagName())) continue; String keyValue = element.getAttribute(attrName); if (keyValue == null) continue; retlist.put(keyValue, element); } } if (retlist.size() == 0) return null; return retlist; }
From source file:Main.java
public static List<Element> elementsQName(final Element element, final Set<QName> allowedTagNames) { List<Element> elements = null; final NodeList nodeList = element.getChildNodes(); if (nodeList != null) { for (int i = 0; i < nodeList.getLength(); i++) { final Node child = nodeList.item(i); if (Element.class.isAssignableFrom(child.getClass())) { final Element childElement = (Element) child; final QName childElementQName = new QName(childElement.getNamespaceURI(), childElement.getLocalName()); if (allowedTagNames.contains(childElementQName)) { if (elements == null) { elements = new ArrayList<Element>(); }/*from w ww. jav a 2 s . com*/ elements.add(childElement); } } } } return elements; }
From source file:Main.java
/** * Get the element text node. Locate the text node and return it. For example; * <element>this is the text node</element> * * @param element The element to get the text node for * @return The text node/*from w w w . jav a 2 s. c o m*/ */ public static Node getElementTextNode(Element element) { Node textNode = null; // go through each child element Node node; short nodeType; NodeList children = element.getChildNodes(); for (int ii = 0; ii < children.getLength(); ii++) { node = children.item(ii); nodeType = node.getNodeType(); if (nodeType == Node.TEXT_NODE) { textNode = node; break; } else if (nodeType == Node.CDATA_SECTION_NODE) { textNode = node; break; } } return (textNode); }