JavaEar 专注于收集分享传播有价值的技术资料

Problem with parsing XML file for the first time

问题描述


英文原文

I'm parsing an XML file for the first time for an homework I have. I am having a problem that is probably something stupid but I can't find what is going wrong. Some pieces are in French as that's my main language.

Here's the piece of codes that I am having troubles with. So with those last lines of code, I am able to print the informations I need to send to my constructor.

public void ParseXML(File fichierXML){

        final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

        {
            try {
                final DocumentBuilder builder = factory.newDocumentBuilder();
                final Document document = builder.parse(fichierXML);
                final Element racine = document.getDocumentElement();
                final NodeList racineNoeuds = racine.getChildNodes();
                final int nbRacineNoeuds = racineNoeuds.getLength();

                // Adjusting XML file
                document.getDocumentElement().normalize();

                // Printing out the main node
                System.out.println("Racine (root) : " + racine.getNodeName());

                for (int i = 0; i<nbRacineNoeuds; i++)
                {
                    if(racineNoeuds.item(i).getNodeType() == Node.ELEMENT_NODE)
                    {
                        final Element sousSection = (Element) racineNoeuds.item(i);
                        System.out.println("Sous-section : " + sousSection.getNodeName());

                        final NodeList usines = sousSection.getElementsByTagName("usine");
                        final int nbUsinesElements = usines.getLength();

                        for(int j = 0; j<nbUsinesElements; j++) {

                            final Element usine = (Element) usines.item(j);
                            String type = usine.getAttribute("type");
                            System.out.println(usine.getAttribute("type"));
                            String StringId = usine.getAttribute("id");
                            System.out.println(usine.getAttribute("id"));
                            String StringX = usine.getAttribute("x");
                            System.out.println(usine.getAttribute("x"));
                            String StringY = usine.getAttribute("y");
                            System.out.println(usine.getAttribute("y"));

                            //int id = Integer.parseInt(StringId);
                            //int x = Integer.parseInt(StringX);
                            //int y = Integer.parseInt(StringY);

                            //AjouterBatiment(new Usine(1, ImageIO.read(new File(("src/ressources/UMP0%.png"))),
                            //        new Point(x,y), 100));
                        }
                    }
                }

            } catch (ParserConfigurationException | IOException | SAXException |  NumberFormatException e) {
                e.printStackTrace();
            }

        }

    }

Now, whenever I remove the comments from the lines where I try to assign those to a String variable, if I check the debugger, I can see that they are empty. I do not understand why because they just printed out one line before.

So then obviously I am having an error after that when I try to convert the String I get into an int because the variable is empty.

Here is the XML file :

<?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>

Here is the output of the prints :

usine-assemblage
41
160
192
entrepot
51
640
192
usine-matiere
13
544
576
usine-matiere
12
96
352
usine-moteur
31
320
352

Any help would be very appreciated as I am working on this particular problem since last night without much luck.

Thank you!


中文翻译

I'm parsing an XML file for the first time for an homework I have. I am having a problem that is probably something stupid but I can't find what is going wrong. Some pieces are in French as that's my main language.

Here's the piece of codes that I am having troubles with. So with those last lines of code, I am able to print the informations I need to send to my constructor.

public void ParseXML(File fichierXML){

        final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

        {
            try {
                final DocumentBuilder builder = factory.newDocumentBuilder();
                final Document document = builder.parse(fichierXML);
                final Element racine = document.getDocumentElement();
                final NodeList racineNoeuds = racine.getChildNodes();
                final int nbRacineNoeuds = racineNoeuds.getLength();

                // Adjusting XML file
                document.getDocumentElement().normalize();

                // Printing out the main node
                System.out.println("Racine (root) : " + racine.getNodeName());

                for (int i = 0; i<nbRacineNoeuds; i++)
                {
                    if(racineNoeuds.item(i).getNodeType() == Node.ELEMENT_NODE)
                    {
                        final Element sousSection = (Element) racineNoeuds.item(i);
                        System.out.println("Sous-section : " + sousSection.getNodeName());

                        final NodeList usines = sousSection.getElementsByTagName("usine");
                        final int nbUsinesElements = usines.getLength();

                        for(int j = 0; j<nbUsinesElements; j++) {

                            final Element usine = (Element) usines.item(j);
                            String type = usine.getAttribute("type");
                            System.out.println(usine.getAttribute("type"));
                            String StringId = usine.getAttribute("id");
                            System.out.println(usine.getAttribute("id"));
                            String StringX = usine.getAttribute("x");
                            System.out.println(usine.getAttribute("x"));
                            String StringY = usine.getAttribute("y");
                            System.out.println(usine.getAttribute("y"));

                            //int id = Integer.parseInt(StringId);
                            //int x = Integer.parseInt(StringX);
                            //int y = Integer.parseInt(StringY);

                            //AjouterBatiment(new Usine(1, ImageIO.read(new File(("src/ressources/UMP0%.png"))),
                            //        new Point(x,y), 100));
                        }
                    }
                }

            } catch (ParserConfigurationException | IOException | SAXException |  NumberFormatException e) {
                e.printStackTrace();
            }

        }

    }

