#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    1
    Rep Power
    0

    Problem with sorting and comparing Java from XML


    Hi! I just found this forum and though maybe you guys can help me out with a java problem I have.

    So, i was given XML and schema files. My goal was to output all data from XML (without duplicates) and order this list by the date of birth. Currently i got all data printed out(with duplicates) and i don't know what to do next. I've tried different things, but unsuccessfully. If you guys could give me some code example or give some tips, i would really appreciate that!

    Here's my XML code:

    Code:
    <?xml version="1.0"?>
    <patsiendid xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="schema.xsd">
        <isik>
            <firstname>Peeter</firstname>
            <surname>Esimene</surname>
            <dateofbirth>1981-01-11</dateofbirth>
            <sex>M</sex>
            <isa>
                <firstname>Aleksander</firstname>
                <surname>Teine</surname>
                <dateofbirth>1952-02-21</dateofbirth>
                <sex>M</sex>
                <isa>
                    <firstname>Louis</firstname>
                    <surname>Kolmas</surname>
                    <dateofbirth>1923-03-21</dateofbirth>
                    <sex>M</sex>
                </isa>
                <ema>
                    <firstname>Marie</firstname>
                    <surname>Kolmas</surname>
                    <dateofbirth>1923-03-22</dateofbirth>
                    <sex>F</sex>
                </ema>
            </isa>
            <ema>
                <firstname>Linda</firstname>
                <surname>Teine</surname>
                <dateofbirth>1952-02-22</dateofbirth>
                <sex>F</sex>
                <isa>
                    <firstname>Kalev</firstname>
                    <surname>Neljas</surname>
                    <dateofbirth>1923-03-23</dateofbirth>
                    <sex>M</sex>
                </isa>
                <ema>
                    <firstname>Mari</firstname>
                    <surname>Neljas</surname>
                    <dateofbirth>1923-03-24</dateofbirth>
                    <sex>F</sex>
                </ema>
    		</ema>   		
    		<oedvennad>
                <isik>
                    <firstname>Beatrix</firstname>
                    <surname>Esimene</surname>
                    <dateofbirth>1982-02-12</dateofbirth>
                    <sex>F</sex>
    		        <isa>
    		            <firstname>Aleksander</firstname>
    		            <surname>Teine</surname>
    		            <dateofbirth>1952-02-21</dateofbirth>
    		            <sex>M</sex>
    		            <isa>
    		                <firstname>Louis</firstname>
    		                <surname>Kolmas</surname>
    		                <dateofbirth>1923-03-21</dateofbirth>
    		                <sex>M</sex>
    		            </isa>
    		            <ema>
    		                <firstname>Marie</firstname>
    		                <surname>Kolmas</surname>
    		                <dateofbirth>1923-03-22</dateofbirth>
    		                <sex>F</sex>
    		            </ema>
    		        </isa>
    		        <ema>
    		            <firstname>Linda</firstname>
    		            <surname>Teine</surname>
    		            <dateofbirth>1952-02-22</dateofbirth>
    		            <sex>F</sex>
    		            <isa>
    		                <firstname>Kalev</firstname>
    		                <surname>Neljas</surname>
    		                <dateofbirth>1923-03-23</dateofbirth>
    		                <sex>M</sex>
    		            </isa>
    		            <ema>
    		                <firstname>Mari</firstname>
    		                <surname>Neljas</surname>
    		                <dateofbirth>1923-03-24</dateofbirth>
    		                <sex>F</sex>
    		            </ema>
    				</ema>
                </isik>
                <isik>
                    <firstname>Felix</firstname>
                    <surname>Esimene</surname>
                    <dateofbirth>1983-03-13</dateofbirth>
                    <sex>M</sex>
    		        <isa>
    		            <firstname>Aleksander</firstname>
    		            <surname>Teine</surname>
    		            <dateofbirth>1952-02-21</dateofbirth>
    		            <sex>M</sex>
    		            <isa>
    		                <firstname>Louis</firstname>
    		                <surname>Kolmas</surname>
    		                <dateofbirth>1923-03-21</dateofbirth>
    		                <sex>M</sex>
    		            </isa>
    		            <ema>
    		                <firstname>Marie</firstname>
    		                <surname>Kolmas</surname>
    		                <dateofbirth>1923-03-22</dateofbirth>
    		                <sex>F</sex>
    		            </ema>
    		        </isa>
    		        <ema>
    		            <firstname>Linda</firstname>
    		            <surname>Teine</surname>
    		            <dateofbirth>1952-02-22</dateofbirth>
    		            <sex>F</sex>
    		            <isa>
    		                <firstname>Kalev</firstname>
    		                <surname>Neljas</surname>
    		                <dateofbirth>1923-03-23</dateofbirth>
    		                <sex>M</sex>
    		            </isa>
    		            <ema>
    		                <firstname>Mari</firstname>
    		                <surname>Neljas</surname>
    		                <dateofbirth>1923-03-24</dateofbirth>
    		                <sex>F</sex>
    		            </ema>
    				</ema>
                </isik>
            </oedvennad>
        </isik>
    </patsiendid>
    And my Java:
    Code:
    import java.io.File;
    import java.util.ArrayList;
    import java.util.HashSet;
    import java.util.List;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    
    public class harjutus {
    	public static void main(String args[]){
    		try {
    			
    			File harjutus = new File("file.xml");
    			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    			Document doc = dBuilder.parse(harjutus);
    
    
    			List<Node> inimesteList = new ArrayList<Node>();
    			NodeList isa = doc.getElementsByTagName("isa");
    			NodeList ema = doc.getElementsByTagName("ema");
    			NodeList isik = doc.getElementsByTagName("isik");			
    			System.out.println();
    			
    			for (int i = 0; i < isa.getLength(); i++){
    				Node yksnode = isa.item(i);
    				inimesteList.add(yksnode);
    			}
    			for (int i = 0; i < ema.getLength(); i++){
    				Node yksnode = ema.item(i);
    				inimesteList.add(yksnode);
    			}
    			for (int i = 0; i < isik.getLength(); i++){
    				Node yksnode = isik.item(i);
    				inimesteList.add(yksnode);
    			}
    						
    			System.out.println("Total: " + inimesteList.size());
    			
    			for (int i = 0; i < inimesteList.size(); i++){
    				Element node = (Element) inimesteList.get(i);
    				System.out.println(i+1 + ". " + node);
    				System.out.println("Firstname: " + node.getElementsByTagName("firstname").item(0).getTextContent());
    				System.out.println("Surname: " + node.getElementsByTagName("surname").item(0).getTextContent());
    				System.out.println("Date of Birth: " + node.getElementsByTagName("dateofbirth").item(0).getTextContent());
    				System.out.println("Sex: " + node.getElementsByTagName("sex").item(0).getTextContent());
    			}
    
    
    		} catch (Exception e) {
    			e.printStackTrace();
    	    }
    	}
    }
    For removing duplicates I tried to use HashSet and LinkedHashSet, either didn't work.
    Code:
           List<Node> inimesteList = new ArrayList<Node>();
    
    
            NodeList isa = doc.getElementsByTagName("isa");
            NodeList ema = doc.getElementsByTagName("ema");
            NodeList isik = doc.getElementsByTagName("isik");
            System.out.println();
    
            for (int i = 0; i < isa.getLength(); i++){
                Node yksnode = isa.item(i);
                inimesteList.add(yksnode);
            }
            for (int i = 0; i < ema.getLength(); i++){
                Node yksnode = ema.item(i);
                inimesteList.add(yksnode);
            }
            for (int i = 0; i < isik.getLength(); i++){
                Node yksnode = isik.item(i);
                inimesteList.add(yksnode);
            }
    
            LinkedHashSet<Node> listWithoutDuplicates =  new LinkedHashSet<Node>();
            listWithoutDuplicates.addAll(inimesteList);
            inimesteList.clear();
            inimesteList.addAll(listWithoutDuplicates);
    
    
            System.out.println("Total: " + inimesteList.size());
    If you have any thougths, what to do, let me know. Thank you!
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    Can you post what the program currently outputs and describe what is wrong with it and show what it should be?

IMN logo majestic logo threadwatch logo seochat tools logo