dotroot
dotroot

Reputation: 33

Marshalling and Unmarshalling with XML and Java

I am trying to marshall Java to XML and unmarshall that file back into Java. I am working with an ArrayList and still quite new at programming. Any guidance would be much appreciated.

Here is my save method

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

Here is my load method

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

And here is my Items class

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

}

I have tried adding item.setpcPart(save); to my save method but it can't be resolved and I have tried using save.addAll(item.getpcPart()); to my load method and it also can't be resolved. Anytime I save the output is <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <items/> and there is no output from load. Thanks

Upvotes: 1

Views: 3140

Answers (1)

mhasan
mhasan

Reputation: 3709

Add setter to your Items class like below:

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>

Upvotes: 1

Related Questions