Now, whenever I remove the comments from the lines where I try to assign those to a String variable, if I check the debugger, I can see that they are empty. I do not understand why because they just printed out one line before.

So then obviously I am having an error after that when I try to convert the String I get into an int because the variable is empty.

Here is the XML file :

<?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>

Here is the output of the prints :

usine-assemblage
41
160
192
entrepot
51
640
192
usine-matiere
13
544
576
usine-matiere
12
96
352
usine-moteur
31
320
352

Any help would be very appreciated as I am working on this particular problem since last night without much luck.

Thank you!

I'm parsing an XML file for the first time for an homework I have. I am having a problem that is probably something stupid but I can't find what is going wrong. Some pieces are in French as that's my main language.

Here's the piece of codes that I am having troubles with. So with those last lines of code, I am able to print the informations I need to send to my constructor.

public void ParseXML(File fichierXML){

        final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

        {
            try {
                final DocumentBuilder builder = factory.newDocumentBuilder();
                final Document document = builder.parse(fichierXML);
                final Element racine = document.getDocumentElement();
                final NodeList racineNoeuds = racine.getChildNodes();
                final int nbRacineNoeuds = racineNoeuds.getLength();

                // Adjusting XML file
                document.getDocumentElement().normalize();

                // Printing out the main node
                System.out.println("Racine (root) : " + racine.getNodeName());

                for (int i = 0; i<nbRacineNoeuds; i++)
                {
                    if(racineNoeuds.item(i).getNodeType() == Node.ELEMENT_NODE)
                    {
                        final Element sousSection = (Element) racineNoeuds.item(i);
                        System.out.println("Sous-section : " + sousSection.getNodeName());

                        final NodeList usines = sousSection.getElementsByTagName("usine");
                        final int nbUsinesElements = usines.getLength();

                        for(int j = 0; j<nbUsinesElements; j++) {

                            final Element usine = (Element) usines.item(j);
                            String type = usine.getAttribute("type");
                            System.out.println(usine.getAttribute("type"));
                            String StringId = usine.getAttribute("id");
                            System.out.println(usine.getAttribute("id"));
                            String StringX = usine.getAttribute("x");
                            System.out.println(usine.getAttribute("x"));
                            String StringY = usine.getAttribute("y");
                            System.out.println(usine.getAttribute("y"));

                            //int id = Integer.parseInt(StringId);
                            //int x = Integer.parseInt(StringX);
                            //int y = Integer.parseInt(StringY);

                            //AjouterBatiment(new Usine(1, ImageIO.read(new File(("src/ressources/UMP0%.png"))),
                            //        new Point(x,y), 100));
                        }
                    }
                }

            } catch (ParserConfigurationException | IOException | SAXException |  NumberFormatException e) {
                e.printStackTrace();
            }

        }

    }

Now, whenever I remove the comments from the lines where I try to assign those to a String variable, if I check the debugger, I can see that they are empty. I do not understand why because they just printed out one line before.

So then obviously I am having an error after that when I try to convert the String I get into an int because the variable is empty.

Here is the XML file :

<?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>

Here is the output of the prints :

usine-assemblage
41
160
192
entrepot
51
640
192
usine-matiere
13
544
576
usine-matiere
12
96
352
usine-moteur
31
320
352

Any help would be very appreciated as I am working on this particular problem since last night without much luck.

Thank you!

I'm parsing an XML file for the first time for an homework I have. I am having a problem that is probably something stupid but I can't find what is going wrong. Some pieces are in French as that's my main language.

Here's the piece of codes that I am having troubles with. So with those last lines of code, I am able to print the informations I need to send to my constructor.

