Java 使用 JAXB 和选定的输出解码 XML 文档文件

标签 java xml jaxb unmarshalling

我在编程方面还是个新手,如果有人能帮助我解决这个问题,我将不胜感激,基本上我有一个想要解码的电影文件,并且只有“Robert Benton”人/导演作为 system.out 上的输出.

使用 JAXBU 的 Java 类

        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 绑定(bind)生成的源 - 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;
    }

}

最佳答案

您可能想从一些更简单的练习开始,您的代码中犯了一些基本的 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
}

关于Java 使用 JAXB 和选定的输出解码 XML 文档文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13806529/

相关文章:

web-services - JAXB 类 : reuse for REST

java - android当url音频流时,音乐没有停止

xml - Xpath:比较十进制类型的节点属性

php simpleXMLElement 到数组 : null value

java - 如何在 Maven 中表达对 java ee 特性的依赖以过渡到 Java 9?

java - 如何在 XmlElements 列表中获取 XmlElement 名称

java - 回滚事务功能是否仅在抛出异常时才起作用?

java - 使用 Java 交替打印 2 个具有多个变量的字符串

java条码阅读器

java - android apk 中的错误 xml