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 :

Insérer qu'un noeud dans un document XML


Sujet :

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

  1. #1
    Membre confirmé
    Homme Profil pro
    Technophile Web
    Inscrit en
    Mai 2007
    Messages
    930
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations professionnelles :
    Activité : Technophile Web
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mai 2007
    Messages : 930
    Points : 467
    Points
    467
    Par défaut Insérer qu'un noeud dans un document XML
    Bonjour,

    Je souhaite faire une applis basée sur XML (stockage) et Java (traitement). Je souhaite que à chaque modification de mon interface soie suvegarder un document XML. On me dit que cela
    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
    public void addQuestion(Question q)
            {
                    doc=new Document();
                    Element question = new Element("question");
                    question.setAttribute("id",String.valueOf(q.getIdentifiant())); // <-- écriture de l'identifiant unique de la question
                    question.setAttribute("group",q.getGroup());
                    Element title = new Element("title"); // <-- mise à jour du titre
                    question=question.addContent(title);
                    title.setText(q.getTitle());
                    Element choice = new Element("choice"); // <-- écriture des choix possibles
                    question = question.addContent(choice);
                    choice.setAttribute("type",q.getChoiceType());
                    for(int i=0;i<q.getPossibilities().size();i++) 
                    {
                            Element poss = new Element("option");
                            poss.setText((String) q.getPossibilities().get(i));
                            choice = choice.addContent(poss);
                    }
                    XMLOutputter out = new XMLOutputter();
                    try
                    {
                            out.output(doc, new FileWriter(this.file));
                    }
                    catch(IOException e2)
                    {
                            System.out.println("Fichier non trouvé");
                    }
     
            }
    m'écris un fichier XML en entier. Donc vous imaginé, si je fait appel à cette méthode 50000 fois cela va me récrire de A à Z un fichier XML. Je voudrais cependant ne rajouter que la partie que je viens d'écrire et non pas tout le document. Est ce possible en java ?

  2. #2
    Membre du Club
    Profil pro
    Inscrit en
    Janvier 2008
    Messages
    47
    Détails du profil
    Informations personnelles :
    Localisation : France, Savoie (Rhône Alpes)

    Informations forums :
    Inscription : Janvier 2008
    Messages : 47
    Points : 56
    Points
    56
    Par défaut
    je ne crois pas que ce soit possible. Enfin, pas avec jdom. L'écriture du fichier XML se fait avec un document paramètre et non un éventuel nouveau nœud.

    Soit tu ne sauvegardes que de temps en temps en séparant l'ajout d'un nœud de la sauvegarde, soit tu payes le cout de la sauvegarde à chaque ajout.

  3. #3
    Membre confirmé
    Homme Profil pro
    Technophile Web
    Inscrit en
    Mai 2007
    Messages
    930
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations professionnelles :
    Activité : Technophile Web
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mai 2007
    Messages : 930
    Points : 467
    Points
    467
    Par défaut
    et avec SAX c'est possible alors ?

  4. #4
    Membre du Club
    Profil pro
    Inscrit en
    Janvier 2008
    Messages
    47
    Détails du profil
    Informations personnelles :
    Localisation : France, Savoie (Rhône Alpes)

    Informations forums :
    Inscription : Janvier 2008
    Messages : 47
    Points : 56
    Points
    56
    Par défaut
    oui, cela semble possible, mais je ne sais pas comment

  5. #5
    Membre confirmé
    Homme Profil pro
    Technophile Web
    Inscrit en
    Mai 2007
    Messages
    930
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations professionnelles :
    Activité : Technophile Web
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mai 2007
    Messages : 930
    Points : 467
    Points
    467
    Par défaut
    et avec stax ?

  6. #6
    Membre confirmé Avatar de T`lash
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2007
    Messages
    381
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : Saint-Pierre-Et-Miq.

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Biens de consommation

    Informations forums :
    Inscription : Septembre 2007
    Messages : 381
    Points : 519
    Points
    519
    Par défaut
    Tu peux aussi modifier le document que tu gardes en mémoire et utiliser un timer qui va enregistrer à interface régulier le fichier sur le disque si un drapeau (booléen) indique que le document a été altéré depuis la dernière sauvegarde.

    Tu fais ça toutes les 30 secondes et à la fermeture du programme.

    cela peut causer des problèmes si ton programme plante, mais ça peut être une solution si les changements effectués durant un intervalle de 30s sont importants où s'ils peuvent être rattrapé facilement.

    Tout dépend de la sensibilité de tes données.

  7. #7
    Membre confirmé
    Homme Profil pro
    Technophile Web
    Inscrit en
    Mai 2007
    Messages
    930
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations professionnelles :
    Activité : Technophile Web
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mai 2007
    Messages : 930
    Points : 467
    Points
    467
    Par défaut
    oki mais justement je ne souhaitais justement pas faire cela
    maintenant j'utilse Stax pour me gérer l etout (via buffer)
    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
    package com.datalion.exogema.application;
     
    import java.io.FileOutputStream;
    import java.io.IOException;
     
    import javax.xml.stream.XMLOutputFactory;
    import javax.xml.stream.XMLStreamException;
    import javax.xml.stream.XMLStreamWriter;
     
     
    public class Document 
    {
    	private String ENCODING="utf-8";
    	private String VERSION="1.0";
    	private FileOutputStream output; // <--  le flux de sortie
    	private XMLStreamWriter writer; // <-- l'écrivain
    	private String NS="http://datalion.eu"; // <-- le namespace
    	public Document(String URL) 
    	{
    		System.out.println("document creation");
    		XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    		try
    		{
    			this.output = new FileOutputStream(URL);
    			this.writer = outputFactory.createXMLStreamWriter(output);
    			this.writer.writeStartDocument(this.ENCODING, this.VERSION);
    			this.writer.writeStartDocument();
    			this.writer.setPrefix("tns",this.NS);
    			this.writer.setDefaultNamespace(this.NS);
    			this.writer.writeStartElement(this.NS,"enquiry");
    			this.writer.writeNamespace("dtl",this.NS);
    		}
    		catch(XMLStreamException e1)
    		{
    			e1.printStackTrace();
    		}
    		catch(IOException e2)
    		{
    			e2.printStackTrace();
    		}
    	}
    	public void add(Question q)
    	{
    	      try
    	      {
    			  this.writer.writeStartElement(this.NS,"question");
    			  this.writer.writeAttribute("group", q.getGroup());
    			  this.writer.writeAttribute("id",String.valueOf(q.getIdentifiant()));
    			  this.writer.writeStartElement(this.NS,"title");
    			  this.writer.writeCharacters(q.getTitle());
    			  this.writer.writeEndElement();
    			  this.writer.writeStartElement(this.NS,"choice");
    			  this.writer.writeCharacters(q.getChoiceType());
    			  for(int i=0;i<q.getPossibilities().size();i++)
    			  {
    				  this.writer.writeStartElement(this.NS,"possibility");
    				  this.writer.writeCharacters((q.getPossibilities().get(i).toString()));
    				  this.writer.writeEndElement();
    			  }
    			  this.writer.flush();
    	      }
    	      catch(XMLStreamException e)
    	      {
    	    	  e.printStackTrace();
    	      }
    	}
    	/** Méthode permettant de déplacer une question */
    	public void modify(int id)
    	{
     
    	}
    	/** Méthode permettant de détruire une question */
    	public void remove(int id)
    	{
     
    	}
    	public void close()
    	{
    		try 
    		{
    			this.writer.writeEndElement();
    			this.writer.writeEndDocument();
    			this.writer.flush();
    			this.writer.close();
    		} 
    		catch (XMLStreamException e) 
    		{
    			e.printStackTrace();
    		}
    	}
    }
    mais le problème c'est que je ne peux pas écrire un fichier utf-8 à ce que java me dit
    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
    javax.xml.stream.XMLStreamException: Underlying stream encoding 'Cp1252' and input paramter for writeStartDocument() method 'utf-8' do not match.
    	at com.sun.xml.internal.stream.writers.XMLStreamWriterImpl.writeStartDocument(Unknown Source)
    	at com.datalion.exogema.application.Document.<init>(Document.java:26)
    	at com.datalion.exogema.gui.MenuBarActionListener.actionPerformed(MenuBarActionListener.java:35)
    	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    	at javax.swing.AbstractButton.doClick(Unknown Source)
    	at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
    	at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
    	at java.awt.Component.processMouseEvent(Unknown Source)
    	at javax.swing.JComponent.processMouseEvent(Unknown Source)
    	at java.awt.Component.processEvent(Unknown Source)
    	at java.awt.Container.processEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Window.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)

Discussions similaires

  1. objet pour stocker la liste des noeuds dans un document xml
    Par nancy maman dans le forum Général Python
    Réponses: 3
    Dernier message: 29/08/2011, 16h28
  2. Réponses: 5
    Dernier message: 03/11/2006, 18h38
  3. recherche dans un document xml via DOM
    Par ndoye_zaff dans le forum APIs
    Réponses: 5
    Dernier message: 11/06/2003, 14h44
  4. Stocker du texte mis en forme dans un document XML
    Par ovh dans le forum XML/XSL et SOAP
    Réponses: 2
    Dernier message: 13/02/2003, 10h23

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