使用XML和Java进行编组和解组

时间:2016-09-22 02:45:41

标签: java xml marshalling unmarshalling

我正在尝试将Java编组为XML并将该文件解组为Java。我正在使用ArrayList,并且在编程时仍然很新。任何指导都将非常感谢。

这是我的保存方法

public void save() throws Exception {

try {
    File file = new File("order.xml");
    Items item = new Items();
    JAXBContext c = JAXBContext.newInstance(Items.class);
    Marshaller m = c.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    m.marshal(item, file);
    m.marshal(item, System.out);
 }
    catch (JAXBException e) {
    e.printStackTrace();
    }
 }

这是我的加载方法

public void load() throws Exception {
try {
    File file = new File("order.xml");
    JAXBContext c = JAXBContext.newInstance(Items.class);
    Unmarshaller u = c.createUnmarshaller();
    Items item = (Items) u.unmarshal(file);
    u.unmarshal(file);
}
catch (JAXBException e){
    e.printStackTrace();
}
}

这是我的Items类

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.*;

@XmlRootElement

public class Items {
    private List<String> pcPart;

    @XmlElement(name = "pcPart")
    public List<String> getpcPart(){
        if(pcPart == null){
        pcPart = new ArrayList<String>();
        }
        return pcPart;
    }

}

我尝试将item.setpcPart(save);添加到我的保存方法但无法解决,我尝试将save.addAll(item.getpcPart());用于我的加载方法,但也无法解决。我保存输出的任何时候都是 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <items/>并且没有来自加载的输出。感谢

1 个答案:

答案 0 :(得分:0)

将setter添加到Items类中,如下所示:

  public void setPcPart(List<String> pcPart) {
        this.pcPart = pcPart;

}

import java.io.File;
import java.util.ArrayList;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

    public class Parser {


    public static void main(String[] args) throws Exception {
        new Parser().save();
        new Parser().load();
    }

    public void save() throws Exception {

        try {
            File file = new File("C://order.xml");
            Items item = new Items();
            item.setPcPart(new ArrayList<String>(){{add("a");add("b");add("b");}});
            JAXBContext c = JAXBContext.newInstance(Items.class);
            Marshaller m = c.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            m.marshal(item, file);
            m.marshal(item, System.out);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }

    public void load() throws Exception {
        try {
            File file = new File("C://order.xml");
            JAXBContext c = JAXBContext.newInstance(Items.class);
            Unmarshaller u = c.createUnmarshaller();
            Items item = (Items) u.unmarshal(file);
            u.unmarshal(file);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }

}

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.*;

@XmlRootElement

public class Items {
    private List<String> pcPart;

    @XmlElement(name = "pcPart")
    public List<String> getpcPart(){
        if(pcPart == null){
        pcPart = new ArrayList<String>();
        }
        return pcPart;
    }

    public void setPcPart(List<String> pcPart) {
        this.pcPart = pcPart;
    }



}

Output
--------------
<items>
    <pcPart>a</pcPart>
    <pcPart>b</pcPart>
    <pcPart>b</pcPart>
</items>