summaryrefslogtreecommitdiff
path: root/src/main/java/design/persistence/XMLParser.java
blob: 472931ab8fcf1166295dd701530e551ae6c71c05 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package design.persistence;
import java.io.File;
import java.io.IOException;

import com.fasterxml.jackson.databind.ObjectMapper;

import design.model.Golfer;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;



public class XMLParser {
    
    private final ObjectMapper jsonMapper = new ObjectMapper();
    private final XmlMapper xmlMapper = new XmlMapper();

    public File exportData() throws IOException {
        File jsonData = JSONPersonalDatabase.instance().exportData();
        Golfer[] golfers = jsonMapper.readValue(jsonData, Golfer[].class);
        File xmlFile = new File("data/data.xml");
        xmlMapper.writerWithDefaultPrettyPrinter().writeValue(xmlFile, golfers);
        return xmlFile;
    }

    public void importData(File xmlFile) throws IOException {
        Golfer[] golfers = xmlMapper.readValue(xmlFile, Golfer[].class);
        File tempJson = File.createTempFile("imported", ".json");
        jsonMapper.writerWithDefaultPrettyPrinter().writeValue(tempJson, golfers);
        JSONPersonalDatabase.instance().importData(tempJson);
        tempJson.delete();
    }
}