Results 1 to 6 of 6
  1. #1
    mond14 is offline Member
    Join Date
    May 2013
    Posts
    6
    Rep Power
    0

    Default java jdom, find doubling element

    Hello, I hope you will very well,

    Please I have xml file and I use jdom to parse it, I want to know if this file it exsite of doubling id.

    here is a code but I can not manage to do so,


    Java Code:
    static boolean doubling() {
    
    
            List listEtudiant = root.getChildren("user");
    
            Iterator i = listEtudiant.iterator();
    
            boolean bool = false;
    
        
            while (i.hasNext()) {
                Element courant = (Element) i.next();
    
              
                if (courant.getChild("id").getTextTrim().equals(?????????)) {
    
                    bool = true;
    
                }
    
            }
            return bool;
        }
    and here is the xml file:

    Java Code:
    <resplist>
    <user>
    
            <id>1</id> 
            <nom>xxxx</nom>
            <prenom>yyyyy</prenom>
    </user>
    <user>
    
            <id>3</id> 
            <nom>xxxx</nom>
            <prenom>yyyyy</prenom>
    </user>
    <user>
    
            <id>3</id> 
            <nom>xxxx</nom>
            <prenom>yyyyy</prenom>
    </user>
    </resplist>
    thank you for your help

  2. #2
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,874
    Rep Power
    5

    Default Re: java jdom, find doubling element

    You could collect all the IDs in the XML and store them somewhere (like say in a List or a Map) and then check if an ID you fetch from a node is already in your collection.

    NOTE: you can of course do both at the same time.

    - is ID in collection? if yes: error. If no, add ID to collection.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    mond14 is offline Member
    Join Date
    May 2013
    Posts
    6
    Rep Power
    0

    Default Re: java jdom, find doubling element

    thank you for your reply, I understand what you mean, but I am a beginner in java,
    I don't know how to introduce the collections in the code, if you can help me more than that

    thank you very much

  4. #4
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,874
    Rep Power
    5

    Default Re: java jdom, find doubling element

    I find it -highly- strange that you are doing something as complicated as XML parsing and yet you haven't learned something as basic as collection classes yet. Is this a school exercise?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    mond14 is offline Member
    Join Date
    May 2013
    Posts
    6
    Rep Power
    0

    Default Re: java jdom, find doubling element

    no it's not a school exercise, I have already worked with ArrayList an List, but I don't know how to use it in this code

  6. #6
    mond14 is offline Member
    Join Date
    May 2013
    Posts
    6
    Rep Power
    0

    Default Re: java jdom, find doubling element

    thank you I found the solution, it is very easy, I believed that I should do something else,

    here is the solution:

    Java Code:
    static boolean doubling() {
    
    
            List listComand = root.getChildren("cmd");
    
            Iterator i = listComand.iterator();
    
            boolean bool = false;
    
       ArrayList<String> ids = new ArrayList<String>();  
            while (i.hasNext()) {
                Element courant = (Element) i.next();
    
    
              
                if (ids.contains(courant.getChild("id").getTextTrim())) {
    
                    bool = true;
    
                }
                
                else{ 
                    ids.add(courant.getChild("id").getTextTrim());
              
               
                }
    
            }
            return bool;
        }

    thank you very much

Similar Threads

  1. Replies: 1
    Last Post: 10-12-2011, 11:22 AM
  2. Replies: 0
    Last Post: 08-21-2011, 07:30 AM
  3. JDOM how to insert Element:HELP!!!
    By dottbin in forum XML
    Replies: 1
    Last Post: 04-14-2011, 03:53 PM
  4. Cannot find the declaration of element 'web-app'
    By khewuwei in forum Java Servlet
    Replies: 1
    Last Post: 08-21-2009, 10:15 AM
  5. JDOM in java applications
    By boy22 in forum Advanced Java
    Replies: 1
    Last Post: 08-02-2007, 05:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •