Home > Blockchain >  how to convert XML to Collection and iterate based on the indexing? as I need to further perform ope
how to convert XML to Collection and iterate based on the indexing? as I need to further perform ope

Time:07-05

I need to filter and aggregate the nodes having same value in a xml file using the java code.

In the below code, i need to aggregate and write the details of students having same gender in different files.

enter code here

<?xml version="1.0"?>

<student> 
   <name>aaa</name>  
   <gender>M<gender> 
   <Address>
     <City>Auckland</City>
     <Zipcode>2310`enter code here`</Zipcode>
    </Address>
<student/> 
<student>  
  <name>bbb</name> 
  <gender>f</gender> 
  <Address>
     <City>Wellington</City>
     <Zipcode>2310</Zipcode>
    </Address>

</student> 
<student>  
  <name>ccc</name>  
  <gender>f</gender>
  <Address>
     <City>NorthIsland</City>
     <Zipcode>5671</Zipcode>
    </Address>
</student>

This is the current parser code. I am unable to print distinct values. I tried using hashset but no luck.

    public static void main(String[] args) {
     
         try {
              List<Student> student = new ArrayList<Student>();

            File file = new File("/Downloads/student.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(Company.class);
     
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            Student com= (Student) jaxbUnmarshaller.unmarshal(file);
            
        //  ArrayList<Student> list=(ArrayList<Student>) com.getStudent();
            List<Student> list = com.getStudent();
            for(Student stu:list) {
    
            student.add(new Student(stu.getFirstName() " " stu.getLastName() " " stu.getContactNo() " " stu.getGender() " " stu.getGender());
            }


        List<Employee> newList = list.stream().distinct().collect(Collectors.toList());

Output

John Green 1234567890 m

Need to check for the distinct records. This is not printing the distinct values.

List<Student> newList = list.stream().distinct().collect(Collectors.toList());

CodePudding user response:

Well, as per comments & question it looks like you need to do below:

public static void main(String[] args) {
           
     try {
 
        File file = new File("/Downloads/student.xml");
        JAXBContext jaxbContext = JAXBContext.newInstance(Company.class);
 
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        Student com= (Student) jaxbUnmarshaller.unmarshal(file);
        List<Student> list = com.getStudent();
        list.stream().forEach(stu->{
         if(stu.getGender().equalsIgnoreCase("M")){
          //write to file where Male gender details can be stored.
           
          }
         else {
          //write to file where Male gender details can be stored.
         }
         });
        
        }
     }}

Here, you can obtain stream & compare each element & write to your respective files element by element.

Please note, you can declare your files at beginning of main.

Edit: As you are looking for multilevel grouping then below is what I am suggesting:

Map<String, Map<String, List<Student>>> map = list.stream()
            .collect(Collectors.groupingBy(x -> x.getGender(), Collectors.groupingBy(y -> y.getAddress().getZipCode())));

This map has aggregation of data based on gender and zip. Iterate this map & put details into different files.

  • Related