IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Android Discussion :

Dom et Android, erreur avec appendChild


Sujet :

Android

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Invité
    Invité(e)
    Par défaut Dom et Android, erreur avec appendChild
    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

  2. #2
    Invité
    Invité(e)
    Par défaut
    Pour info, la solution était de remanier browseCategory comme ceci :

    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
    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());
    	}
     
    	if (category.getCategoryList() != null && !category.getCategoryList().isEmpty()) {
    		for (CategoryBean childCategory : category.getCategoryList()) {
    			categoryNode = browseCategory(childCategory, doc, categoryNode);
    		}
    	}
     
    	if (category instanceof QuestionsBean && ((QuestionsBean) category).getQuestionList() != null
    			&& !((QuestionsBean) category).getQuestionList().isEmpty()) {
    		categoryNode = browseQuestions((QuestionsBean) category, doc, categoryNode);
    	}
     
    	if (category.getParentCategory() != null) {
    		parentCategoryElement.appendChild(categoryNode);
    	}
     
    	return parentCategoryElement;
    }

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [DOM] erreur avec DOM PHP5
    Par opeo dans le forum Bibliothèques et frameworks
    Réponses: 13
    Dernier message: 20/07/2007, 16h59
  2. [DOM] Erreur sur appendChild
    Par helter_skelter dans le forum Bibliothèques et frameworks
    Réponses: 1
    Dernier message: 28/03/2007, 17h36
  3. [DOM] Problème avec appendChild
    Par it_bcn dans le forum Bibliothèques et frameworks
    Réponses: 3
    Dernier message: 16/02/2007, 12h12
  4. Erreur avec l'API dom xml
    Par bluemartini dans le forum Langage
    Réponses: 8
    Dernier message: 23/06/2006, 11h05
  5. [DOM] sous internet Explorer probleme avec appendChild
    Par magnus2005 dans le forum Général JavaScript
    Réponses: 3
    Dernier message: 27/03/2006, 17h04

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo