Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Problem with parsing XML file for the first time

  1. #1
    Junior Member
    Join Date
    Sep 2019
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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();
     
                    // 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!
    Last edited by Vince_Magik; September 22nd, 2019 at 02:35 PM. Reason: Added XML file

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem with parsing XML file for the first time

    Can you post all the code needed to compile and execute it? There needs to be import statements and a class definition and a main method that starts the execution of the method.

    I am having an error
    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2019
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with parsing XML file for the first time

    I just added the full class. The error I am getting is a java.lang.NumberFormatException: For input string: "" because the variable StringId is empty and I try to convert it from String to int. So I know why I am getting the error (the variable is empty) but I do not know why it is empty...

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem with parsing XML file for the first time

    The code does not compile because of missing classes.
    Please remove all classes that are not needed for testing. The code for testing should have the minimum needed to compile, execute and show the problem.
    Also there is no main method.

    I am getting is a java.lang.NumberFormatException: For input string: ""
    What source line is that on? Why do you think the String variable should have a numeric value?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Problem trying to add a new file each time, without overwriting the previous.
    By ArcherGilly in forum File I/O & Other I/O Streams
    Replies: 8
    Last Post: February 23rd, 2014, 12:44 PM
  2. xml file parsing using xsd file in Java
    By gowri in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 11th, 2013, 01:13 PM
  3. Parsing CSV file
    By java42069 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 5th, 2013, 10:12 PM
  4. Replies: 0
    Last Post: January 28th, 2013, 05:29 AM
  5. RDF File parsing
    By informtopradip in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: December 20th, 2011, 07:05 AM