public void ParseXML(File fichierXML){

        final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

        {
            try {
                final DocumentBuilder builder = factory.newDocumentBuilder();
                final Document document = builder.parse(fichierXML);
                final Element racine = document.getDocumentElement();
                final NodeList racineNoeuds = racine.getChildNodes();
                final int nbRacineNoeuds = racineNoeuds.getLength();

                // Adjusting XML file
                document.getDocumentElement().normalize();

                // Printing out the main node
                System.out.println("Racine (root) : " + racine.getNodeName());

                for (int i = 0; i<nbRacineNoeuds; i++)
                {
                    if(racineNoeuds.item(i).getNodeType() == Node.ELEMENT_NODE)
                    {
                        final Element sousSection = (Element) racineNoeuds.item(i);
                        System.out.println("Sous-section : " + sousSection.getNodeName());

                        final NodeList usines = sousSection.getElementsByTagName("usine");
                        final int nbUsinesElements = usines.getLength();

                        for(int j = 0; j<nbUsinesElements; j++) {

                            final Element usine = (Element) usines.item(j);
                            String type = usine.getAttribute("type");
                            System.out.println(usine.getAttribute("type"));
                            String StringId = usine.getAttribute("id");
                            System.out.println(usine.getAttribute("id"));
                            String StringX = usine.getAttribute("x");
                            System.out.println(usine.getAttribute("x"));
                            String StringY = usine.getAttribute("y");
                            System.out.println(usine.getAttribute("y"));

                            //int id = Integer.parseInt(StringId);
                            //int x = Integer.parseInt(StringX);
                            //int y = Integer.parseInt(StringY);

                            //AjouterBatiment(new Usine(1, ImageIO.read(new File(("src/ressources/UMP0%.png"))),
                            //        new Point(x,y), 100));
                        }
                    }
                }

            } catch (ParserConfigurationException | IOException | SAXException |  NumberFormatException e) {
                e.printStackTrace();
            }

        }

    }

Now, whenever I remove the comments from the lines where I try to assign those to a String variable, if I check the debugger, I can see that they are empty. I do not understand why because they just printed out one line before.

So then obviously I am having an error after that when I try to convert the String I get into an int because the variable is empty.

Here is the XML file :

<?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>

Here is the output of the prints :

usine-assemblage
41
160
192
entrepot
51
640
192
usine-matiere
13
544
576
usine-matiere
12
96
352
usine-moteur
31
320
352

Any help would be very appreciated as I am working on this particular problem since last night without much luck.

Thank you!

1个回答

    最佳答案

  1. 英文原文

    Your "usine" element appears in two sections - metadonnees and simulation. Full output looks like this:

    Racine : configuration
    Sous-section : metadonnees
    usine-matiere
    
    
    
    usine-aile
    
    
    
    usine-moteur
    
    
    
    usine-assemblage
    
    
    
    entrepot
    
    
    
    Sous-section : simulation
    usine-matiere
    11
    32
    32
    usine-aile
    21
    320
    32
    usine-assemblage
    41
    160
    192
    entrepot
    51
    640
    192
    usine-matiere
    13
    544
    576
    usine-matiere
    12
    96
    352
    usine-moteur
    31
    320
    352
    

    When the debugger stops your code at a breakpoint, it stops every time a given line is "hit". First 5 hits include elements from "metadonnes" as shown in the output - so there is no problem here, as elements from "metadonnes" do not contain x, y and id attributes. You need to skip the first 5 stops to get the data you want in the debugger.

    What you need to do to ignore those "empty" entries - just ignore everything from "metadonnes" node. One of the approaches would be to only enter the loop that retrieves x, y and id attributes when "simulation" section is being parsed.

    for(int j = 0; "simulation".equals(sousSection.getNodeName()) && j<nbUsinesElements; j++) {
    

    This modification will allow you to skip anything that is not in "simulation" node


    中文翻译

    Your "usine" element appears in two sections - metadonnees and simulation. Full output looks like this:

    Racine : configuration
    Sous-section : metadonnees
    usine-matiere
    
    
    
    usine-aile
    
    
    
    usine-moteur
    
    
    
    usine-assemblage
    
    
    
    entrepot
    
    
    
    Sous-section : simulation
    usine-matiere
    11
    32
    32
    usine-aile
    21
    320
    32
    usine-assemblage
    41
    160
    192
    entrepot
    51
    640
    192
    usine-matiere
    13
    544
    576
    usine-matiere
    12
    96
    352
    usine-moteur
    31
    320
    352
    

    When the debugger stops your code at a breakpoint, it stops every time a given line is "hit". First 5 hits include elements from "metadonnes" as shown in the output - so there is no problem here, as elements from "metadonnes" do not contain x, y and id attributes. You need to skip the first 5 stops to get the data you want in the debugger.

    What you need to do to ignore those "empty" entries - just ignore everything from "metadonnes" node. One of the approaches would be to only enter the loop that retrieves x, y and id attributes when "simulation" section is being parsed.

    for(int j = 0; "simulation".equals(sousSection.getNodeName()) && j<nbUsinesElements; j++) {
    

    This modification will allow you to skip anything that is not in "simulation" node