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(); // Ajustement du fichier XML document.getDocumentElement().normalize(); // Affichage du noeud principal System.out.println("Racine : " + 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 : https://pastebin.com/GyxZ42a5
Here is the full class : https://pastebin.com/sXGAD85s
Any help would be very appreciated as I am working on this particular problem since last night without much luck.
Thank you!