JDom

dadoulilya Messages postés 1 Date d'inscription mardi 16 février 2010 Statut Membre Dernière intervention 16 février 2010 - 16 févr. 2010 à 13:08
20021988 Messages postés 3 Date d'inscription jeudi 8 octobre 2009 Statut Membre Dernière intervention 21 février 2010 - 21 févr. 2010 à 12:36
Bonjour,
veuillez agréer, mes sincères salutations,
Je suis un nouveau membre dans ce site
Si il vous plaît ,je veux votre aide concernant API JDom.
J'ai crée un fichier XML à l'aide de cette API JDom.
Mon problème est le suivant:je veux qu' a partir d'une interface réalisée par SWING de créer une arborescence en XML.
Et merci d'avance.

1 réponse

20021988 Messages postés 3 Date d'inscription jeudi 8 octobre 2009 Statut Membre Dernière intervention 21 février 2010
21 févr. 2010 à 12:36
voila un petit exemple [code=java][code=java]




import java.io.*;
import org.jdom.*;
//import org.jdom.Comment;
import org.jdom.output.*;
import org.jdom.input.*;
import org.jdom.filter.*;
import java.util.List;
import java.util.Iterator;
//import org.jdom.input.SAXBuilder;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//import java.io.PrintStream;
//import java.io.PrintWriter;
import java.io.FileWriter;
import java.io.PrintWriter;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.io.IOException;



/**
* Main.
* @param args
*/



public class tentatif extends JFrame implements ActionListener


{//Ajouter cette méthodes à la classe
// a ajouter
//******************



/** Déclaration des composants*/
/*private JTextField zoneTexte;
private JTextField zoneTexte1;

private JButton bouton;*/
JPanel p ;
JPanel p1 ;
JPanel p2 ;
JPanel p3 ;
JLabel label1 ;
static JTextField e1 ;
JLabel label2 ;
static JTextField e2 ;
JLabel label3 ;
static JTextField e3 ;
JLabel label4 ;
JTextField e4 ;
JLabel label5;
JTextField e5 ;
JLabel label6 ;
JTextField e6 ;
JLabel label7 ;
JTextField e7 ;
JLabel label8 ;
JTextField e8 ;
JLabel label9 ;
JTextField e9 ;
JLabel label10;
JTextField e10 ;
JLabel label11 ;
JTextField e11 ;
JLabel label12 ;
JTextField e12 ;
JLabel label13 ;
JButton b,b1,b2,b3;

/**
* Constructeur.
*/
public tentatif() {


p= new JPanel();
BorderLayout d1= new BorderLayout();
p.setLayout(d1);
label13 = new JLabel("Le schéma dépositaire distibué:");
label13.setBounds(45,5,200,40) ;



p.add("North",label13);

label1 = new JLabel ("url");
e1 = new JTextField(10);
label2 = new JLabel ("scId");
e2 = new JTextField(10);
label3 = new JLabel ("scName");
e3 = new JTextField(10);
label4 = new JLabel ("description");
e4 = new JTextField(10);
label5 = new JLabel ("version");
e5 = new JTextField(10);
label6 = new JLabel ("serviceName");
e6 = new JTextField(10);
label7 = new JLabel ("operationName");
e7 = new JTextField(10);
label8 = new JLabel ("type");
e8 = new JTextField(10);
label9 = new JLabel ("serviceName");
e9 = new JTextField(10);
label10 = new JLabel ("binOperation");
e10 = new JTextField(10);
label11 = new JLabel ("unbinOperation");
e11 = new JTextField(10);
label12 = new JLabel ("multiplicity");
e12 = new JTextField(10);
b = new JButton("creer");
b1 = new JButton("Modifier");
b2 = new JButton("Ajouter");
b3= new JButton("Raffiner");
p1 = new JPanel ();
GridLayout d2 = new GridLayout(7,2);
p1.setLayout(d2);

p1.add(label1);
p1.add(e1);
p1.add(label2);
p1.add(e2);
p1.add(label3);
p1.add(e3);
p1.add(label4);
p1.add(e4);
p1.add(label5);
p1.add(e5);
p1.add(label6);
p1.add(e6);
p1.add(b);

p.add("West",p1);

p2 = new JPanel ();
GridLayout d3 = new GridLayout(8,2);
p2.setLayout(d3);


p2.add(label7);
p2.add(e7);
p2.add(label8);
p2.add(e8);
p2.add(label9);
p2.add(e9);
p2.add(label10);
p2.add(e10);
p2.add(label11);
p2.add(e11);
p2.add(label12);
p2.add(e12);
p2.add(b1);
p2.add(b2);
p2.add(b3);
p.add("East",p2);


b.setForeground(Color.red);

b1.setForeground(Color.red);
b3.setForeground(Color.red);

b.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);

//Crée le cadre et lui affecte le panneau

//b.addActionListener(this);

JFrame cadre = new JFrame("Le schéma dépositaire distibué");
cadre.setContentPane(p);


//Affecte ses dimensions suffisantes pour
//prendre en compte tous les controles

cadre.pack();

//Enfin,affiche la fenetre

cadre.setVisible(true);



}

