Add helper class for list of movies to satisfy XML format

This commit is contained in:
karl 2019-12-01 18:08:16 +01:00
parent 9927038407
commit b73bed091d
2 changed files with 19 additions and 3 deletions

View File

@ -0,0 +1,17 @@
package at.technikumwien.movies;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;
/** Wrapper for XML with multiple movies **/
@XmlRootElement(name = "movies")
@AllArgsConstructor
@NoArgsConstructor
public class MovieList {
@XmlElement(name="movie")
public List<Movies> movies;
}

View File

@ -6,15 +6,14 @@ import java.io.File;
import java.util.List; import java.util.List;
public class XmlHelper { public class XmlHelper {
public static void moviesToXml(List<Movies> movies,String filename) throws Exception { public static void moviesToXml(MovieList movies,String filename) throws Exception {
JAXBContext jaxbContext = JAXBContext.newInstance(Movies.class); JAXBContext jaxbContext = JAXBContext.newInstance(MovieList.class);
Marshaller marshaller = jaxbContext.createMarshaller(); Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
// FIXME: Can't marshal list
marshaller.marshal(movies, System.out); marshaller.marshal(movies, System.out);
marshaller.marshal(movies, new File(filename)); marshaller.marshal(movies, new File(filename));
} }