package design.persistence.importexport; import java.io.File; import java.io.IOException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.dataformat.xml.XmlMapper; public class XMLHandler implements DataHandler { private final DataSource dataSource; private final XmlMapper xmlMapper = new XmlMapper(); public XMLHandler(DataSource dataSource) { this.dataSource = dataSource; } @Override public void importData(File file) throws IOException { JsonNode tree = xmlMapper.readTree(file); JsonNode unwrapped = tree.get("items"); dataSource.importData(unwrapped); } @Override public void exportData(File file) throws IOException { JsonNode tree = dataSource.exportData(); ObjectNode wrapper = xmlMapper.createObjectNode(); wrapper.set("items", tree); xmlMapper.writerWithDefaultPrettyPrinter().withRootName("export").writeValue(file, wrapper); } }