Question:
There are several objects that need to be combined into a HashSet collection and saved in an XML file and later retrieved from there.
package laba2;
import javax.xml.bind.JAXBException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import static laba2.XMLworker.*;
public class Laba2 {
public static void main(String[]args){
FoodResidus fr1 = new FoodResidus();
fr1.name=someName1;
fr1.ID=0;
FoodResidus fr2 = new FoodResidus();
fr2.name=someName2;
fr2.ID=1;
FoodResidus fr3 = new FoodResidus();
fr3.name=someName3;
fr3.ID=2;
HashSet<FoodResidus> collection = new HashSet<>();
rubbishBin.add(fr1);
rubbishBin.add(fr2);
rubbishBin.add(fr3);
try {
saveCollection("some.xml",collection);
} catch (JAXBException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
To do this, I created a class with two corresponding methods.
package laba2;
import javax.xml.bind.*;
import javax.xml.namespace.QName;
import java.io.*;
import java.util.ArrayList;
import java.util.HashSet;
public class XMLworker {
public static void saveCollection(String path, FoodResidus[] hs)throws JAXBException,IOException {
JAXBContext context = JAXBContext.newInstance(FoodResidus[].class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
JAXBElement<FoodResidus[]> jaxbElement = new JAXBElement<FoodResidus[]>(new QName("My_XML_Class"), FoodResidus[].class, hs);
File fileWrite = new File(path);
FileWriter fw = new FileWriter(fileWrite);
BufferedWriter bw = new BufferedWriter(fw);
marshaller.marshal(jaxbElement, bw);
}
public static HashSet getCollection(String path)throws JAXBException,IOException{
File fileRead = new File(path);
FileReader fr = new FileReader(fileRead);
BufferedReader br = new BufferedReader(fr);
HashSet returnedHS = JAXB.unmarshal(br, HashSet.class);
return returnedHS;
}
}
I look at the resulting file, and there it is:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<My_XML_Class/>
I tried to save each object separately, everything comes out correctly and using the getCollection () method they are perfectly retrieved. What's wrong with collections?
Here is the FoodResidus class
package laba2;
public class FoodResidus {
public String name = "someOfFoodresidus";
public Integer ID=666;
public boolean fliesAttraction=false;
public FoodResidus(){}
}
}
Answer:
Solved People suggested that XML does not tolerate collections as root objects Used a wrapper class – ClassWrapper
package laba2;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.HashSet;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class ClassWrapper {
private HashSet<FoodResidus> theCollection;
public ClassWrapper(){
theCollection = new HashSet<FoodResidus>();
}
public HashSet<FoodResidus> getTheCollection(){
return theCollection;
}
public void setTheCollection(HashSet<FoodResidus> theCollection){
this.theCollection=theCollection;
}
}
Changed XMLworker a bit accordingly
package laba2;
import javax.xml.bind.*;
import java.io.*;
import java.util.HashSet;
/**
* Created by danil on 23.02.2017.
*/
public class XMLworker {
public static void saveCollection(String path, HashSet hs)throws JAXBException,IOException {
JAXBContext context = JAXBContext.newInstance(ClassWrapper.class);
ClassWrapper cw = new ClassWrapper();
cw.setTheCollection(hs);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
File fileWrite = new File(path);
FileWriter fw = new FileWriter(fileWrite);
BufferedWriter bw = new BufferedWriter(fw);
marshaller.marshal(cw, bw);
}
public static HashSet getCollection(String path)throws JAXBException,IOException{
File fileRead = new File(path);
FileReader fr = new FileReader(fileRead);
BufferedReader br = new BufferedReader(fr);
ClassWrapper returnedHS = JAXB.unmarshal(br, ClassWrapper.class);
return returnedHS.getTheCollection();
}
}
In the end, everything worked, I hope it will be useful to someone.