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

Format d'échange (XML, JSON...) Java Discussion :

Ecrire un fichier xml


Sujet :

Format d'échange (XML, JSON...) Java

  1. #1
    Expert confirmé Avatar de ManusDei
    Homme Profil pro
    vilain troll de l'UE
    Inscrit en
    Février 2010
    Messages
    1 619
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : France

    Informations professionnelles :
    Activité : vilain troll de l'UE

    Informations forums :
    Inscription : Février 2010
    Messages : 1 619
    Points : 4 350
    Points
    4 350
    Par défaut Ecrire un fichier xml
    Bonjour,

    j'essaye d'écrire un fichier xml à partir de données en mémoire, et je n'ai rien en sortie, je n'arrive pas à comprendre pourquoi.

    Mon fichier fait lecture/ecriture, il y a une fonction read qui n'intervient pas au moment de l'écriture (et la lecture fonctionne parfaitement).

    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
    public class FILEProvider extends XMLProvider{
     
        private static Logger logger = Logger.getLogger (FILEProvider.class);
     
        private XMLInputFactory myfactory;
        private XMLEventReader myfile;
        private InputStream in = null;
     
        private XMLOutputFactory outfactory;
        private XMLEventWriter outputfile;
        private FileOutputStream out = null;
     
        private HashMap<String, String> path=new HashMap<String, String>();
     
     
        public FILEProvider(String file, String output) throws FileNotFoundException, XMLStreamException, ProviderException {
            super(file);
            this.myfactory = XMLInputFactory.newInstance();
            this.outfactory = XMLOutputFactory.newInstance();
            try {
                this.in = new FileInputStream(file);
            } catch (FileNotFoundException e) {
                System.out.println("Erreur, fichier : "+file+" introuvable");
                e.printStackTrace();
            }
            try {
                this.myfile=myfactory.createXMLEventReader(in);
            } catch (XMLStreamException e) {
                System.out.println("");
                e.printStackTrace();
            }
            try {
                this.out = new FileOutputStream(output);
            } catch (FileNotFoundException e) {
                System.out.println(e.getMessage());
            }
            this.outputfile = outfactory.createXMLEventWriter(out);
     
        }
     
        @SuppressWarnings("unchecked")
        public void write(Manager manager) {
            try {
                XMLEventWriter eventWriter = outfactory.createXMLEventWriter(this.out);
                // Create a EventFactory
                XMLEventFactory eventFactory = XMLEventFactory.newInstance();
                startwrite(eventWriter,eventFactory);
                for(Class<Entity> mapKey : manager.getClasses().keySet()){
                    for(Entity entity : manager.getObjects(mapKey)){
                        try {
                            XMLConverter conv = this.getConverter(entity.getClass());
                            conv.write(outputfile, entity);
                            System.out.println("Printing to output "+entity.getId());
                        } catch (ProviderException e) {
                            logger.error("Error while trying to get Converter "+entity.getClass());
                        }
                    }
                }
                eventWriter.flush();
                eventWriter.close();
            } catch (XMLStreamException e) {
                logger.error("error reading stream while trying to write file");
            }
        }
     
        private void startwrite(XMLEventWriter eventWriter, XMLEventFactory eventFactory) throws XMLStreamException {
            //end = eventFactory.createDTD("\n");
            // Create and write Start Tag
            StartDocument startDocument = eventFactory.createStartDocument();
            eventWriter.add(startDocument);
            // Create config open tag
            StartElement configStartElement = eventFactory.createStartElement("", "", "package");
            eventWriter.add(configStartElement);
        }
    }
    Tel que le code est fait pour l'instant, il devrait me ressortir un fichier contenant juste ça, car mapKey est vide.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    <?xml version="1.0" encoding="UTF-8"?>
    <PACKAGE>
    </PACKAGE>
    Sauf que je n'ai rien, le fichier n'est pas crée, et je n'ai pas non de FileNotFoundException dans le constructeur.
    Quel est le problème ?
    http://www.traducteur-sms.com/ On ne sait jamais quand il va servir, donc il faut toujours le garder sous la main

  2. #2
    Membre averti
    Inscrit en
    Avril 2010
    Messages
    239
    Détails du profil
    Informations forums :
    Inscription : Avril 2010
    Messages : 239
    Points : 313
    Points
    313
    Par défaut
    Bonjour,

    Comment appelez-vous vos méthodes ? Pouvez-vous copier le main ?

Discussions similaires

  1. Ecrire un fichier xml avec un accés par Url
    Par jlmdev dans le forum C#
    Réponses: 1
    Dernier message: 04/04/2009, 21h56
  2. [Free Pascal] Ecrire des fichiers XML
    Par M.Dlb dans le forum Free Pascal
    Réponses: 2
    Dernier message: 27/09/2007, 07h09
  3. [PHP-JS] Ecrire un fichier xml avec un string XML
    Par bobic dans le forum Langage
    Réponses: 1
    Dernier message: 22/12/2006, 14h18
  4. [C#] Ecrire un fichier XML en plusieurs fois
    Par schizette dans le forum Windows Forms
    Réponses: 3
    Dernier message: 09/08/2006, 15h54
  5. Ecrire un fichier XML avec la méthode Dom
    Par aswat dans le forum Général Python
    Réponses: 2
    Dernier message: 20/05/2006, 19h41

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