2012-12-10 31 views
0

我仍然是编程新手,如果任何人都可以帮我解决这个问题,基本上我有一个我想解开的电影文件,只有“Robert Benton “person/director作为system.out的输出。与JAXBUJava使用JAXB和选定的输出解开XML文档文件

 package jaxbadv; 

    import java.io.File; 
    import java.util.Iterator; 
    import java.util.List; 

    import org.me.media.*; 
    /** 
    * 
    * @author Ket 
    */ 
    public class rbFilms { 

    public static void main(String[] args) { 
     // Create root XML node 'todaysShow' and get its main element 'movies_today' 
     ShowingToday todaysShow = new ShowingToday(); 
     List<MovieType> movies_today = todaysShow.getMovieCollection(); 
     // Create Movie instanses and add them to the 'movies_today' collection 
     MovieType film; 


     film = new MovieType(); 
     film.getTitle(); 
     film.getDirector(); 
     film.getYear(); 

     try { 
      javax.xml.bind.JAXBContext jaxbCtx = javax.xml.bind.JAXBContext.newInstance(film.getClass().getPackage().getName()); 
      javax.xml.bind.Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller(); 
      film = (MovieType) unmarshaller.unmarshal(new java.io.File("Now_Showing.txt")); //NOI18N 

      //print out only movies produced after 1990 
      MovieType nextMovie = new MovieType(); 
      Iterator itr = movies_today.iterator(); 
      while(itr.hasNext()) { 
       nextMovie = (MovieType) itr.next(); 
       if(nextMovie.getDirector() == "Robert Benton") { 
        System.out.println(nextMovie.getTitle()); 
       } 
      } 


     } catch (javax.xml.bind.JAXBException ex) { 
      // XXXTODO Handle exception 
      java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE, null, ex); //NOI18N 
     } 




    } 
} 

XML文件

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<Showing_Today xmlns="http://xml.netbeans.org/schema/Shows"> 
    <movie_collection> 
     <Title>Red</Title> 
     <Director>Robert Schwentke</Director> 
     <Year>2010</Year> 
    </movie_collection> 
    <movie_collection> 
     <Title>Kramer vs Kramer</Title> 
     <Director>Robert Benton</Director> 
     <Year>1979</Year> 
    </movie_collection> 
    <movie_collection> 
     <Title>La Femme Nikita</Title> 
     <Director>Luc Besson</Director> 
     <Year>1997</Year> 
    </movie_collection> 
    <movie_collection> 
     <Title>Feast of love</Title> 
     <Director>Robert Benton</Director> 
     <Year>2007</Year> 
    </movie_collection> 
</Showing_Today> 

JAXB绑定生成的源

Java类 - ShowingToday

package org.me.media; 

import java.util.ArrayList; 
import java.util.List; 
import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlRootElement; 
import javax.xml.bind.annotation.XmlType; 

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "", propOrder = { 
    "movieCollection" 
}) 
@XmlRootElement(name = "Showing_Today") 
public class ShowingToday { 

    @XmlElement(name = "movie_collection") 
    protected List<MovieType> movieCollection; 

public List<MovieType> getMovieCollection() { 
     if (movieCollection == null) { 
      movieCollection = new ArrayList<MovieType>(); 
     } 
     return this.movieCollection; 
    } 

} 

回答

1

您可能想从一些简单的练习开始,在代码中存在一些基本的Java错误。例如,你有一些没有效果的任务:

showingToday todaysShow = new ShowingToday(); // value isn't used 
List<MovieType> movies_today = todaysShow.getMovieCollection(); // value isn't used 

有的地方要初始化变量,使得没有效果就可以了get电话:

film = new MovieType(); // values is never used 
film.getTitle();  // this and the other get calls are not needed 
film.getDirector(); 
film.getYear(); 

您将要修复这些。


至于您的特定JAXB的问题,从我可以告诉,你应该从你的XML流反序列化ShowingToday实例,然后从中获取信息。代码将类似于此:

try { 
    final JAXBContext context = JAXBContext 
     .newInstance(ShowingToday.class); 
    final Unmarshaller unmarshaller = context.createUnmarshaller(); 
    final ShowingToday showingToday = unmarshaller.unmarshal(
     new StreamSource(new File("absolute path of file here")), 
     ShowingToday.class).getValue(); 

} catch (final Exception e) { 
    // Do something useful here 
} 
+0

正确我会看看它,感谢您的意见。 – Ket

+0

@Ket - 我已经添加了一个代码示例。 – Perception