Bonjour,

J'ai fait quelques méthodes sur un projet Java que j'ai testées et qui marchent sans problème.

J'ai voulu les récupérer pour un projet Android.

Les voici :

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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
public void createXml(File xmlDestFile, CategoryBean category) {
	try {// création d'une fabrique de documents
		DocumentBuilderFactory fabrique = DocumentBuilderFactory.newInstance();
 
		// création d'un constructeur de documents
		DocumentBuilder constructeur;
 
		constructeur = fabrique.newDocumentBuilder();
		constructeur.setErrorHandler(new ErrorsManager());
 
		// lecture d'un document
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder parser = factory.newDocumentBuilder();
		Document doc = parser.newDocument();
 
		// category = new XmlParser().parse(new File("F:\\Android\\test_parsing.xml"));
 
		Element root = browseCategory(category, doc, doc.getDocumentElement());
 
		doc.appendChild(root);
 
		File file = XmlParser.createXml(doc, xmlDestFile, "utf-8");
 
	} catch (ParserConfigurationException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	} catch (TransformerConfigurationException e) {
		e.printStackTrace();
	} catch (TransformerException e) {
		e.printStackTrace();
	} catch (Exception e) {
		e.printStackTrace();
	}
 
}
 
private Element browseCategory(CategoryBean category, Document doc, Element parentCategoryElement) {
	Element categoryNode = null;
	// On vérifie si on est à la racine
	if (category.getParentCategory() == null) {
		parentCategoryElement = doc.createElement(MAIN_CATEGORY);
		parentCategoryElement.setAttribute(ACTIVATED, String.valueOf(category.getActivated()));
		parentCategoryElement.setAttribute(STATE, String.valueOf(category.getState()));
		parentCategoryElement.setAttribute(TITLE, category.getTitle());
		categoryNode = parentCategoryElement;
	} else {
		if (category instanceof QuestionsBean) {
			categoryNode = doc.createElement(QUESTIONS);
			if (((QuestionsBean) category).getComment() != null) {
				categoryNode.setAttribute(COMMENT, ((QuestionsBean) category).getComment());
			}
		} else {
			categoryNode = doc.createElement(CATEGORY);
		}
		categoryNode.setAttribute(ACTIVATED, String.valueOf(category.getActivated()));
		categoryNode.setAttribute(STATE, String.valueOf(category.getState()));
		categoryNode.setAttribute(TITLE, category.getTitle());
		parentCategoryElement.appendChild(categoryNode);
	}
 
	if (category.getCategoryList() != null && !category.getCategoryList().isEmpty()) {
		for (CategoryBean childCategory : category.getCategoryList()) {
			if (category.getParentCategory() != null) {
				Element categoryElement = browseCategory(childCategory, doc, categoryNode);
				parentCategoryElement.appendChild(categoryElement);
			} else {
				parentCategoryElement = browseCategory(childCategory, doc, categoryNode);
			}
		}
	}
 
	if (category instanceof QuestionsBean && ((QuestionsBean) category).getQuestionList() != null
			&& !((QuestionsBean) category).getQuestionList().isEmpty()) {
		Element questionsElement = browseQuestions((QuestionsBean) category, doc, categoryNode);
                // Element questionsElement = doc.createElement(QUESTION);
		parentCategoryElement.appendChild(questionsElement);
	}
 
	return parentCategoryElement;
}
 
private Element browseQuestions(QuestionsBean questions, Document doc, Element categoryElement) {
	for (QuestionBean question : questions.getQuestionList()) {
		Element questionElement = doc.createElement(QUESTION);
		if (question.getComment() != null) {
			questionElement.setAttribute(COMMENT, question.getComment());
		}
		questionElement.setAttribute(HAS_INT_VALUE, question.hasIntValue() ? "1" : "0");
		if (question.hasIntValue()) {
			questionElement.setAttribute(INT_VALUE, String.valueOf(question.getIntValue()));
		}
		questionElement.setAttribute(VALUE, String.valueOf(question.getValue()));
		questionElement.setAttribute(PRIORITY, String.valueOf(question.getPriority()));
		questionElement.setAttribute(ACTIVATED, String.valueOf(question.getActivated()));
		questionElement.appendChild(doc.createTextNode(question.getQuestion()));
 
		categoryElement.appendChild(questionElement);
		break;
	}
 
	return categoryElement;
}
 
