Search code examples
javaxmlimdb

imdb get only movie summary java (xml)


Is there some easy way to get the summary of an imdb movie as a String value in a Java-programm. I have a program that contains imdb-id's and I want the storyline of that movie to be shown in my application.

I don't know if imdb has some kind of easy way to do this. Because I have some troubles with using xml.

http://www.omdbapi.com/?i=tt2820852&plot=full&r=xml


Solution

  • I prefer JAXB and this is how you do it with JAXB:

    public static void main(String[] args) throws Exception {
        InputStream stream = new FileInputStream("imdb.xml"); // use your stream source
        JAXBContext ctx = JAXBContext.newInstance(Root.class);
        Unmarshaller um = ctx.createUnmarshaller();
        JAXBElement<Root> imdb = um.unmarshal(new StreamSource(stream), Root.class);
        System.out.println(imdb.getValue().movie.plot);
    }
    
    public class Root {
        @XmlElement(name="movie")
        public Movie movie;    
    }
    
    public class Movie {
        @XmlAttribute(name="plot")
        public String plot;
        // Add fields for other attributes you want to read
    }