Java将XML文档文件与JAXB和所选输出进行解组
我还是一个编程新手,如果有人能帮我解决这个问题,我将不胜感激。基本上,我有一个电影文件,我想解组,system.out上只有“Robert Benton”个人/导演作为输出 带有JAXBU的Java类Java将XML文档文件与JAXB和所选输出进行解组,java,xml,jaxb,unmarshalling,Java,Xml,Jaxb,Unmarshalling,我还是一个编程新手,如果有人能帮我解决这个问题,我将不胜感激。基本上,我有一个电影文件,我想解组,system.out上只有“Robert Benton”个人/导演作为输出 带有JAXBU的Java类 package jaxbadv; import java.io.File; import java.util.Iterator; import java.util.List; import org.me.media.*; /**
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
}
}
}
jaxbadv包;
导入java.io.File;
导入java.util.Iterator;
导入java.util.List;
导入org.me.media.*;
/**
*
*@作者Ket
*/
公共类rbFilms{
公共静态void main(字符串[]args){
//创建根XML节点“todaysShow”并获取其主要元素“movies\u today”
ShowingToday todaysShow=新ShowingToday();
列出电影\u todays=todaysShow.getMovieCollection();
//创建电影实例并将其添加到“今日电影”集合中
电影胶片;
电影=新电影类型();
film.getTitle();
电影导演();
film.getYear();
试一试{
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(新的java.io.File(“Now_Showing.txt”);//NOI18N
//仅打印1990年以后制作的电影
MovieType nextMovie=新MovieType();
迭代器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句柄异常
java.util.logging.Logger.getLogger(“全局”).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>
红色
罗伯特·斯文克
2010
克莱默对克莱默
罗伯特·本顿
1979
尼基塔女士酒店
吕克·贝松
1997
爱的盛宴
罗伯特·本顿
2007
JAXB绑定生成的源代码-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;
}
}
package org.me.media;
导入java.util.ArrayList;
导入java.util.List;
导入javax.xml.bind.annotation.XmlAccessType;
导入javax.xml.bind.annotation.XmlAccessorType;
导入javax.xml.bind.annotation.xmlement;
导入javax.xml.bind.annotation.XmlRootElement;
导入javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name=),比例={
“电影收藏”
})
@XmlRootElement(name=“今天显示”)
公共课展示日{
@xmlement(name=“电影收藏”)
受保护列表电影收藏;
公共列表getMovieCollection(){
if(movieCollection==null){
movieCollection=newarraylist();
}
返回此.movieCollection;
}
}
您可能希望从一些简单的练习开始,因为代码中存在一些基本的Java错误。例如,您有一些无效的指定:
showingToday todaysShow = new ShowingToday(); // value isn't used
List<MovieType> movies_today = todaysShow.getMovieCollection(); // value isn't used
你会想要修复这些
就您的特定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
}
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
}