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 :

xml avec java


Sujet :

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

Mode arborescent

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2018
    Messages
    2
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : France, Indre et Loire (Centre)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mai 2018
    Messages : 2
    Par défaut xml avec java
    Bonjour
    j'ai un fichier xml qui est le suivant
    Code XML : 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
    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
    	<metadonnees>
    		<usine type="usine-matiere">
    			<icones>
    				<icone type="vide" path="src/ressources/UMP0%.png"/>
    				<icone type="un-tiers" path="src/ressources/UMP33%.png"/>
    				<icone type="deux-tiers" path="src/ressources/UMP66%.png"/>
    				<icone type="plein" path="src/ressources/UMP100%.png"/>
    			</icones>
    			<sortie type = "metal"/>
    			<interval-production>100</interval-production>
    		</usine>
    		<usine type="usine-aile">
    			<icones>
    				<icone type="vide" path="src/ressources/UT0%.png"/>
    				<icone type="un-tiers" path="src/ressources/UT33%.png"/>
    				<icone type="deux-tiers" path="src/ressources/UT66%.png"/>
    				<icone type="plein" path="src/ressources/UT100%.png"/>
    			</icones>
    			<entree type="metal" quantite="2"/>
    			<sortie type="aile"/>
    			<interval-production>50</interval-production>
    		</usine>
    		<usine type="usine-moteur">
    			<icones>			
    				<icone type="vide" path="src/ressources/UM0%.png"/>
    				<icone type="un-tiers" path="src/ressources/UM33%.png"/>
    				<icone type="deux-tiers" path="src/ressources/UM66%.png"/>
    				<icone type="plein" path="src/ressources/UM100%.png"/>
    			</icones>
    			<entree type="metal" quantite="4"/>
    			<sortie type="moteur"/>
    			<interval-production>75</interval-production>
    		</usine>
    		<usine type="usine-assemblage">
    			<icones>
    				<icone type="vide" path="src/ressources/UA0%.png"/>
    				<icone type="un-tiers" path="src/ressources/UA33%.png"/>
    				<icone type="deux-tiers" path="src/ressources/UA66%.png"/>
    				<icone type="plein" path="src/ressources/UA100%.png"/>
    			</icones>
    			<entree type="aile" quantite="2"/>
    			<entree type="moteur" quantite="4"/>
    			<sortie type="avion"/>
    			<interval-production>110</interval-production>
    		</usine>
    		<usine type="entrepot">
    			<icones>
    				<icone type="vide" path="src/ressources/E0%.png"/>
    				<icone type="un-tiers" path="src/ressources/E33%.png"/>
    				<icone type="deux-tiers" path="src/ressources/E66%.png"/>
    				<icone type="plein" path="src/ressources/E100%.png"/>
    			</icones>
    			<entree type="avion" capacite="5"/>
    		</usine>
    	</metadonnees>
     
    	<simulation>
    		<usine type="usine-matiere" id="11" x="32" y="32"/>
    		<usine type="usine-aile" id="21" x="320" y="32"/>
    		<usine type="usine-assemblage" id="41" x="160" y="192"/> 
    		<usine type="entrepot" id="51" x="640" y="192"/>
    		<usine type="usine-matiere" id="13" x="544" y="576"/>
    		<usine type="usine-matiere" id="12" x="96" y="352"/>
    		<usine type="usine-moteur" id="31" x="320" y="352"/>
    		<chemins>
    			<chemin de="11" vers="21" />
    			<chemin de="21" vers="41" />
    			<chemin de="41" vers="51" />
    			<chemin de="12" vers="31" />
    			<chemin de="13" vers="31" />
    			<chemin de="31" vers="41" />
    		</chemins>
    	</simulation>
     
    </configuration>


    je veux le lire je cree une classe usineXml qui represente un objet usine

    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
    public class UsineXml {
     
     
    	private String type;
    	private String typeIcone;
    	private String pathIcone;
    	private String typeEntree;
    	private String typeSortie;
    	private int intervalProuction;
    	public String getType() {
    		return type;
    	}
    	public void setType(String type) {
    		this.type = type;
    	}
    	public String getTypeIcone() {
    		return typeIcone;
    	}
    	public void setTypeIcone(String typeIcone) {
    		this.typeIcone = typeIcone;
    	}
    	public String getPathIcone() {
    		return pathIcone;
    	}
    	public void setPathIcone(String pathIcone) {
    		this.pathIcone = pathIcone;
    	}
    	public String getTypeEntree() {
    		return typeEntree;
    	}
    	public void setTypeEntree(String typeEntree) {
    		this.typeEntree = typeEntree;
    	}
    	public String getTypeSortie() {
    		return typeSortie;
    	}
    	public void setTypeSortie(String typeSortie) {
    		this.typeSortie = typeSortie;
    	}
    	public int getIntervalProuction() {
    		return intervalProuction;
    	}
    	public void setIntervalProuction(int intervalProuction) {
    		this.intervalProuction = intervalProuction;
    	}
     
    	 @Override
    	    public String toString() {
    	        return "usine:: Name=" + this.type ;
    	    }
     
    }
    ensuite j'attaque la lecture

    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
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
     
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
     
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import org.xml.sax.SAXException;
     
     
    public class XmlReaderDom {
     
        public static void main(String[] args) {
            String filePath = "configuration.xml";
            File xmlFile = new File(filePath);
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder;
            try {
                dBuilder = dbFactory.newDocumentBuilder();
                Document doc = dBuilder.parse(xmlFile);
                doc.getDocumentElement().normalize();
                System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
                NodeList nodeList = doc.getElementsByTagName("metadonnees");
                //now XML is loaded as Document in memory, lets convert it to Object List
                List<UsineXml> empList = new ArrayList<UsineXml>();
                for (int i = 0; i < nodeList.getLength(); i++) {
                    empList.add(getUsine(nodeList.item(i)));
                }
                //lets print Employee list information
                for (UsineXml emp : empList) {
                    System.out.println(emp.toString());
                }
            } catch (SAXException | ParserConfigurationException | IOException e1) {
                e1.printStackTrace();
            }
     
        }
     
     
        private static UsineXml getUsine(Node node) {
            //XMLReaderDOM domReader = new XMLReaderDOM();
            UsineXml uneUsine = new UsineXml();
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                Element element = (Element) node;
                uneUsine.setType(getTagValue("type", element));
     
            }
     
            return uneUsine;
        }
     
     
        private static String getTagValue(String tag, Element element) {
            NodeList nodeList = element.getElementsByTagName(tag).item(0).getChildNodes();
            Node node = (Node) nodeList.item(0);
            return node.getNodeValue();
        }
     
    }
    mais des erreurs apparaissent
    pouvez vous m'aider pour corriger ce code
    le projet est en piece jointe

    merci
    Fichiers attachés Fichiers attachés

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

Discussions similaires

  1. affichage de fichier XML avec java
    Par smisoufafa dans le forum Format d'échange (XML, JSON...)
    Réponses: 1
    Dernier message: 01/04/2008, 11h57
  2. Modification entête XML avec Java pour RSS
    Par Hyst76 dans le forum Format d'échange (XML, JSON...)
    Réponses: 2
    Dernier message: 27/09/2007, 16h58
  3. parser du xml avec java
    Par 080983 dans le forum Format d'échange (XML, JSON...)
    Réponses: 5
    Dernier message: 30/07/2007, 09h56
  4. Java et XML : Traitement d'un fichier XML avec JAVA
    Par nice dans le forum XML/XSL et SOAP
    Réponses: 3
    Dernier message: 18/05/2006, 21h24
  5. [XML] Comment parser ce fichier XML avec java !
    Par jlassira dans le forum Format d'échange (XML, JSON...)
    Réponses: 7
    Dernier message: 13/03/2006, 12h56

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