попробуйте следующее решение, этот пример связан с вашим предыдущим вопросом. Во-первых, вы должны разделить файл csv на несколько файлов csv, включая ожидаемое количество строк. затем подготовьте отдельно подготовленный файл scv и создайте xml файлы
People. java
@XmlRootElement(name="people")
@XmlAccessorType (XmlAccessType.FIELD)
public class People {
@XmlElement(name="person")
private List<Person> listPerson;
public List<Person> getListPerson() {
return listPerson;
}
public void setListPerson(List<Person> listPerson) {
this.listPerson = listPerson;
}
}
Person. java
@XmlRootElement(name="person")
@XmlAccessorType (XmlAccessType.FIELD)
public class Person {
private String id;
private String firstName;
private String lastName;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
разделенный файл csv в несколько файлов csv и создайте файл xml отдельно,
public class Marshaller {
int rowCount;
int fileCount = 1;
PrintWriter writer;
int lineCount;
int index;
String line;
ArrayList<Person> list;
public static void main(String[] args) {
Marshaller marshaller = new Marshaller();
marshaller.readCSV(); //split csv file into multiple csv files
marshaller.readMultipleCSV(); //create XML files using multiple csv files
}
public void readCSV(){
try {
BufferedReader buffeReader = new BufferedReader(new FileReader("inputCSV.csv"));
while ((line = buffeReader.readLine()) != null) { //get every single line individually in csv file
rowCount++;
if(rowCount == 1){
openNewFile();
}
String[] value = line.split(","); //collect the comma separated values into array
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(value[0]+','+value[1]+','+value[2]+'\n'); //append the array values to stringBuilder with comma separation
writer.write(stringBuilder.toString()); //write individual data line into csv file
if(rowCount == 100){
fileCount++;
closeWriter();
}
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void openNewFile(){
try {
writer = new PrintWriter(new File("your_multiple_csv_file_output_path/"+fileCount+".csv")); //create a new csv file
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void closeWriter(){
writer.close(); //close a csv file
rowCount = 0;
}
public void readMultipleCSV(){
ArrayList<File> xmlFileList = new ArrayList();
System.out.println(xmlFileList.size());
xmlFileList.addAll(Arrays.asList(new File("your_multiple_csv_file_output_path").listFiles())); //add all generated csv files into array list
for (int i = 0; i < xmlFileList.size(); i++) {
People people = new People();
ArrayList<Person> list = new ArrayList();
try {
BufferedReader br = new BufferedReader(new FileReader(xmlFileList.get(i).toString())); //get csv file separately
while ((line = br.readLine()) != null) { //get every single line individually in csv file
String[] value = line.split(","); //collect the comma separated values into array
Person person = new Person();
person.setId(value[0]); //first element of an array is id
person.setFirstName(value[1]); //second element of an array is firstName
person.setLastName(value[2]); //third element of an array is lastName
list.add(person); //add person object into the list
}
} catch (IOException e) {
e.printStackTrace();
}
people.setListPerson(list);
prepareXML(people, i);
}
}
public void prepareXML(People people, int csvFileNo){
//marshaling with java
try {
JAXBContext jaxbContext = JAXBContext.newInstance(People.class);
javax.xml.bind.Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(people, new File("your_xml_file_output_path/output "+(csvFileNo+1)+".xml"));
jaxbMarshaller.marshal(people, System.out);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
inputCSV.csv (без имен столбцов)
1,yong,mook kim
2, Alez, Aunritod
3,yong,mook kim
.
.
.