Précédent   Forum des professionnels en informatique > Java > Général Java
Général Java Java SE, Java ME, APIs, Persistance, JDBC, Spring, XML. Avant de poster -> FAQ Java, Sources Java
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 07/02/2012, 11h16   #1
Membre du Club
 
Femme
Étudiant
Inscription : juillet 2011
Messages : 114
Détails du profil
Informations personnelles :
Sexe : Femme
Localisation : Tunisie

Informations professionnelles :
Activité : Étudiant
Secteur : High Tech - Multimédia et Internet

Informations forums :
Inscription : juillet 2011
Messages : 114
Points : 49
Points : 49
Envoyer un message via MSN à nounouuuuu201186 Envoyer un message via Skype™ à nounouuuuu201186
Par défaut Appel de code C# à partir de Java

Bonjour,
J'ai un code C# que je veux l'exécuter au sein d'Eclipse:
Code :
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
public bool LoadPictures(string searchType, string searchWord)
{
    switch (searchType)
    {
        case "Most Recent":
            this.url=MOST_RECENT;
            break;
        case "Interesting":
            this.url=INTERESTING;
            break;
        case "By Search Word":
            this.url = ENTER_TAG + searchWord;
            break;
        default :
            this.url = MOST_RECENT;
            break;
    }
 
    try
    {
        var xraw = XElement.Load(url);
        var xroot = XElement.Parse(xraw.Xml);
        //select the RSS data from Flickr, and use standard LINQ projection
        //to store it within a new PhotoInfo which is an object of my own
    //making
        var photos = (from photo in xroot.Element("photos").Elements("photo")
            select new PhotoInfo
            {
                Id = (string)photo.Attribute("id"),
                Owner = (string)photo.Attribute("owner"),
                Title = (string)photo.Attribute("title"),
                Secret = (string)photo.Attribute("secret"),
                Server = (string)photo.Attribute("server"),
                Farm = (string)photo.Attribute("Farm"),
            }).Skip(pageIndex * columns * rows).Take(columns * rows);
 
        //set the allowable next/prev states
        int count = photos.Count();
 
        if (pageIndex == 0)
        {
            this.prevAvail = false;
            this.nextAvail = true;
        }
        else
        {
            this.prevAvail = true;
        }
        //see if there are less photos than sum(Columns * Rows) if there are
        //less cant allow next operation
        if (count < columns * rows)
        {
            this.nextAvail = false;
        }
        //then write results out to a file, which can then be used by XPF
        //Application
        XDocument photosDoc = new XDocument();
        XElement photosElement = new XElement("photos", from pi in photos
                select new XElement("photo",
                    new XElement("url", pi.PhotoUrl(false)),
                    new XElement("title", pi.Title))
            );
        photosDoc.Add(photosElement);
        photosDoc.Save(@"c:\photos.xml");
        return true;
    }
    catch (Exception ex)
    {
        return false;
    }
}
Y a-t-il quelqu'un qui peut me dire comment faire pour exécuter un code C# au sein d'une application java?
nounouuuuu201186 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 07/02/2012, 11h24   #2
Modérateur
 
Avatar de Robin56
 
Homme Nicolas
Ingénieur développement logiciels
Inscription : juin 2009
Messages : 1 715
Détails du profil
Informations personnelles :
Nom : Homme Nicolas
Localisation : France

Informations professionnelles :
Activité : Ingénieur développement logiciels

Informations forums :
Inscription : juin 2009
Messages : 1 715
Points : 4 826
Points : 4 826
Citation:
Envoyé par nounouuuuu201186 Voir le message
Y a-t-il quelqu'un qui peut me dire comment faire pour exécuter un code C# au sein d'une application java?
Je pencherais pour l'utilisation de JNI qui permettrait de faire des appels à du code C# au sein d'une DLL. Mais dans ton cas, je préconiserais la traduction de ton code en Java puisqu'il n'a pas l'air si long que cela.
__________________
Robin56 est actuellement connecté   Envoyer un message privé Réponse avec citation 00
Vieux 07/02/2012, 11h26   #3
Membre du Club
 
