List of usage examples for org.w3c.dom Node ELEMENT_NODE
short ELEMENT_NODE
To view the source code for org.w3c.dom Node ELEMENT_NODE.
Click Source Link
Element
. From source file:XmlHelper.java
/** * Returns an iterator over the children of the given element with the given * tag name./*from w w w .j a v a 2s . c om*/ * * @param element * The parent element * @param tagName * The name of the desired child * @return An interator of children or null if element is null. */ public static Iterator getChildrenByTagName(Element element, String tagName) { if (element == null) return null; // getElementsByTagName gives the corresponding elements in the whole // descendance. We want only children NodeList children = element.getChildNodes(); ArrayList goodChildren = new ArrayList(); for (int i = 0; i < children.getLength(); i++) { Node currentChild = children.item(i); if (currentChild.getNodeType() == Node.ELEMENT_NODE && ((Element) currentChild).getTagName().equals(tagName)) { goodChildren.add(currentChild); } } return goodChildren.iterator(); }
From source file:Main.java
/** * Removes all the children of a given Node * * @param node the Node whose children is to be removed * @return Node after the children removed *//*from ww w . j a v a 2 s . c o m*/ public static Node removeChildren(Node node) { /* System.out.println(" Node whose children are to be removed:" + node.getNodeName() + ":Type:" + node.getNodeType() + ":value:" + node.getNodeValue()); */ // Currently remove children of only Element Nodes if (node.getNodeType() == Node.ELEMENT_NODE) { NodeList children = node.getChildNodes(); //System.out.println("***** XMLUTIL....removeChildren....# of children are " + children.getLength()); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); /* System.out.println("Child Name:" + child.getNodeName() + ":Type:" + child.getNodeType() + " value:" + child.getNodeValue() + ":i= " + i); */ Node temp = node.removeChild(child); /* System.out.println("Removed Name:" + temp.getNodeName() + ":Type:" + temp.getNodeType() + " value:" + temp.getNodeValue() + ":i= " + i); */ i--; } } return node; }
From source file:Main.java
/** * List all child nodes with name sName/*from w ww . j av a 2 s.c o m*/ * NOTE: we assume no same name nodes are nested. * @param node * @param sName * @return Element */ public static ArrayList<Element> listChildElementsByName(Node node, String sName) { ArrayList<Element> aNodes = new ArrayList<Element>(); NodeList nl = node.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node n = nl.item(i); if (n.getNodeType() == Node.ELEMENT_NODE) { if (sName.equals(n.getNodeName())) { aNodes.add((Element) n); } else { ArrayList<Element> nextNodes = listChildElementsByName(n, sName); if (nextNodes != null) aNodes.addAll(nextNodes); } } // Don't search anything but elements } return aNodes; }
From source file:Main.java
/** * Search for the first <code>Node</code> with the nodeName().equals(nodeName) in the nodeList * /* ww w. j a v a2 s. c om*/ * @param nodeList * @param nodeName * @return Node or null if no Node has been found */ private static Node findNode(NodeList nodeList, String nodeName) { Node curNode; for (int i = 0; i < nodeList.getLength(); i++) { curNode = nodeList.item(i); if (curNode.getNodeType() == Node.ELEMENT_NODE && nodeName.equals(curNode.getNodeName())) { return curNode; } } return null; }
From source file:Main.java
/** * Finds and returns the next sibling node with the given name and * attribute name, value pair. Since only elements have attributes, * the node returned will be of type Node.ELEMENT_NODE. *///from w ww . j ava 2 s . c om public static Element getNextSiblingElement(Node node, String elemName, String attrName, String attrValue) { // search for node Node sibling = node.getNextSibling(); while (sibling != null) { if (sibling.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) sibling; if (element.getNodeName().equals(elemName) && element.getAttribute(attrName).equals(attrValue)) { return element; } } sibling = sibling.getNextSibling(); } // not found return null; }
From source file:Main.java
/** * Utility function for getting the first child element from a element * * @param element the parent element/*from www .j av a2 s . com*/ * @return the first child element if one was found, null otherwise */ public static Element getFirstChildElement(Element element) { if (element == null) { throw new IllegalArgumentException("nullArgument: element"); // i18n.getMessage("nullArgument", "element")); } for (Node currentChild = element.getFirstChild(); currentChild != null; currentChild = currentChild .getNextSibling()) { if (currentChild.getNodeType() == Node.ELEMENT_NODE) { return (Element) currentChild; } } return null; }
From source file:Main.java
/** * Returns the MIME type of the XSLT transformation output taken from the "media-type" attribute of the xsl:output element. * If the "media-type" attribute is not used it takes the "method" attribute of the same element. * The following mappings between the "method" attribute values and MIME types are used * text -> text/plain// w w w . j a v a 2 s .c om * xml -> application/xml * html -> text/html * @param xslName * @return */ public static String getOutputMimeType(String xslName) { String mimeType = ""; String mediaType = ""; String method = ""; getDocument(xslName); NodeList nList = xsl.getElementsByTagName("xsl:output"); for (int i = 0; i < nList.getLength(); i++) { Node outputNode = nList.item(i); if (outputNode.getNodeType() == Node.ELEMENT_NODE) { Element outputElement = (Element) outputNode; mediaType = outputElement.getAttribute("media-type"); method = outputElement.getAttribute("method"); } } if ("".equals(mediaType)) { mimeType = ("text".equals(method)) ? "text/plain" : ("xml".equals(method)) ? "application/xml" : ("html".equals(method)) ? "text/html" : ""; } else mimeType = mediaType; return mimeType; }
From source file:Main.java
/** * Gets the ancestor element node to the given node. * //from w w w. j a v a 2 s.c om * @param currentNode the node to retrive the ancestor for * * @return the ancestral element node of the current node, or null */ public static Element getElementAncestor(Node currentNode) { Node parent = currentNode.getParentNode(); if (parent != null) { short type = parent.getNodeType(); if (type == Node.ELEMENT_NODE) { return (Element) parent; } return getElementAncestor(parent); } return null; }
From source file:Main.java
/** * Get the next sibling Element of the supplied node. * * @param node The DOM Node.// w ww. j av a 2 s .c o m * @return The next sibling element */ public static Element getNextSiblingElement(Node node) { Node sibling = node.getNextSibling(); while (sibling != null && sibling.getNodeType() != Node.ELEMENT_NODE) { sibling = sibling.getNextSibling(); } return (Element) sibling; }
From source file:Main.java
/** * Get the first node with a given name and returns it * * @param root the node that contains the children to search within * @param name the name of the node/*w w w.j av a 2 s . co m*/ * @return a node with given or null if not found */ public static Node find(Node root, String name) { final NodeList list = root.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { final Node subnode = list.item(i); if (subnode.getNodeType() == Node.ELEMENT_NODE) { if (subnode.getNodeName().equals(name)) { return subnode; } } } return null; }