List of usage examples for org.w3c.dom Node getNodeName
public String getNodeName();
From source file:Main.java
/** * Returns a single element having a given tag *///from w ww . jav a 2 s . com public static Element getSingleElement(Element element, String tagName) { Node node = element.getFirstChild(); while (node != null) { if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().compareTo(tagName) == 0) return (Element) node; node = node.getNextSibling(); } return null; }
From source file:Main.java
public static Node getPrevious(final Node current, final boolean sameName) { String name = null;//from ww w . j av a 2 s .c o m if (sameName) { name = current.getNodeName(); } int type = current.getNodeType(); return getPrevious(current, name, type); }
From source file:Main.java
/** * find elements by name.//from w ww. jav a 2s . c o m * * @param node - current node * @param name - name element * @param elements - list of found elements */ static public void findElementsByName(Node node, List<Node> elements, String name) { // get children NodeList children = node.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); // if current child is required then add his to list if (name.equalsIgnoreCase((child.getNodeName()))) { elements.add(child); } else { findElementsByName(child, elements, name); } } }
From source file:Main.java
/** * Check if a node in the parent hierarchy has the name given in parameter * /* w w w .j a v a 2 s . com*/ * @param node * @param string * @return true if the node exists */ public static boolean containsInHierarchy(Node node, String string) { Node parent = node; if (node != null) { do { parent = parent.getParentNode(); } while (parent != null && !parent.getNodeName().equals(string)); } return parent != null; }
From source file:Main.java
/** * Copy elements from one document to another attaching at the specified * element and translating the namespace. * * @param from copy the children of this element (exclusive) * @param to where to attach the copied elements * @param newNamespace destination namespace * * @since 8.4//w w w .j a v a 2s .c o m */ public static void copyDocument(Element from, Element to, String newNamespace) { Document doc = to.getOwnerDocument(); NodeList nl = from.getChildNodes(); int length = nl.getLength(); for (int i = 0; i < length; i++) { Node node = nl.item(i); Node newNode = null; if (Node.ELEMENT_NODE == node.getNodeType()) { Element oldElement = (Element) node; newNode = doc.createElementNS(newNamespace, oldElement.getTagName()); NamedNodeMap m = oldElement.getAttributes(); Element newElement = (Element) newNode; for (int index = 0; index < m.getLength(); index++) { Node attr = m.item(index); newElement.setAttribute(attr.getNodeName(), attr.getNodeValue()); } copyDocument(oldElement, newElement, newNamespace); } else { newNode = node.cloneNode(true); newNode = to.getOwnerDocument().importNode(newNode, true); } if (newNode != null) { to.appendChild(newNode); } } }
From source file:Main.java
/** * Reverse Engineers an XPath Expression of a given Node in the DOM. * /*from w w w . j a v a 2s .c om*/ * @param node * the given node. * @return string xpath expression (e.g., "/html[1]/body[1]/div[3]"). */ public static String getXPathExpression(Node node) { Object xpathCache = node.getUserData(FULL_XPATH_CACHE); if (xpathCache != null) { return xpathCache.toString(); } Node parent = node.getParentNode(); if ((parent == null) || parent.getNodeName().contains("#document")) { String xPath = "/" + node.getNodeName() + "[1]"; node.setUserData(FULL_XPATH_CACHE, xPath, null); return xPath; } StringBuffer buffer = new StringBuffer(); if (parent != node) { buffer.append(getXPathExpression(parent)); buffer.append("/"); } buffer.append(node.getNodeName()); List<Node> mySiblings = getSiblings(parent, node); for (int i = 0; i < mySiblings.size(); i++) { Node el = mySiblings.get(i); if (el.equals(node)) { buffer.append('[').append(Integer.toString(i + 1)).append(']'); // Found so break; break; } } String xPath = buffer.toString(); node.setUserData(FULL_XPATH_CACHE, xPath, null); return xPath; }
From source file:Main.java
private static void findRecursive(Node parent, String name, List<Node> nodes, boolean onlyOne) { String nn = parent.getNodeName(); int off = nn.indexOf(':'); if (off >= 0) nn = nn.substring(off + 1);/*from ww w. j a va 2 s . c o m*/ if (nn.equals(name)) { nodes.add(parent); if (onlyOne) return; } for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) { findRecursive(child, name, nodes, onlyOne); if (onlyOne && (nodes.size() > 0)) return; } }
From source file:Main.java
/** Helper method - finds the node with the specified element name. The returned node can be the passed in node itself or one of its child nodes. The first node found is the one returned./*from w w w . j ava 2s. c o m*/ @param pNode A <I>org.w3c.dom.Node</I> object being searched. @param strElementName Name of the element being searched for. @return <I>org.w3c.dom.Node</I> object. */ public static Node findNode(Node pNode, String strElementName) { // Check the parent, first. if (pNode.getNodeName().equals(strElementName)) return pNode; NodeList pNodeList = null; if (pNode instanceof Element) pNodeList = ((Element) pNode).getElementsByTagName(strElementName); else if (pNode instanceof Document) pNodeList = ((Document) pNode).getElementsByTagName(strElementName); if ((null == pNodeList) || (0 == pNodeList.getLength())) return null; return pNodeList.item(0); }
From source file:Main.java
/** * This method returns a list of the direct element node children of this element node with the specified tag. * @param node - parent node//from w w w .j a v a 2 s . c om * @param tag - tag of direct children to be returned * @return a list containing the direct element children with the given tag * @author Tristan Bepler */ public static List<Element> getDirectChildElementsByTag(Element node, String tag) { List<Element> children = new ArrayList<Element>(); Node child = node.getFirstChild(); while (child != null) { if (child.getNodeType() == Node.ELEMENT_NODE && (child.getNodeName().equals(tag) || tag.equals(DOM_WILDCARD))) { children.add((Element) child); } child = child.getNextSibling(); } return children; }
From source file:Main.java
public static void updateUserMgtXML(File userMgtXML, String jndiConfigNameUserDB) throws Exception { Document doc = initializeXML(userMgtXML); NodeList configNodeList = doc.getElementsByTagName("Configuration"); Node configNode = configNodeList.item(0); //get the 0 index, since only one available NodeList nodeList = configNode.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { if (node.getNodeName().equalsIgnoreCase("Property")) { if (node.hasAttributes()) { NamedNodeMap namedNodeMap = node.getAttributes(); Node attr = namedNodeMap.getNamedItem("name"); if (attr != null && attr.getNodeValue().equalsIgnoreCase("dataSource")) { node.setTextContent(jndiConfigNameUserDB); }/*ww w . j av a2s .c o m*/ } } } } finalizeXML(userMgtXML, doc, 4); }