@Test public void testGetUniqueXPath() throws Exception { final Document testDoc = loadTestDocument(); final XPathFactory xPathFactory = XPathFactory.newInstance(); final XPath xPath = xPathFactory.newXPath(); final XPathExpression xPathExpression = xPath.compile(" final Node node = (Node) xPathExpression.evaluate(testDoc, XPathConstants.NODE); final XmlUtilitiesImpl xmlUtilities = new XmlUtilitiesImpl(); final String nodePath = xmlUtilities.getUniqueXPath(node); assertEquals("/layout/folder[2]/folder[3]", nodePath); }
@Override public String getUniqueXPath(Node node) { if (node == null) { throw new IllegalArgumentException("Node cannot be null"); } final StringBuilder path = new StringBuilder(); for (; node != null && node.getNodeType() == Node.ELEMENT_NODE; node = node.getParentNode()) { final int elementIndex = getElementIndex(node); final String nodeName = node.getNodeName(); if (elementIndex > 1) { path.insert(0, "]").insert(0, elementIndex).insert(0, "["); } path.insert(0, nodeName).insert(0, "/"); } return path.toString(); }
XmlUtilitiesImpl implements XmlUtilities { @Override public String getUniqueXPath(Node node) { if (node == null) { throw new IllegalArgumentException("Node cannot be null"); } final StringBuilder path = new StringBuilder(); for (; node != null && node.getNodeType() == Node.ELEMENT_NODE; node = node.getParentNode()) { final int elementIndex = getElementIndex(node); final String nodeName = node.getNodeName(); if (elementIndex > 1) { path.insert(0, "]").insert(0, elementIndex).insert(0, "["); } path.insert(0, nodeName).insert(0, "/"); } return path.toString(); } }
XmlUtilitiesImpl implements XmlUtilities { @Override public String getUniqueXPath(Node node) { if (node == null) { throw new IllegalArgumentException("Node cannot be null"); } final StringBuilder path = new StringBuilder(); for (; node != null && node.getNodeType() == Node.ELEMENT_NODE; node = node.getParentNode()) { final int elementIndex = getElementIndex(node); final String nodeName = node.getNodeName(); if (elementIndex > 1) { path.insert(0, "]").insert(0, elementIndex).insert(0, "["); } path.insert(0, nodeName).insert(0, "/"); } return path.toString(); } XmlUtilitiesImpl(); }
XmlUtilitiesImpl implements XmlUtilities { @Override public String getUniqueXPath(Node node) { if (node == null) { throw new IllegalArgumentException("Node cannot be null"); } final StringBuilder path = new StringBuilder(); for (; node != null && node.getNodeType() == Node.ELEMENT_NODE; node = node.getParentNode()) { final int elementIndex = getElementIndex(node); final String nodeName = node.getNodeName(); if (elementIndex > 1) { path.insert(0, "]").insert(0, elementIndex).insert(0, "["); } path.insert(0, nodeName).insert(0, "/"); } return path.toString(); } XmlUtilitiesImpl(); @Autowired void setCachingResourceLoader(CachingResourceLoader cachingResourceLoader); @Autowired void setTemplatesBuilder(TemplatesBuilder templatesBuilder); @Override Templates getTemplates(Resource stylesheet); @Override Transformer getTransformer(Resource stylesheet); @Override Transformer getIdentityTransformer(); @Override Serializable getStylesheetCacheKey(Resource stylesheet); @Override XMLOutputFactory getXmlOutputFactory(); @Override XMLOutputFactory getHtmlOutputFactory(); @Override XMLInputFactory getXmlInputFactory(); @Override String serializeXMLEvents(List<XMLEvent> xmlEvents); @Override String serializeXMLEvents(List<XMLEvent> xmlEvents, boolean isHtml); @Override Node convertToDom(XMLEventReader xmlEventReader); @Override String getUniqueXPath(Node node); static String getElementText(Element e); static String toString(Node node); static String toString(XMLEvent event); }
XmlUtilitiesImpl implements XmlUtilities { @Override public String getUniqueXPath(Node node) { if (node == null) { throw new IllegalArgumentException("Node cannot be null"); } final StringBuilder path = new StringBuilder(); for (; node != null && node.getNodeType() == Node.ELEMENT_NODE; node = node.getParentNode()) { final int elementIndex = getElementIndex(node); final String nodeName = node.getNodeName(); if (elementIndex > 1) { path.insert(0, "]").insert(0, elementIndex).insert(0, "["); } path.insert(0, nodeName).insert(0, "/"); } return path.toString(); } XmlUtilitiesImpl(); @Autowired void setCachingResourceLoader(CachingResourceLoader cachingResourceLoader); @Autowired void setTemplatesBuilder(TemplatesBuilder templatesBuilder); @Override Templates getTemplates(Resource stylesheet); @Override Transformer getTransformer(Resource stylesheet); @Override Transformer getIdentityTransformer(); @Override Serializable getStylesheetCacheKey(Resource stylesheet); @Override XMLOutputFactory getXmlOutputFactory(); @Override XMLOutputFactory getHtmlOutputFactory(); @Override XMLInputFactory getXmlInputFactory(); @Override String serializeXMLEvents(List<XMLEvent> xmlEvents); @Override String serializeXMLEvents(List<XMLEvent> xmlEvents, boolean isHtml); @Override Node convertToDom(XMLEventReader xmlEventReader); @Override String getUniqueXPath(Node node); static String getElementText(Element e); static String toString(Node node); static String toString(XMLEvent event);  }