Add xmlToMovies function to XmlHelper
This commit is contained in:
parent
b73bed091d
commit
03320279b9
@ -1,12 +1,12 @@
|
||||
package at.technikumwien.movies;
|
||||
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.Marshaller;
|
||||
import javax.xml.bind.*;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
public class XmlHelper {
|
||||
public static void moviesToXml(MovieList movies,String filename) throws Exception {
|
||||
public static void moviesToXml(MovieList movies, String filename) throws Exception {
|
||||
JAXBContext jaxbContext = JAXBContext.newInstance(MovieList.class);
|
||||
|
||||
Marshaller marshaller = jaxbContext.createMarshaller();
|
||||
@ -17,4 +17,18 @@ public class XmlHelper {
|
||||
marshaller.marshal(movies, System.out);
|
||||
marshaller.marshal(movies, new File(filename));
|
||||
}
|
||||
|
||||
public static List<Movies> xmlToMovies(String filename) throws JAXBException {
|
||||
JAXBContext jaxbContext = JAXBContext.newInstance(MovieList.class);
|
||||
|
||||
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
|
||||
|
||||
StreamSource streamSource = new StreamSource(new File(filename));
|
||||
JAXBElement jaxElement = unmarshaller.unmarshal(streamSource, MovieList.class);
|
||||
|
||||
// TODO: Cast here shouldn't be necessary?
|
||||
MovieList movieList = (MovieList) jaxElement.getValue();
|
||||
|
||||
return movieList.movies;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user