【问题标题】:Converting CSV File to Hierarchy XML in JAVA with JAXB使用 JAXB 在 JAVA 中将 CSV 文件转换为层次结构 XML
【发布时间】:2020-08-07 02:45:15
【问题描述】:

我需要使用 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 文件而不是手动读取它?

【问题讨论】:

  • 我看到了上面提到的链接,我试过了,但没有给我结果
  • 提到的链接是关于漂亮的打印,但我的问题不是漂亮的打印!

标签: java xml csv jaxb


【解决方案1】:

尝试以下解决方案,

首先读取 csv 文件并从各个行中收集对象(人)。然后将对象列表编组为 xml 文件

People.java(xml 文件的根元素)

@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;
    }
}

使用 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();
        }

    }
}

输入CSV.csv

id,firstname,Lastname
1,yong,mook kim
2, Alez, Aunritod

输出.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>

【讨论】:

  • 如果我有一些其他的类,里面的人,名字叫PersonDetail,而这个类还有其他三个元素,我该怎么办?你能帮我解决这个问题吗?
  • 如果你想添加&lt;persondetail&gt;标签作为&lt;person&gt;标签的子标签并且以上三个元素(idfirstNamelastName)包含在&lt;persondetail&gt;下标签。像这样,&lt;person&gt;&lt;persondetail&gt; &lt;id&gt;2&lt;/id&gt; &lt;firstName&gt; Alez&lt;/firstName&gt; &lt;lastName&gt; Aunritod&lt;/lastName&gt; &lt;/persondetail&gt;&lt;/person&gt;
猜你喜欢
  • 2020-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-23
  • 2014-02-20
  • 2016-07-21
  • 1970-01-01
  • 2011-03-18
相关资源
最近更新 更多