Femme
Étudiant
Inscription : juillet 2011
Messages : 114
Détails du profil
Informations personnelles :
Sexe : Femme
Localisation : Tunisie

Informations professionnelles :
Activité : Étudiant
Secteur : High Tech - Multimédia et Internet

Informations forums :
Inscription : juillet 2011
Messages : 114
Points : 49
Points : 49
Envoyer un message via MSN à nounouuuuu201186 Envoyer un message via Skype™ à nounouuuuu201186
Voici le code complet:
Code :
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
using System;
using System.Collections.Generic;
using System.Text;
using System.Query;
using System.Xml.XLinq;
using System.Data.DLinq;
 
namespace ImageTest
{
 
 
 
    public class RSSImageFeed
    {
        private const string FLICKR_API_KEY = "c705bfbf75e8d40f584c8a946cf0834c";
        private const string MOST_RECENT = "http://www.flickr.com/services/rest/?method=flickr.photos.getRecent&api_key=" + FLICKR_API_KEY;
        private const string INTERESTING = "http://www.flickr.com/services/rest/?method=flickr.interestingness.getList&api_key=" + FLICKR_API_KEY;
        private const string ENTER_TAG = "http://www.flickr.com/services/rest/?method=flickr.photos.search&api_key=" + FLICKR_API_KEY + "&tags=";
        private string url = MOST_RECENT;
        private int pageIndex = 0;
        private int columns = 5;
        private int rows = 2;
        private bool prevAvail = false;
        private bool nextAvail = false;
 
 
        public RSSImageFeed()
        {
 
        }
 
        public bool IsPrevAvail
        {
            get { return prevAvail; }
        }
        public bool IsNextAvail
        {
            get { return nextAvail; }
        }
 
        public int PageIndex
        {
            set { pageIndex = value; }
            get { return pageIndex; }
        }
 
 
 
        public bool LoadPictures(string searchType, string searchWord)
        {
 
 
            switch (searchType)
            {
                case "Most Recent":
                    this.url=MOST_RECENT;
                    break;
                case "Interesting":
                    this.url=INTERESTING;
                    break;
                case "By Search Word":
                    this.url = ENTER_TAG + searchWord;
                    break;
                default :
                    this.url = MOST_RECENT;
                    break;
            }
 
            try
            {
                var xraw = XElement.Load(url);
                var xroot = XElement.Parse(xraw.Xml);
                //select the RSS data from Flickr, and use standard LINQ projection
                //to store it within a new PhotoInfo which is an object of my own making
                var photos = (from photo in xroot.Element("photos").Elements("photo")
                    select new PhotoInfo
                    { 
                        Id = (string)photo.Attribute("id"),
                        Owner = (string)photo.Attribute("owner"),
                        Title = (string)photo.Attribute("title"),
                        Secret = (string)photo.Attribute("secret"),
                        Server = (string)photo.Attribute("server"),
                        Farm = (string)photo.Attribute("Farm"),
                    }).Skip(pageIndex * columns * rows).Take(columns * rows);
 
                //set the allowable next/prev states
                int count = photos.Count();
 
                if (pageIndex == 0)
                {
                    this.prevAvail = false;
                    this.nextAvail = true;
                }
                else
                {
                    this.prevAvail = true;
                }
                //see if there are less photos than sum(Columns * Rows) if there are less
                //cant allow next operation
                if (count < columns * rows)
                {
                    this.nextAvail = false;
                }
                //then write results out to a file, which can then be used by XPF Application
                XDocument photosDoc = new XDocument();
                XElement photosElement = new XElement("photos", from pi in photos
                        select new XElement("photo",
                            new XElement("url", pi.PhotoUrl(false)),
                            new XElement("title", pi.Title))
                    );
                photosDoc.Add(photosElement);
                photosDoc.Save(@"c:\photos.xml");
                return true;
 
            }
            catch (Exception ex)
            {
                return false;
            }
        }
 
 
 
 
    }
}
nounouuuuu201186 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 07/02/2012, 11h31   #4
Membre du Club
 
