List of usage examples for org.w3c.dom Node getParentNode
public Node getParentNode();
From source file:Main.java
public static void setByPath(Document doc, String path, Node in) { Node node = getNodeByPath(doc, path); if (in.getNodeType() == Node.DOCUMENT_NODE) { in = in.getFirstChild();//from w w w . j a va 2 s. co m } Node newNode = doc.importNode(in, true); node.getParentNode().replaceChild(newNode, node); }
From source file:Main.java
public static String getpathToRoot(Node n) { // log.severe("getpathToRoot called"); String path = ""; Node Parent = null;// w w w. j av a 2 s . c om int y = 0; if (n != null) { while (n.getParentNode() != null) { Parent = n.getParentNode(); if (y > 0) { path = ":" + path; } y++; int i = getCurrentPosition(n); i = i - 1; // log.finest("Current position of Node = " + i); // Element elem = (Element) n; // log.finest("Node = " + elem.getLocalName() + // " AttribName = "+ // elem.getAttribute("name")); // Check to see it has children as otherwise the htmltree has a // problem with expanding leaves. if (n.hasChildNodes()) { path = i + path; } n = Parent; } } // log.severe("path = " + path); if (path == null || path.length() == 0) { path = "0"; } return path; }
From source file:Main.java
/** * Method provides getting Element objects out of a NodeList. This method is * useful if the xml file is formated with whitespaces because they are * interpreted by the DOM parser as text nodes. So especially calls to<br> * getFirstChild(), getLastChild(), etc<br> * will not surely return elements. To be sure, that you will get an Element * use this method by giving the list of child nodes * (parent.getChildNodes()) and the parent node (necessary so that only the * direct child will be returned and no deeper ones).<br> * If no Element is in the list that is a direct child of parent null will * be returned./* w w w .j ava 2s . c o m*/ * * @param childs * a NodeList that contains all nodes which should be checked * wether they are elements and direct childs of the given * parent. The first node that matches these criterions will be * returned * @param parent * the parent node of the given child nodes * * @return the first Element of the given NodeList whose parent node is the * given parent. If no such Element exists null will be returned */ public static Element getChildElement(NodeList childs, Node parent) { Node childNode; String parentName; parentName = parent.getNodeName(); for (int i = 0; i < childs.getLength(); i++) { childNode = childs.item(i); if ((childNode instanceof Element) && (childNode.getParentNode().getNodeName().equals(parentName))) { return (Element) childNode; } } return null; }
From source file:Main.java
/** * Reverse Engineers an XPath Expression of a given Node in the DOM. * //ww w . j a v a 2 s . com * @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:de.jaetzold.philips.hue.HueBridgeComm.java
static List<HueBridge> discover() { final Logger log = Logger.getLogger(HueBridge.class.getName()); final SimpleServiceDiscovery serviceDiscovery = new SimpleServiceDiscovery(); int attempted = 0; int maxAttempts = Math.min(4, Math.max(1, HueBridge.discoveryAttempts)); Map<String, URL> foundBriges = new HashMap<>(); // if nothing is found the first time try up to maxAttempts times with increasing timeouts while (foundBriges.isEmpty() && attempted < maxAttempts) { serviceDiscovery.setSearchMx(1 + attempted); serviceDiscovery.setSocketTimeout(500 + attempted * 1500); final List<? extends SimpleServiceDiscovery.Response> responses = serviceDiscovery .discover(SimpleServiceDiscovery.SEARCH_TARGET_ROOTDEVICE); try {//from w w w . ja v a 2 s . co m for (SimpleServiceDiscovery.Response response : responses) { String urlBase = null; final String usn = response.getHeader("USN"); if (usn != null && usn.matches("uuid:[-\\w]+")) { if (!foundBriges.containsKey(usn)) { final String server = response.getHeader("SERVER"); if (server != null && server.contains("IpBridge")) { final String location = response.getHeader("LOCATION"); if (location != null && location.endsWith(".xml")) { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(new URL(location).openStream()); final NodeList modelNames = doc.getElementsByTagName("modelName"); for (int i = 0; i < modelNames.getLength(); i++) { final Node item = modelNames.item(i); if (item.getParentNode().getNodeName().equals("device") && item .getTextContent().matches("(?i).*philips\\s+hue\\s+bridge.*")) { final NodeList urlBases = doc.getElementsByTagName("URLBase"); if (urlBases.getLength() > 0) { urlBase = urlBases.item(0).getTextContent(); break; } } } } } } } if (urlBase != null) { foundBriges.put(usn, new URL(urlBase)); } } } catch (Exception e) { HueBridge.lastDiscoveryException = e; log.log(Level.INFO, "Exception when dicovering devices", e); } attempted++; } List<HueBridge> result = new ArrayList<>(); for (Map.Entry<String, URL> entry : foundBriges.entrySet()) { final HueBridge bridge = new HueBridge(entry.getValue(), null); bridge.UDN = entry.getKey(); result.add(bridge); } return result; }
From source file:com.wavemaker.tools.ws.XJCCompiler.java
private static Element removeImportElement(Element element) { NodeList nodeList = element.getElementsByTagNameNS(Constants.XSD_NS, "import"); if (nodeList.getLength() == 0) { return element; // simply returns the original one }/*w ww .j ava2 s. c o m*/ // do a clone since we are going to remove the import stuffs from the // element Element elementClone = (Element) element.cloneNode(true); nodeList = elementClone.getElementsByTagNameNS(Constants.XSD_NS, "import"); List<Node> ns = new ArrayList<Node>(); for (int tmp = 0; tmp < nodeList.getLength(); tmp++) { Node importNode = nodeList.item(tmp); ns.add(importNode); } for (Node item : ns) { Node schemaNode = item.getParentNode(); schemaNode.removeChild(item); } return elementClone; }
From source file:de.openali.odysseus.chart.factory.util.LayerTypeHelper.java
public static ReferencableType getParentNode(final LayerType baseLayerType) { final Node node = baseLayerType.getDomNode(); final Node superiorNode1 = node.getParentNode(); if (superiorNode1 == null) return null; final Node superiorNode2 = superiorNode1.getParentNode(); if (superiorNode2 == null) return null; final String localName = superiorNode2.getLocalName(); try {//from w ww . j a v a2s . c om if ("Layer".equals(localName)) //$NON-NLS-1$ { final LayerDocument layerDocument = LayerDocument.Factory.parse(superiorNode2); return layerDocument.getLayer(); } else if ("Chart".equals(localName)) //$NON-NLS-1$ { final ChartDocument document = ChartDocument.Factory.parse(superiorNode2); return document.getChart(); } } catch (final XmlException e) { OdysseusChartFactory.getDefault().getLog() .log(new Status(IStatus.ERROR, OdysseusChartFactory.PLUGIN_ID, e.getLocalizedMessage(), e)); } throw new UnsupportedOperationException(); }
From source file:Main.java
public static Element getParentXElement(Element startElement, String xmlTag) { boolean found = false; if (startElement == null) return null; Node parentNode = startElement.getParentNode(); while (!found) { if (parentNode == null) found = true;//w w w. j a v a 2s. com else if (parentNode.getNodeName().equals(xmlTag)) found = true; if (!found) parentNode = parentNode.getParentNode(); } return (Element) parentNode; }
From source file:Main.java
/** * Remove empty CDATA sections from the given node and all of its descendants. Some parsers * silently discard empty CDATA sections, and this method may be used to compare the output from * parsers that behave differently with respect to empty CDATA sections. * /*from ww w . j a va 2s .c o m*/ * @param node * the node to process */ public static void removeEmptyCDATASections(Node node) { Node child = node.getFirstChild(); while (child != null) { Node next = child.getNextSibling(); switch (child.getNodeType()) { case Node.CDATA_SECTION_NODE: if (child.getNodeValue().length() == 0) { child.getParentNode().removeChild(child); } break; case Node.ELEMENT_NODE: removeEmptyCDATASections(child); } child = next; } }
From source file:Main.java
/** * Returns next sibling element node// w w w.j a v a 2 s. com * @param element * @return */ protected static Element getNextSiblingElement(Element element) { try { Node tmpNode = null; tmpNode = element.getNextSibling(); while (tmpNode.getNodeType() != Node.ELEMENT_NODE) { //fix for structure that has more than two parents null if (tmpNode.getNextSibling() == null) tmpNode = tmpNode.getParentNode(); tmpNode = tmpNode.getNextSibling(); } return (Element) tmpNode; } catch (Exception e) { e.printStackTrace(); return null; } }