Bonjour à tous,
Depuis hier, je suis confronté à un problème avec SAX.
Le principe suivant est de pouvoir associer à chaque noeud, un xpath absolue.
Je m'explique :
En entrée le fichier XML suivant :
<data>
<title> toto </title>
<desc>
<p> blabla </p>
<p> bloblo </p>
</desc>
<data>

En sortie je veux construire le xpath suivant pour le noeud contenant le mot "bloblo"
data[1]/desc[1]/p[2]

Voici mes quelques essais (du bidoullage... ):

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
	public void startElement(String nameSpaceURI, String localName,
			String rawName, Attributes attributs) throws SAXException {
 
		switch (localName) {
 
		case "DATA":
 
			if (StringUtils.isBlank(path)) {
				path = XPathUtils.getPathFromElement(rawName, path, parentPath,
						cptBalade);
			}
			parentPath = path;
			break;
		case "TITLE":
			path += XPathUtils.getPathFromElement(rawName, path, parentPath,
					cptPres);
			parentPath = path;
			break;
		case "DESC":
			path += XPathUtils.getPathFromElement(rawName, path, parentPath,
					cptTitle);
 
			break;
		case "P":
			path = XPathUtils.getPathFromElement(rawName, path, parentPath,
					cptP);
		 }
 
			break;
 
		default:
			break;
		}
 
	}
Classe XPathUtils

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class XPathUtils {
 
	public static String getPathFromElement(String localName,
			String xPathCurrent,String parentPath, int compteurIdenfier) {
 
 
		if(compteurIdenfier == 0){
			xPathCurrent = "/" + localName + "[" + compteurIdenfier + "]";	
			parentPath=xPathCurrent;
 
 
		}
		else if(compteurIdenfier > 0 ){
 
			xPathCurrent = parentPath+ "/" + localName + "[" + compteurIdenfier + "]";	
 
		}
 
 
		return xPathCurrent;
 
	}
 
}
Je vous remercie d'avance