Femme
Étudiant
Inscription : juillet 2011
Messages : 114
Détails du profil
Informations personnelles :
Sexe : Femme
Localisation : Tunisie

Informations professionnelles :
Activité : Étudiant
Secteur : High Tech - Multimédia et Internet

Informations forums :
Inscription : juillet 2011
Messages : 114
Points : 49
Points : 49
Envoyer un message via MSN à nounouuuuu201186 Envoyer un message via Skype™ à nounouuuuu201186
Merci Robin56.
Donc, il est préférable de le traduire en java.
Pour moi, je n'ai aucune connaissance avec C# mais je veux essayer quand même.
nounouuuuu201186 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 07/02/2012, 11h43   #5
Modérateur
 
Avatar de Robin56
 
Homme Nicolas
Ingénieur développement logiciels
Inscription : juin 2009
Messages : 1 715
Détails du profil
Informations personnelles :
Nom : Homme Nicolas
Localisation : France

Informations professionnelles :
Activité : Ingénieur développement logiciels

Informations forums :
Inscription : juin 2009
Messages : 1 715
Points : 4 826
Points : 4 826
Citation:
Envoyé par nounouuuuu201186 Voir le message
Merci Robin56.
Donc, il est préférable de le traduire en java.
Pour moi, je n'ai aucune connaissance avec C# mais je veux essayer quand même.
De ce que je vois et de mes connaissances basiques de C#, il ne semble pas y avoir quoi que ce soit spécifique au langage dans ce morceau de code. Les traitements qui vont changer concerneront le chargement de l'URL puis le parsing (en gros, ce qui fait intervenir XElement).

PS : J'ajouterais que pour toute cette partie XML, l'utilisation d'API comme JDOM ou concurrent est appropriée.
__________________
Robin56 est actuellement connecté   Envoyer un message privé Réponse avec citation 00
Vieux 07/02/2012, 13h21   #6
Membre du Club
 
Femme
Étudiant
Inscription : juillet 2011
Messages : 114
Détails du profil
Informations personnelles :
Sexe : Femme
Localisation : Tunisie

Informations professionnelles :
Activité : Étudiant
Secteur : High Tech - Multimédia et Internet

Informations forums :
Inscription : juillet 2011
Messages : 114
Points : 49
Points : 49
Envoyer un message via MSN à nounouuuuu201186 Envoyer un message via Skype™ à nounouuuuu201186
Voici mon nouveau code:
Code :
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
import java.net.URL;
 
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
 
import org.w3c.dom.Document;
 
 
 
public class FlickrFeed {
 
	private  String FLICKR_API_KEY = "204e5627ea6626101221a5c7b4b0dd3a";
    private  String MOST_RECENT = "http://www.flickr.com/services/rest/?method=flickr.photos.getRecent&api_key=" + FLICKR_API_KEY;
    private  String INTERESTING = "http://www.flickr.com/services/rest/?method=flickr.interestingness.getList&api_key=" + FLICKR_API_KEY;
    private  String ENTER_TAG = "http://www.flickr.com/services/rest/?method=flickr.photos.search&api_key=" + FLICKR_API_KEY + "&tags=";
    private String url = MOST_RECENT;
    private int pageIndex = 0;
    private int columns = 5;
    private int rows = 2;
    private boolean prevAvail = false;
    private boolean nextAvail = false;
 
    public enum SearchType
    {
    	Most_Recent, Interesting, By_Search_Word
    }
 
