Bonjour,

J'ai des soucis de performance d'un programme Java.
Je cherche donc à l'optimiser en utilisant les XPathExpression.

J'ai bien réussi à faire un petit test.

Mais j'ai un gros souci lorsque je les inclus dans mon programme.
Ca ne marche plus !

En fait, c'est dans le main que l'erreur se produit, ici
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
 
leftField = leftFieldExpr.evaluate(isDesripteurXml);
separator = separatorExpr.evaluate(isDesripteurXml);
rightField = rightFieldExpr.evaluate(isDesripteurXml);
Chacune de ces expression prise individuellement fonctionne
Mais impossible d'en faire plus de 2 !!!

Voici le fichier XML.

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
 
<?xml version="1.0" encoding="ISO-8859-1"?>
<data type="02">
    <dataDescription>
        <dataOrganisation>
            <field role="NAME"/>
            <field role="SEPARATOR" value=";"/>
            <field role="VALUE"/>
        </dataOrganisation>
 
        <dataFields>
            <field name="ADHEOFD"/>
            <field name="ADHEOFR"/>
            <field name="BUYRBIC"/>
            <field name="BUYRLB1"/>
            <field name="CENTRDT"/>
            <field name="TYPCOMM"/>
            <field name="TYPSORA"/>
            <field name="VALORDT"/>
            <field name="ZONEGEO"/>
        </dataFields>
        <recordType>
            <firstField name="DEBFLUX"/>
            <lastField name="FINFLUX"/>
        </recordType>
    </dataDescription>
</data>
et voici la source Java des mon test unitaire.

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
 
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
 
import org.xml.sax.InputSource;
 
public class TestXPath {
 
    private static InputSource isDesripteurXml = null;
    private static XPathExpression leftFieldExpr = null;
    private static XPathExpression separatorExpr = null;
    private static XPathExpression rightFieldExpr = null;
 
    private static void init(File desripteurXml) throws FileNotFoundException, XPathExpressionException {
 
        FileInputStream fis = new FileInputStream(desripteurXml);
        isDesripteurXml = new InputSource(fis);
        compileAllXPathExpressions();
    }
 
    private static void compileAllXPathExpressions() throws XPathExpressionException {
 
        String leftFieldXPath = "//dataDescription/dataOrganisation/field[1]/@role";
        String separatorXPath = "//dataDescription/dataOrganisation/field[2]/@value";
        String rightFieldXPath = "//dataDescription/dataOrganisation/field[3]/@role";
 
        XPathFactory factory = XPathFactory.newInstance();
        XPath xPath = factory.newXPath();
 
        leftFieldExpr = xPath.compile(leftFieldXPath);
        separatorExpr = xPath.compile(separatorXPath);
        rightFieldExpr = xPath.compile(rightFieldXPath);
    }
 
    public static void main(String[] args) {
 
        File desripteurXml = new File(args[0]);
        System.out.println("Fichier '" + args[0] + "' ouvert.");
 
        try {
            init(desripteurXml);
            System.out.println("Initialisation OK.");
        } catch (FileNotFoundException e) {
            System.out.println("Erreur suite à l'ouverture du fichier '" + desripteurXml + "'.");
            System.exit(2);
        } catch (XPathExpressionException e) {
            System.out.println("Erreur de compilation: " + e.getCause());
            System.exit(2);
        }
 
        String leftField = null;
        String separator = null;
        String rightField = null;
 
        try {
            leftField = leftFieldExpr.evaluate(isDesripteurXml);
            separator = separatorExpr.evaluate(isDesripteurXml);
            rightField = rightFieldExpr.evaluate(isDesripteurXml);
        } catch (XPathExpressionException e) {
            System.out.println("xPath.evaluate: " + e.getCause());
            System.exit(2);
        }
 
        System.out.println("leftField=" + leftField);
        System.out.println("separator=" + separator);
        System.out.println("rightField=" + rightField);
    }
}
Est-ce que quelqu'un peut m'aider?

Merci.