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

Java Discussion :

Appel de code C# à partir de Java


Sujet :

Java

  1. #1
    Membre confirmé Avatar de nounouuuuu201186
    Femme Profil pro
    Stagiaire
    Inscrit en
    Juillet 2011
    Messages
    169
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Tunisie

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

    Informations forums :
    Inscription : Juillet 2011
    Messages : 169
    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 : 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
    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?

  2. #2
    Modérateur

    Avatar de Robin56
    Homme Profil pro
    Architecte de système d'information
    Inscrit en
    Juin 2009
    Messages
    5 297
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : Architecte de système d'information

    Informations forums :
    Inscription : Juin 2009
    Messages : 5 297
    Par défaut
    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.
    Responsable Java de Developpez.com (Twitter et Facebook)
    Besoin d'un article/tutoriel/cours sur Java, consulter la page cours
    N'hésitez pas à consulter la FAQ Java et à poser vos questions sur les forums d'entraide Java
    --------
    Architecte Solution
    LinkedIn : https://www.linkedin.com/in/nicolascaudard/

  3. #3
    Membre confirmé Avatar de nounouuuuu201186
    Femme Profil pro
    Stagiaire
    Inscrit en
    Juillet 2011
    Messages
    169
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Tunisie

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

    Informations forums :
    Inscription : Juillet 2011
    Messages : 169
    Par défaut
    Voici le code complet:
    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
    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;
                }
            }
     
     
     
     
        }
    }

  4. #4
    Membre confirmé Avatar de nounouuuuu201186
    Femme Profil pro
    Stagiaire
    Inscrit en
    Juillet 2011
    Messages
    169
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Tunisie

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

    Informations forums :
    Inscription : Juillet 2011
    Messages : 169
    Par défaut
    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.

  5. #5
    Modérateur

    Avatar de Robin56
    Homme Profil pro
    Architecte de système d'information
    Inscrit en
    Juin 2009
    Messages
    5 297
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : Architecte de système d'information

    Informations forums :
    Inscription : Juin 2009
    Messages : 5 297
    Par défaut
    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.
    Responsable Java de Developpez.com (Twitter et Facebook)
    Besoin d'un article/tutoriel/cours sur Java, consulter la page cours
    N'hésitez pas à consulter la FAQ Java et à poser vos questions sur les forums d'entraide Java
    --------
    Architecte Solution
    LinkedIn : https://www.linkedin.com/in/nicolascaudard/

  6. #6
    Membre confirmé Avatar de nounouuuuu201186
    Femme Profil pro
    Stagiaire
    Inscrit en
    Juillet 2011
    Messages
    169
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Tunisie

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

    Informations forums :
    Inscription : Juillet 2011
    Messages : 169
    Par défaut
    Voici mon nouveau code:
    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
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     var xraw = XElement.Load(url);
                var xroot = XElement.Parse(xraw.Xml);
    par
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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?

  7. #7
    Modérateur

    Avatar de Robin56
    Homme Profil pro
    Architecte de système d'information
    Inscrit en
    Juin 2009
    Messages
    5 297
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : Architecte de système d'information

    Informations forums :
    Inscription : Juin 2009
    Messages : 5 297
    Par défaut
    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.
    Responsable Java de Developpez.com (Twitter et Facebook)
    Besoin d'un article/tutoriel/cours sur Java, consulter la page cours
    N'hésitez pas à consulter la FAQ Java et à poser vos questions sur les forums d'entraide Java
    --------
    Architecte Solution
    LinkedIn : https://www.linkedin.com/in/nicolascaudard/

  8. #8
    Membre confirmé Avatar de nounouuuuu201186
    Femme Profil pro
    Stagiaire
    Inscrit en
    Juillet 2011
    Messages
    169
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Tunisie

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

    Informations forums :
    Inscription : Juillet 2011
    Messages : 169
    Par défaut
    Voici mon nouveau code:
    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
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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?

  9. #9
    Modérateur

    Avatar de Robin56
    Homme Profil pro
    Architecte de système d'information
    Inscrit en
    Juin 2009
    Messages
    5 297
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : Architecte de système d'information

    Informations forums :
    Inscription : Juin 2009
    Messages : 5 297
    Par défaut
    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)
    Responsable Java de Developpez.com (Twitter et Facebook)
    Besoin d'un article/tutoriel/cours sur Java, consulter la page cours
    N'hésitez pas à consulter la FAQ Java et à poser vos questions sur les forums d'entraide Java
    --------
    Architecte Solution
    LinkedIn : https://www.linkedin.com/in/nicolascaudard/

  10. #10
    Membre confirmé Avatar de nounouuuuu201186
    Femme Profil pro
    Stagiaire
    Inscrit en
    Juillet 2011
    Messages
    169
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Tunisie

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

    Informations forums :
    Inscription : Juillet 2011
    Messages : 169
    Par défaut
    Voici mon code:
    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
    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.

  11. #11
    Modérateur

    Avatar de Robin56
    Homme Profil pro
    Architecte de système d'information
    Inscrit en
    Juin 2009
    Messages
    5 297
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : Architecte de système d'information

    Informations forums :
    Inscription : Juin 2009
    Messages : 5 297
    Par défaut
    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.
    Responsable Java de Developpez.com (Twitter et Facebook)
    Besoin d'un article/tutoriel/cours sur Java, consulter la page cours
    N'hésitez pas à consulter la FAQ Java et à poser vos questions sur les forums d'entraide Java
    --------
    Architecte Solution
    LinkedIn : https://www.linkedin.com/in/nicolascaudard/

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Appeler un code java à partir d'un script shell
    Par supcomingenieur dans le forum Shell et commandes GNU
    Réponses: 3
    Dernier message: 06/06/2013, 16h01
  2. [JNI] Appeler une DLL Fortran à partir de Java
    Par Graffity dans le forum Entrée/Sortie
    Réponses: 2
    Dernier message: 13/11/2008, 00h07
  3. Appeler du code C# à partir de code C
    Par popoms dans le forum C++/CLI
    Réponses: 8
    Dernier message: 29/10/2008, 14h17
  4. Unité Delphi appelée à partir de JAVA
    Par babaahmed dans le forum API standards et tierces
    Réponses: 2
    Dernier message: 26/04/2003, 10h51

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