    public FlickrFeed()
    {
 
    }
    public boolean IsPrevAvail()
    {
        return prevAvail; 
    }
    public boolean IsNextAvail()
    {
         return nextAvail; 
    }
    public int PageIndex(int value)
    {
         pageIndex = value; 
         return pageIndex; 
    }
    public void LoadPictures( String searchWord)
    {
    	SearchType searchType = SearchType.Most_Recent;
 
    	switch (searchType)
        {
            case Most_Recent:
                this.url=MOST_RECENT;
                break;
            case Interesting:
                this.url=INTERESTING;
                break;
            case By_Search_Word:
                this.url = ENTER_TAG + searchWord;
                break;
            default :
                this.url = MOST_RECENT;
                break;
        }
 
 
    	 try
         {
 
 
    		 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
             URL Url = new URL(url);
             Document doc = builder.parse(Url.openStream());
.......
concerneront le chargement de l'URL puis le parsing, j'ai changé
Code :
1
2
 var xraw = XElement.Load(url);
            var xroot = XElement.Parse(xraw.Xml);
par
Code :
1
2
3
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
             URL Url = new URL(url);
             Document doc = builder.parse(Url.openStream());
Je me suis bloquée à ce niveau:
Code :
1
2
3
4
5
6
7
8
9
10
11
12
//select the RSS data from Flickr, and use standard LINQ projection
             //to store it within a new PhotoInfo which is an object of my own making
             var photos = (from photo in xroot.Element("photos").Elements("photo")
                 select new PhotoInfo
                 { 
                     Id = (string)photo.Attribute("id"),
                     Owner = (string)photo.Attribute("owner"),
                     Title = (string)photo.Attribute("title"),
                     Secret = (string)photo.Attribute("secret"),
                     Server = (string)photo.Attribute("server"),
                     Farm = (string)photo.Attribute("Farm"),
                 }).Skip(pageIndex * columns * rows).Take(columns * rows);
Pouvez-vous m'aider pour continuer?
nounouuuuu201186 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 07/02/2012, 13h30   #7
Modérateur
 
Avatar de Robin56
 
Homme Nicolas
Ingénieur développement logiciels
Inscription : juin 2009
Messages : 1 715
Détails du profil
Informations personnelles :
Nom : Homme Nicolas
Localisation : France

Informations professionnelles :
Activité : Ingénieur développement logiciels

Informations forums :
Inscription : juin 2009
Messages : 1 715
Points : 4 826
Points : 4 826
Citation:
Envoyé par Robin56 Voir le message
PS : J'ajouterais que pour toute cette partie XML, l'utilisation d'API comme JDOM ou concurrent est appropriée.
Utilise ce conseil. Cette partie de code parcourt ton document XML en fonction des balises trouvées.
__________________
Robin56 est actuellement connecté   Envoyer un message privé Réponse avec citation 00
Vieux 07/02/2012, 15h22   #8
Membre du Club
 
Femme
Étudiant
Inscription : juillet 2011
Messages : 114
Détails du profil
Informations personnelles :
Sexe : Femme
Localisation : Tunisie

Informations professionnelles :
Activité : Étudiant
Secteur : High Tech - Multimédia et Internet

Informations forums :
Inscription : juillet 2011
Messages : 114
Points : 49
Points : 49
Envoyer un message via MSN à nounouuuuu201186 Envoyer un message via Skype™ à nounouuuuu201186
Voici mon nouveau code:
Code :
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
 
 
import java.net.URL;
 
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
 
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
 
 
 
public class FlickrFeed {
 
	private  String FLICKR_API_KEY = "204e5627ea6626101221a5c7b4b0dd3a";
    private  String MOST_RECENT = "http://www.flickr.com/services/rest/?method=flickr.photos.getRecent&api_key=" + FLICKR_API_KEY;
    private  String INTERESTING = "http://www.flickr.com/services/rest/?method=flickr.interestingness.getList&api_key=" + FLICKR_API_KEY;
    private  String ENTER_TAG = "http://www.flickr.com/services/rest/?method=flickr.photos.search&api_key=" + FLICKR_API_KEY + "&tags=";
    private String url = MOST_RECENT;
    private int pageIndex = 0;
    private int columns = 5;
    private int rows = 2;
    private boolean prevAvail = false;
    private boolean nextAvail = false;
 
    public enum SearchType
    {
    	Most_Recent, Interesting, By_Search_Word
    }
 
    public FlickrFeed()
    {
 
    }
    public boolean IsPrevAvail()
    {
        return prevAvail; 
    }
    public boolean IsNextAvail()
    {
         return nextAvail; 
    }
    public int PageIndex(int value)
    {
         pageIndex = value; 
         return pageIndex; 
    }
    public boolean LoadPictures( String searchWord)
    {
    	SearchType searchType = SearchType.By_Search_Word;
 
    	switch (searchType)
        {
            case Most_Recent:
                this.url=MOST_RECENT;
                break;
            case Interesting:
                this.url=INTERESTING;
                break;
            case By_Search_Word:
                this.url = ENTER_TAG + searchWord;
                break;
            default :
                this.url = MOST_RECENT;
                break;
        }
 
 
    	 try
         {
 
 
    		 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
             URL Url = new URL(url);
             Document doc = builder.parse(Url.openStream());
             NodeList nodes = null;
             Element element = null;
 
             //select the RSS data from Flickr, and store it within a new PhotoInfo which is an object of my own making
             nodes = doc.getElementsByTagName("photo");
             for (int i = 0; i < nodes.getLength(); i++) {
            	 element = (Element) nodes.item(i);
            String Id =	 (String)element.getAttribute("id");
            	 System.out.println("id:="+Id);
            String Owner=(String)element.getAttribute("owner");
                 System.out.println("Owner:="+Owner);
            String Title =	 (String)element.getAttribute("title");
                 System.out.println("Title:="+Title);
            String  Secret=	 (String)element.getAttribute("secret");
                 System.out.println("Secret:="+Secret);
            String  Server=	 (String)element.getAttribute("server");
                 System.out.println("Server:="+Server);
            String  Farm=	 (String)element.getAttribute("farm");
                 System.out.println("Farm:="+Farm);
            	 System.out.println("ok: " + nodes.getLength());
 
 
             }
 
 
             //set the allowable next/prev states
 
            // int count = photos.Count();
             int count = nodes.getLength();
             if (pageIndex == 0)
             {
                 this.prevAvail = false;
                 this.nextAvail = true;
             }
             else
             {
                 this.prevAvail = true;
 
             //see if there are less photos than sum(Columns * Rows) if there are less
             //cant allow next operation
             if (count < columns * rows)
             {
                 this.nextAvail = false;
             }
             //then write results out to a file, which can then be used by XPF Application
             Document photosDoc = new Document();
             Element photosElement = new Element("photos", from pi in photos
                     select new XElement("photo",
                         new XElement("url", pi.PhotoUrl(false)),
                         new XElement("title", pi.Title))
                 );
             photosDoc.Add(photosElement);
             photosDoc.Save("C:\\photos.xml");
             return true;
 
         }
         catch (Exception ex)
         {
             return false;
         }
    	 /**
    	     * Methode permettant de retourner ce que contient un noeud
    	     * @param _node le noeud principal
    	     * @param _path suite des noms des noeud sans espace separés par des "|"
    	     * @return un string contenant la valeur du noeud voulu
    	     */
 
 
    }
    public String readNode(Node _node, String _path) {
 
        String[] paths = _path.split("\\|");
        Node node = null;
 
        if (paths != null && paths.length > 0) {
            node = _node;
 
            for (int i = 0; i < paths.length; i++) {
                node = getChildByName(node, paths[i].trim());
            }
        }
 
        if (node != null) {
            return node.getTextContent();
        } else {
            return "";
        }
    }
    /**
     * renvoye le nom d'un noeud fils a partir de son nom
     * @param _node noeud pricipal
     * @param _name nom du noeud fils
     * @return le noeud fils
     */
    public Node getChildByName(Node _node, String _name) {
        if (_node == null) {
            return null;
        }
        NodeList listChild = _node.getChildNodes();
 
        if (listChild != null) {
            for (int i = 0; i < listChild.getLength(); i++) {
                Node child = listChild.item(i);
                if (child != null) {
                    if ((child.getNodeName() != null && (_name.equals(child.getNodeName()))) || (child.getLocalName() != null && (_name.equals(child.getLocalName())))) {
                        return child;
                    }
                }
            }
        }
        return null;
    }
    public static void main(String[] args) {
    	FlickrFeed f= new FlickrFeed();
    	f.LoadPictures("upcoming:event");
 
    }
 
 
}
Je me suis bloquée au niveau:
Code :
1
2
3
4
5
6
7
8
9
//then write results out to a file, which can then be used by XPF Application
             Document photosDoc = new Document();
             Element photosElement = new Element("photos", from pi in photos
                     select new XElement("photo",
                         new XElement("url", pi.PhotoUrl(false)),
                         new XElement("title", pi.Title))
                 );
             photosDoc.Add(photosElement);
             photosDoc.Save("C:\\photos.xml");
Pouvez-vous me soutenir?
nounouuuuu201186 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 07/02/2012, 15h33   #9
Modérateur
 
Avatar de Robin56
 
Homme Nicolas
Ingénieur développement logiciels
Inscription : juin 2009
Messages : 1 715
Détails du profil
Informations personnelles :
Nom : Homme Nicolas
Localisation : France

Informations professionnelles :
Activité : Ingénieur développement logiciels

Informations forums :
Inscription : juin 2009
Messages : 1 715
Points : 4 826
Points : 4 826
Citation:
Envoyé par nounouuuuu201186 Voir le message
Pouvez-vous me soutenir?
Je te soutiens... et je me répète encore, c'est le même problème. Au dessus c'était de la lecture de fichier XML. Maintenant c'est de l'écriture de fichier XML.

PS Ceci aurait pu t'aider pour toutes ces opérations de lecture/écriture XML (ici)
__________________
Robin56 est actuellement connecté   Envoyer un message privé Réponse avec citation 00
Vieux 08/02/2012, 15h39   #10
Membre du Club
 
Femme
Étudiant
Inscription : juillet 2011
Messages : 114
Détails du profil
Informations personnelles :
Sexe : Femme
Localisation : Tunisie

Informations professionnelles :
Activité : Étudiant
Secteur : High Tech - Multimédia et Internet

Informations forums :
Inscription : juillet 2011
Messages : 114
Points : 49
Points : 49
Envoyer un message via MSN à nounouuuuu201186 Envoyer un message via Skype™ à nounouuuuu201186
Voici mon code:
Code :
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
150
151
152
153
154
155
156
157
158
159
import java.io.FileOutputStream;
import java.net.URL;
import java.util.Iterator;
import java.util.List;
 
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
 
 
 
 
 
public class FlickrFeed {
	private  String FLICKR_API_KEY = "xxxxxxxxxxxxxxxx";
    private  String MOST_RECENT = "http://www.flickr.com/services/rest/?method=flickr.photos.getRecent&api_key=" + FLICKR_API_KEY;
    private  String INTERESTING = "http://www.flickr.com/services/rest/?method=flickr.interestingness.getList&api_key=" + FLICKR_API_KEY;
    private  String ENTER_TAG = "http://www.flickr.com/services/rest/?method=flickr.photos.search&api_key=" + FLICKR_API_KEY + "&tags=";
    private String url = MOST_RECENT;
 
 
    static org.jdom.Document document;
	   static Element racine;
 
    public enum SearchType
    {
    	Most_Recent, Interesting, By_Search_Word
    }
 
    public FlickrFeed()
    {
 
    }
 
 
    public boolean LoadPictures( String searchWord)
    {
SearchType searchType = SearchType.By_Search_Word;
 
    	switch (searchType)
        {
            case Most_Recent:
                this.url=MOST_RECENT;
                break;
            case Interesting:
                this.url=INTERESTING;
                break;
            case By_Search_Word:
                this.url = ENTER_TAG + searchWord;
                break;
            default :
                this.url = MOST_RECENT;
                break;
        }
 
    	//On crée une instance de SAXBuilder
	      SAXBuilder sxb = new SAXBuilder();
 
   	 try
        {
   		//On crée un nouveau document JDOM avec en argument l'URL du fichier XML
 
   		URL Url = new URL(url);
        document = sxb.build(Url.openStream());
 
	      racine = document.getRootElement().getChild("photos");
 
 
	    //select the RSS data from Flickr, and store it 
 
 
	     //affiche();
 
 
   	      afficheALL();
   	   enregistre("flickrfeed.xml");
   		 return true;
        }
   	catch (Exception ex)
    {
        return false;
    }
    }
    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 afficheALL()
	   {
 
	      //On crée une List contenant tous les noeuds "photo" 
	      List listPhotos = racine.getChildren("photo");
	      System.out.println (listPhotos.size());
 
	      //On crée un Iterator sur notre liste
	      Iterator i = listPhotos.iterator();
 
	      while(i.hasNext())
	      {
 
	         //On recrée l'Element courant à chaque tour de boucle afin de
	         //pouvoir utiliser les méthodes propres aux Element comme :
	         //selectionner un noeud fils, modifier du texte, etc...
	         Element courant = (Element)i.next();
 
 
	         //On affiche les attributs de l'element courant
 
 
 
	         String Id =	courant.getAttribute("id").toString();
        	 System.out.println("id:="+Id);
        String Owner=courant.getAttribute("owner").toString();
             System.out.println("Owner:="+Owner);
        String Title =	 courant.getAttribute("title").toString();
             System.out.println("Title:="+Title);
        String  Secret=	 courant.getAttribute("secret").toString();
             System.out.println("Secret:="+Secret);
        String  Server=	 courant.getAttribute("server").toString();
             System.out.println("Server:="+Server);
        String  Farm=	 courant.getAttribute("farm").toString();
             System.out.println("Farm:="+Farm);
 
 
 
	      }
 
 
	   }
    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){}
	   }
 
 
    public static void main(String[] args) {
    	FlickrFeed f= new FlickrFeed();
    	f.LoadPictures("sport");
 
    }
 
}
Il faut juste changer FLICKR_API_KEY par votre propre clé.
Ce n'est pas parfait ni complet mais il peut être un peu utile.
nounouuuuu201186 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 08/02/2012, 15h56   #11
Modérateur
 
Avatar de Robin56
 
Homme Nicolas
Ingénieur développement logiciels
Inscription : juin 2009
Messages : 1 715
Détails du profil
Informations personnelles :
Nom : Homme Nicolas
Localisation : France

Informations professionnelles :
Activité : Ingénieur développement logiciels

Informations forums :
Inscription : juin 2009
Messages : 1 715
Points : 4 826
Points : 4 826
Citation:
Envoyé par nounouuuuu201186 Voir le message
Ce n'est pas parfait ni complet mais il peut être un peu utile.
comme quoi il était plus intéressant de réécrire ce code surtout lorsque le code ne contient aucune spécifité au langage et n'est pas trop long.
__________________
Robin56 est actuellement connecté   Envoyer un message privé Réponse avec citation 10
Réponse Proposer ce sujet en actualité Cette discussion est résolue.
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 18h39.


 
 
 
 
Partenaires

Hébergement Web