public static File createXml(Document document, File xmlDestFile, String encodage) throws Exception {
	FileOutputStream fos = null;
	Writer writer = null;
	try {
		fos = new FileOutputStream(xmlDestFile);
		writer = new OutputStreamWriter(fos, encodage);
		emitDocument(document, writer, encodage);
	} catch (Exception e) {
		throw e;
	} finally {
		if (writer != null) {
			writer.flush();
			writer.close();
		}
		if (fos != null) {
			fos.flush();
			fos.close();
		}
	}
 
	return xmlDestFile;
}
 
private static final void emitDocument(Document doc, Writer writer, String encoding) throws IOException {
	TransformerFactory tf = TransformerFactory.newInstance();
	Transformer t = null;
	try {
		t = tf.newTransformer();
		t.setOutputProperty(OutputKeys.INDENT, "yes");
		t.setOutputProperty(OutputKeys.METHOD, "xml");
		t.setOutputProperty(OutputKeys.ENCODING, encoding);
		t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
	} catch (TransformerConfigurationException tce) {
		assert (false);
	}
	DOMSource doms = new DOMSource(doc);
	StreamResult sr = new StreamResult(writer);
	try {
		t.transform(doms, sr);
	} catch (TransformerException te) {
		IOException ioe = new IOException();
		ioe.initCause(te);
		throw ioe;
	}
}
Quand je fait le parentCategoryElement.appendChild(questionsElement) de la ligne 77, j'ai l'erreur suivante :

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
03-13 11:57:53.400: W/System.err(4272): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0
03-13 11:57:53.400: W/System.err(4272):     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
03-13 11:57:53.400: W/System.err(4272):     at java.util.ArrayList.add(ArrayList.java:147)
03-13 11:57:53.400: W/System.err(4272):     at org.apache.harmony.xml.dom.InnerNodeImpl.insertChildAt(InnerNodeImpl.java:126)
03-13 11:57:53.400: W/System.err(4272):     at org.apache.harmony.xml.dom.InnerNodeImpl.appendChild(InnerNodeImpl.java:52)
03-13 11:57:53.410: W/System.err(4272):     at com.firm.bonjour.xml.XmlParser.browseCategory(XmlParser.java:304)
03-13 11:57:53.410: W/System.err(4272):     at com.firm.bonjour.xml.XmlParser.browseCategory(XmlParser.java:292)
03-13 11:57:53.410: W/System.err(4272):     at com.firm.bonjour.xml.XmlParser.browseCategory(XmlParser.java:295)
03-13 11:57:53.410: W/System.err(4272):     at com.firm.bonjour.xml.XmlParser.createXml(XmlParser.java:237)
03-13 11:57:53.410: W/System.err(4272):     at com.firm.bonjour.excel.ExcelParser.parseExcelFile(ExcelParser.java:90)
03-13 11:57:53.410: W/System.err(4272):     at com.firm.bonjour.Main.loadTypeXML(Main.java:184)
03-13 11:57:53.410: W/System.err(4272):     at com.firm.bonjour.Main.onClick(Main.java:85)
03-13 11:57:53.410: W/System.err(4272):     at android.view.View.performClick(View.java:3127)
03-13 11:57:53.410: W/System.err(4272):     at android.view.View$PerformClick.run(View.java:12025)
03-13 11:57:53.410: W/System.err(4272):     at android.os.Handler.handleCallback(Handler.java:587)
03-13 11:57:53.410: W/System.err(4272):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-13 11:57:53.410: W/System.err(4272):     at android.os.Looper.loop(Looper.java:132)
03-13 11:57:53.410: W/System.err(4272):     at android.app.ActivityThread.main(ActivityThread.java:4126)
03-13 11:57:53.410: W/System.err(4272):     at java.lang.reflect.Method.invokeNative(Native Method)
03-13 11:57:53.410: W/System.err(4272):     at java.lang.reflect.Method.invoke(Method.java:491)
03-13 11:57:53.410: W/System.err(4272):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
03-13 11:57:53.410: W/System.err(4272):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
03-13 11:57:53.410: W/System.err(4272):     at dalvik.system.NativeStart.main(Native Method)
Le CategoryBean category est correctement rempli et il semble qu'il en soit de même pour le questionsElement.

Avez-vous une idée de l'erreur ?

J'utilise le SDK 3.2.

Si vous avez besoin de plus d'info n'hésitez pas.

Merci