使用JAXB在JAVA中将CSV文件转换为层次结构XML

使用JAXB在JAVA中将CSV文件转换为层次结构XML,java,xml,csv,jaxb,Java,Xml,Csv,Jaxb,我需要使用CSV文件创建分层XML id,firstname,Lastname 1,yong,mook kim 2, Alez, Aunritod ,... 我尝试用JAXB来实现这一点,并手动创建类 public class WriteXMLFile { public static void main(String argv[]) { try { DocumentBuilderFactory docFactory = DocumentBuilderF

我需要使用CSV文件创建分层XML

id,firstname,Lastname
1,yong,mook kim
2, Alez, Aunritod
,...
我尝试用JAXB来实现这一点,并手动创建类

public class WriteXMLFile {

    public static void main(String argv[]) {

      try {

        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

        // root elements
        Document doc = docBuilder.newDocument();
        Element rootElement = doc.createElement("company");
        doc.appendChild(rootElement);

        // staff elements
        Element staff = doc.createElement("Staff");
        rootElement.appendChild(staff);

        // set attribute to staff element
        Attr attr = doc.createAttribute("id");
        attr.setValue("1");
        staff.setAttributeNode(attr);

        Element firstname = doc.createElement("firstname");
        firstname.appendChild(doc.createTextNode("yong"));
        staff.appendChild(firstname);

        // lastname elements
        Element lastname = doc.createElement("lastname");
        lastname.appendChild(doc.createTextNode("mook kim"));
        staff.appendChild(lastname);
但我的问题是,我希望从csv文件中读取每个节点的值,而不是在代码中手动读取

        Element firstname = doc.createElement("firstname");
        firstname.appendChild(doc.createTextNode("yong"));
        staff.appendChild(firstname);

有人能帮我从sample.csv文件中读取而不是手动读取吗?

尝试以下解决方案

首先读取csv文件并从各行收集对象(个人)。然后将对象列表封送到xml文件

java(xml文件的根元素)

用java进行封送处理

public class Marshaller {

    public static void main(String[] args) {

        BufferedReader br;
        String line;
        People people = new People();
        ArrayList<Person> list = new ArrayList();

        //read the csv file and collect the person objects
        try {
            br = new BufferedReader(new FileReader("inputCSV.csv"));
            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); //set person object list to people
        people.getListPerson().remove(0);   //remove the first person object of list. because it holds the column's names

        //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("output.xml"));
            jaxbMarshaller.marshal(people, System.out);

        } catch (JAXBException e) {
            e.printStackTrace();
        }

    }
}
output.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<people>
    <person>
        <id>1</id>
        <firstName>yong</firstName>
        <lastName>mook kim</lastName>
    </person>
    <person>
        <id>2</id>
        <firstName> Alez</firstName>
        <lastName> Aunritod</lastName>
    </person>
</people>

1.
勇
木金
2.
阿莱兹
阿努里托德

你可以看到,我看到了上面提到的链接,我试过了,但没有给我结果。上面提到的链接是关于漂亮的打印,但我的问题不是漂亮的打印!如果我有一个其他类,名为PersonDetail的insidePerson,这个类还有其他三个元素,我该怎么做呢?你能帮我一下吗?如果你想添加
标签作为
标签的子标签,并且在
标签下面包括三个以上的元素(
id
firstName
lastName
)。就像那样,
2阿莱兹·阿努里托德
public class Marshaller {

    public static void main(String[] args) {

        BufferedReader br;
        String line;
        People people = new People();
        ArrayList<Person> list = new ArrayList();

        //read the csv file and collect the person objects
        try {
            br = new BufferedReader(new FileReader("inputCSV.csv"));
            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); //set person object list to people
        people.getListPerson().remove(0);   //remove the first person object of list. because it holds the column's names

        //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("output.xml"));
            jaxbMarshaller.marshal(people, System.out);

        } catch (JAXBException e) {
            e.printStackTrace();
        }

    }
}
id,firstname,Lastname
1,yong,mook kim
2, Alez, Aunritod
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<people>
    <person>
        <id>1</id>
        <firstName>yong</firstName>
        <lastName>mook kim</lastName>
    </person>
    <person>
        <id>2</id>
        <firstName> Alez</firstName>
        <lastName> Aunritod</lastName>
    </person>
</people>