Re bonjour,
J'ai réussi à trouver un fonction faisant ce que je veux mais (parce que y'a toujours un mais
sinon c'est pas marrant), j'ai l'erreur suivante.
the method getPreviousSibling() is undefined for the type Node
Je vous mets le code utilisé.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
private String getXPath(Node n) {
// node's full XPath
StringBuffer nodePath = new StringBuffer();
int pos = 1; // default position of an element
Node checker = n; // get a copy current node reference
// browse previous siblings of current node
while ((checker = checker.getPreviousSibling()) != null) {
// if it is an Element, increment position of current node
if (checker.getNodeType() == Node.ELEMENT_NODE) {
pos++;
}
}
// no more previous siblings:
// insert current node path in the full node path
nodePath.insert(0, "/*[" + pos + "]");
// browse current node parents
while ((n = n.getParent()) != null) {
// if it is an Element, compute its position and
// insert node path in the full node path
if (n.getNodeType() == Node.ELEMENT_NODE) {
pos = 1; // reset current node position
checker = n; // get a copy current node reference
// browse previous siblings of current node
while ((checker = checker.getPreviousSibling()) != null) {
// if it is an Element, increment position of current node
if (checker.getNodeType() == Node.ELEMENT_NODE) {
pos++;
}
}
// no more previous siblings:
// insert current node path in the full node path
nodePath.insert(0, "/*[" + pos + "]");
}
}
return nodePath.toString();
} |
Merci de votre aide
Partager