Bonjour a tous .

Je voudrais implémenter s'il vous plait une méthode qui parse une fichier xml qui existe dans une répertoire par exemple D:\Dictionnaire.

J'ai implémenté une méthode qui prend le nom de fichier et le parser. Mais le problème qu'il faut toujours que le fichier soit dans le répertoire du projet courant.

Mon but est d'étendre cette méthode de prendre le nom de fichier et aller chercher dans le répertoire par exemple D:\Dictionnaire , ensuite la parser.

voici la méthode:
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
75
76
77
78
79
80
81
82
 
public static DlmsObjectInformation getDlmsObjectInformationfromTask(String nameOfTask) {
		DlmsObjectInformation dlmsObjectInformation = new DlmsObjectInformation();/*
																	 * the
																	 * result
																	 * of
																	 * the
																	 * method.
																	 * It
																	 * contains
																	 * the
																	 * information
															   	          * of
																	 * dlms
																	 * element
																	 * of
																	 * result
																	 * task
																	 * file.
																	 */
		XMLInputFactory factory = XMLInputFactory.newInstance(); // provides
																// objects
																// that will
																// read an
																// XML
																// document.
		File file = new File(nameOfTask);
		Boolean found = false; // if the dlms element is found , found will be
								// true.
		try {
 
			XMLStreamReader reader = factory.createXMLStreamReader(new FileReader(file));// the
																		// object
																		// that
																		// will
																		// parse
	  																     // the		
                                                                                                                                               // file.																				          
			while (reader.hasNext() && !found) {
				int type = reader.next();// retrive event.
				if (type == XMLStreamReader.START_ELEMENT && reader.getLocalName().equals(
						"dlms")) {/*
									 * If it is a start market, it is verified
									 * that this is a dlms market
									 */
					found = true;
					int nbrOfAtt = reader.getAttributeCount();
					Map<String, String> attributesValues = new HashMap<>(nbrOfAtt);// intermediate
																	// variable
																	// that
																	// will
																	// store
																	// the
																	// Information
																	// given
																	// by
																         // the
																	// dlms
																	// element.
					for (int i = 0; i < nbrOfAtt; i++) {
						attributesValues.put(reader.getAttributeName(i).toString(), reader.getAttributeValue(i));
					}
 
					String frameValue = reader.getElementText();
					dlmsObjectInformation.setAttribute(attributesValues.get("attribute"));
					dlmsObjectInformation.setClassId(attributesValues.get("classId"));
					dlmsObjectInformation.setFrameValue(frameValue);
					dlmsObjectInformation.setObis(attributesValues.get("obis"));
					dlmsObjectInformation.setType(attributesValues.get("type"));
				}
			}
 
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XMLStreamException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
 
		return dlmsObjectInformation;
	}
La méthode est bien commenté .Vous pouvez bien la comprendre .
Mon problème réside comment je vais charger le fichier de répertoire par exemple D:\Dictionnaire.parce que dans la méthode précedente
Code : Sélectionner tout - Visualiser dans une fenêtre à part
File file = new File(nameOfTask);
.
le fichier est dans la répertoire de projet.
Si je change le fichier dans la répertoire D:\Dictionnaire , exeption file not found.

Mercii.