static void modif()
{
racine.setName("repositoriesOSGI");
//Dans un premier temps on liste tous les reps
List listEtudiant = racine.getChildren("repository");
Iterator i = listEtudiant.iterator();
//On parcours la liste grâce à un iterator
while(i.hasNext())
{
Element courant = (Element)i.next();
//Si l'etudiant possède l'Element en question on applique
//les modifications.
// if(courant.getChild(element)!=null)

//On supprime l'Element en question
// courant.removeChild(element);
//On renomme l'Element père sachant qu'une balise XML n'accepte
//ni les espaces ni les caractères spéciaux
//"etudiant modifié" devient "etudiant_modifie"
courant.setName("repositoryOSGI");
Attribute url1 = new Attribute("url","192.168.1.150/repository/repositories.xml");
courant.setAttribute(url1);

}
}

static void affiche()
{
try
{
//On utilise ici un affichage classique avec getPrettyFormat()
XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
sortie.output(document, System.out);
}
catch (java.io.IOException e){}
}

static void enregistre(String fichier)
{
try
{
//On utilise ici un affichage classique avec getPrettyFormat()
XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
//Remarquez qu'il suffit simplement de créer une instance de FileOutputStream
//avec en argument le nom du fichier pour effectuer la sérialisation.
sortie.output(document, new FileOutputStream(fichier));
}
catch (java.io.IOException e){}
}




static void supprElement(String element)
{

List listbun = racine.getChildren("repository");//.getChildren("serviceComponent");

Iterator i = listbun.iterator();
while(i.hasNext())
{
Element courant = (Element)i.next();

if(courant.getChild(element)!=null)
{
//courant.removeChild(element);

courant.setName("1");
}}


}











static void lireFichier(String fichier) throws Exception
{
SAXBuilder sxb = new SAXBuilder();
document = sxb.build(new File(fichier));
racine = document.getRootElement();

}

static void modif1(String element)
{


//Dans un premier temps on liste tous les repository
List listEtudiant = racine.getChildren("repositoryOSGI");
Iterator i = listEtudiant.iterator();
//On parcours la liste grâce à un iterator
while(i.hasNext())
{
Element courant = (Element)i.next();
//Si l'etudiant possède l'Element en question on applique
//les modifications.
if(courant.getChild(element)!=null)
{

courant.getChild(element).setName("bundle");
}
}
}

static void modif2(String element)
{


List listEtudiant2 = racine.getChildren("repositoryOSGI");
Iterator i2 = listEtudiant2.iterator();
//On parcours la liste grâce à un iterator
while(i2.hasNext())
{
Element courant = (Element)i2.next();

if(courant.getChild(element)!=null)
{

courant.getChild(element).setName("bundle");
}
}
}



//Nous allons commencer notre arborescence en créant la racine XML
//qui sera ici "personnes".
static Element racine = new Element("repositories");

//On crée un nouveau Document JDOM basé sur la racine que l'on vient de créer
static org.jdom.Document document = new Document(racine);

public static void main(String[] args)
{
tentatif a = new tentatif();


}



public void actionPerformed(ActionEvent evt) {
// String valeuraffiche="";
/** Si on a cliqué sur le boutonde modi*/
if(evt.getSource() == b1)
{

try {
//

modif();




modif1("serviceComponent");
// enregistre("Exercice2222290.xml");

modif2("serviceComponent");
modif2("serviceComponent");
modif2("serviceComponent");
supprElement("name");
enregistre("exm2.xml");


affiche();
System.exit(1);

enregistre("exm2.xml");


} // fin try



catch(Exception e){}

}// fin if

else if(evt.getSource() == b) {
try {
Element repository = new Element("repository");
racine.addContent(repository);


Attribute url = new Attribute("url",e1.getText());
repository.setAttribute(url);

//On crée un nouvel Element nom, on lui assigne du texte
//et on l'ajoute en tant qu'Element de etudiant
Element serviceComponent = new Element("serviceComponent");

//racine.addContent(serviceComponent);
serviceComponent.setText("");
repository.addContent(serviceComponent);


Attribute id = new Attribute("id",e2.getText());
serviceComponent.setAttribute(id);



Element name = new Element("name");
name.setText("sink");
serviceComponent.addContent(name);
Element n = new Element("hela");
n.setText("sink");
name.addContent(n);

affiche();
enregistre("Exercice2.xml");



} catch(Exception e) {
e.printStackTrace();
}

}//fin if

// pour l ajout
else if(evt.getSource() == b2) {
try {


Element serviceComponent = new Element("serviceComponent");

racine.getChild("repository").addContent(serviceComponent);

Attribute id11 = new Attribute("id",e1.getText());
serviceComponent.setAttribute(id11);



Element name11 = new Element("name");
name11.setText("listenerBroker");
serviceComponent.addContent(name11);
Element n = new Element("hela");
n.setText("sink");
name11.addContent(n);
affiche();
enregistre("a.xml");


} catch(Exception e) {
e.printStackTrace();
}

}//fin if

else if(evt.getSource() == b3) {
try {


List listEtudiant = racine.getChild("repository").getChildren("serviceComponent");

// List listEtudiant = racine.getChild("repository").getChildren("serviceComponent");
Iterator i = listEtudiant.iterator();
//On parcours la liste grâce à un iterator
while(i.hasNext())
{


Element courant = (Element)i.next();
if (courant.getChild("description")==null)

//if (courant.getChild("name").getChild("description")==null)
{
Element description = new Element("description");
description.setText(e2.getText());
courant.addContent(description);


}


affiche();

enregistre("a.xml");




}


} catch(Exception e) {
e.printStackTrace();
}

}//fin if




}// fin action
}

il ya ici des champs unitile mais juste le principe bon travail
0
Rejoignez-nous