Add xmlToMovies function to XmlHelper

This commit is contained in:
karl 2019-12-01 18:13:59 +01:00
parent b73bed091d
commit 03320279b9

View File

@ -1,7 +1,7 @@
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;
@ -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;
}
}