Probleme: erreur pour retourner une string

Freacky Messages postés 5 Date d'inscription mardi 2 novembre 2004 Statut Membre Dernière intervention 5 septembre 2005 - 4 sept. 2005 à 17:07
cs_GodConan Messages postés 2113 Date d'inscription samedi 8 novembre 2003 Statut Contributeur Dernière intervention 6 octobre 2012 - 5 sept. 2005 à 19:13
Salut,

J'ai essayé d'adapter une fonction pour qu'elle retourne un string mais j'ai un message d'erreur :



107. trans.transform(xmlSource,new StreamResult(tmp));


^-^

*** Semantic Error: The variable "tmp" may be accessed here before having been definitely assigned a value.



Voici la fonction :



public static String parse(String xml_file, String xslt_file) throws Exception


{


String tmp;


File xmlFile=new File(xml_file);


File xsltFile=new File(xslt_file);


Source xmlSource=new StreamSource(xmlFile);


Source xsltSource=new StreamSource(xsltFile);


TransformerFactory transFact=TransformerFactory.newInstance();


Transformer trans=transFact.newTransformer(xsltSource);


trans.transform(xmlSource,new StreamResult(tmp));


return tmp;


}



Le code que j'aimerais pouvoir utiliser est :



String a=parse(fichier_xml,fichier_xslt):



Merci.

6 réponses

cs_GodConan Messages postés 2113 Date d'inscription samedi 8 novembre 2003 Statut Contributeur Dernière intervention 6 octobre 2012 12
4 sept. 2005 à 19:47
ben a c est tou simple String est une class donc a ce titre avant d en utiliser une il faut l instancier ... ;o)
String tmp = new String();
ou encor String tmp = "";
car sinon ta reference tmp ne se refere a rien ... ;o)

GL

++

GodConan
0
Freacky Messages postés 5 Date d'inscription mardi 2 novembre 2004 Statut Membre Dernière intervention 5 septembre 2005
4 sept. 2005 à 19:58
Merci !!!

Tu m'a donné la solution à plusieurs de mes problemes.

@++
0
Freacky Messages postés 5 Date d'inscription mardi 2 novembre 2004 Statut Membre Dernière intervention 5 septembre 2005
4 sept. 2005 à 20:35
J'ai testé et ça me sort une autre erreur :



java.io.FileNotFoundException: Aucun fichier ou répertoire de ce type

at gnu.java.nio.channels.FileChannelImpl.open (FileChannelImpl.java)

at gnu.java.nio.channels.FileChannelImpl. (FileChannelImpl.java:108)

at java.io.FileOutputStream. (FileOutputStream.java:158)

at java.io.FileOutputStream. (FileOutputStream.java:85)

at java.io.FileOutputStream. (FileOutputStream.java:106)

at gnu.xml.transform.TransformerImpl.writeStreamResult (TransformerImpl.java:459)

at gnu.xml.transform.TransformerImpl.transform (TransformerImpl.java:328)

at http.parse (http.java:93)

at http.run (http.java:52)

at http.main (http.java:100)



Code :



public static String parse(String xml_file, String xslt_file) throws Exception

{

String result="";

File xmlFile=new File(xml_file);

File xsltFile=new File(xslt_file);

Source xmlSource=new StreamSource(xmlFile);

Source xsltSource=new StreamSource(xsltFile);

TransformerFactory transFact=TransformerFactory.newInstance();

Transformer trans = transFact.newTransformer(xsltSource);

trans.transform(xmlSource, new StreamResult(result));

return result;

}
0
cs_GodConan Messages postés 2113 Date d'inscription samedi 8 novembre 2003 Statut Contributeur Dernière intervention 6 octobre 2012 12
4 sept. 2005 à 21:31
ben le message est claire ;o)
ton fichier n existe pas ..; ;o)

GodConan
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
Freacky Messages postés 5 Date d'inscription mardi 2 novembre 2004 Statut Membre Dernière intervention 5 septembre 2005
5 sept. 2005 à 17:51
Justement non, parce que la version void fonctionne :

public static void parse(String xml_file,String xslt_file) throws Exception {

File xmlFile = new File(xml_file);
File xsltFile = new File(xslt_file);

Source xmlSource = new StreamSource(xmlFile);
Source xsltSource = new StreamSource(xsltFile);

TransformerFactory transFact =
TransformerFactory.newInstance();
Transformer trans = transFact.newTransformer(xsltSource);

trans.transform(xmlSource, new StreamResult(System.out));
}
0
cs_GodConan Messages postés 2113 Date d'inscription samedi 8 novembre 2003 Statut Contributeur Dernière intervention 6 octobre 2012 12
5 sept. 2005 à 19:13
y a un truc pas claire dans ton prog... ;o)
ta class StreamResult reclame un flux et toi tu lui donne une string !!!!
je pense que ca doit etre prevu qd meme par la class puisque il te jete pas a la compile mais vefrifi qd meme si ta pas oublier qque chose pour ce cas preci ... ;o)

GL

++

GodConan
0
Rejoignez-nous