From 38aacc304d5bc55a8e5705f9eb378ca18fc5a4a7 Mon Sep 17 00:00:00 2001 From: WillemDalton Date: Thu, 13 Nov 2025 09:05:29 -0500 Subject: added comments to adapter --- src/main/java/design/persistence/XMLHandler.java | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/main/java/design/persistence/XMLHandler.java b/src/main/java/design/persistence/XMLHandler.java index 174407a..706dabd 100644 --- a/src/main/java/design/persistence/XMLHandler.java +++ b/src/main/java/design/persistence/XMLHandler.java @@ -18,19 +18,32 @@ public class XMLHandler implements DataHandler private final XmlMapper xmlMapper = new XmlMapper(); public void exportPersonalData(File file) throws IOException { - // get our json data + // get our json data just from the JSON database File jsonData = JSONPersonalDatabase.instance().exportData(file); - // read the top tree node (the array) + + // read the the data from the JSON, as a JsonNode. JsonNode golfersNode = jsonMapper.readTree(jsonData); - // map it to an object node - xmlMapper.writerWithDefaultPrettyPrinter().writeValue(file, golfersNode); + + // XML requires a parent node, we can't just throw the golfersnode into the xml or else it will only produce the first element + ObjectNode root = xmlMapper.createObjectNode(); + root.set("golfer", golfersNode); + + //write to the xml file. + xmlMapper.writerWithDefaultPrettyPrinter().writeValue(file, root); } public void importPersonalData(File file) throws IOException { + + // read our root, the object node JsonNode root = xmlMapper.readTree(file); + // debug print line System.out.println(root.toPrettyString()); + + // !!! here is where it's failing...our root is not serializable to an array of golfers. !!! Golfer[] golfers = xmlMapper.treeToValue(root, Golfer[].class); + + // from here out should be good File tempJson = File.createTempFile("imported", ".json"); jsonMapper.writerWithDefaultPrettyPrinter().writeValue(tempJson, golfers); JSONPersonalDatabase.instance().importData(tempJson); -- cgit v1.2.3