I'm currently developing a travel service and we have to write user information to an XML file using JAXB Marshalling. When running the app on the Tomcat server, I type in the information in the textbox and click "Register", the XML format is written within the console, but it's not actually written to the XML file. As of right now, I have my "accounts.xml" in the "src/main/resources" directory. Here is my controller information so far. I'm not exactly sure why it won't write to the XML file:
@Controller
public class AccountController {
@RequestMapping(value = "/register", method = RequestMethod.GET)
public String registerPage(Locale locale, Model model) {
return "register";
}
@RequestMapping(value = "/home", method = RequestMethod.POST)
public String register(@RequestParam("name") String name, @Validated User user, Model model) throws JAXBException, IOException{
Account account = new Account();
account.setName(name);
account.setEmail("email");
account.setPassword("password");
try {
File file = ResourceUtils.getFile("classpath:accounts.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Account.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
jaxbMarshaller.marshal(account, file);
jaxbMarshaller.marshal(account, System.out);
} catch (JAXBException e) {
e.printStackTrace();
}
model.addAttribute("name", user.getName());
return "user";
}
}
My console output looks like this when I submit the information:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<account>
<email>email</email>
<name>testName</name>
<password>password</password>
</account>
Any help would be appreciated!