Java 如何使用JAXB解析嵌套节点中的属性?

Java 如何使用JAXB解析嵌套节点中的属性?,java,xml,jaxb,Java,Xml,Jaxb,我试图解析用omdbAPI创建的XML文件。XML文件如下所示: <?xml version="1.0" encoding="UTF-8"?> <root response="True"> <movie title="Fifty Shades of Grey" year="2015" ... plot="..." imdbRating="4.2" imdbID="t

我试图解析用omdbAPI创建的XML文件。XML文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<root response="True">
   <movie
        title="Fifty Shades of Grey"
        year="2015"
        ...
        plot="..."
        imdbRating="4.2"
        imdbID="tt2322441"
        type="movie" />
</root>
@XmlRootElement(name = "movie")
public class IMDBInfo {
    private String plot;
    private String imdbRating;

    @XmlAttribute(name = "plot")
    public void setPlot(String plot){
        this.plot = plot;
    }

    @XmlAttribute(name = "imdbID")
    public void setImdbRating(String imdbRating){
        this.imdbRating = imdbRating;
    }

    public String getPlot(){
        return plot;
    }

    public String getImdbRating(){
        return imdbRating;
    }
}

这一直给我
jaxbeexception
s。我的JAXB注释怎么了?

XML层次结构是这样的
root/movie

使用JAXB,必须重新创建此层次结构。这就是为什么
@XmlRootElement(name=“movie”)
在这里无效的原因

但是,JAXB允许您将属性从XML源自动映射到类字段,只要属性和字段共享相同的名称

总结前面的所有要点,下面是一个输入XML的工作示例:

一个选择。。。 IMDBInfo.java

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "root")
public class IMDBInfo {

    @XmlElement
    private Movie movie;

    public Movie getMovie() {
        return movie;
    }
}
import javax.xml.bind.annotation.XmlAttribute;

public class Movie {
    @XmlAttribute
    private String plot;

    @XmlAttribute
    private String imdbRating;

    public String getPlot() {
        return plot;
    }

    public String getImdbRating() {
        return imdbRating;
    }
}
Movie.java

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "root")
public class IMDBInfo {

    @XmlElement
    private Movie movie;

    public Movie getMovie() {
        return movie;
    }
}
import javax.xml.bind.annotation.XmlAttribute;

public class Movie {
    @XmlAttribute
    private String plot;

    @XmlAttribute
    private String imdbRating;

    public String getPlot() {
        return plot;
    }

    public String getImdbRating() {
        return imdbRating;
    }
}
示例用法

public static void main(String[] args) {
    try {

        File file = new File("file.xml");
        JAXBContext jaxbContext = JAXBContext.newInstance(IMDBInfo.class);

        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        IMDBInfo imdbInfo = (IMDBInfo) jaxbUnmarshaller.unmarshal(file);
        Movie movie = imdbInfo.getMovie();

        System.out.format(//
                "ImdbRating: %s\nPlot: %s\n", //
                movie.getImdbRating(), //
                movie.getPlot() //
                );

    } catch (JAXBException e) {
        e.printStackTrace();
    }
}
输出

ImdbRating: 4.2
Plot: When Anastasia Steele, a literature student, goes to interview the wealthy Christian Grey as a favor to her roommate Kate Kavanagh, she encounters a beautiful, brilliant and intimidating man. The innocent and naive Ana starts to realize she wants him. Despite his enigmatic reserve and advice, she finds herself desperate to get close to him. Not able to resist Ana's beauty and independent spirit, Christian Grey admits he wants her too, but on his own terms. Ana hesitates as she discovers the singular tastes of Christian Grey - despite the embellishments of success, his multinational businesses, his vast wealth, and his loving family, Grey is consumed by the need to control everything.
... 和其他选择
  • Movie
    类可以是
    IMDBInfo
    类的静态类。
    看看这个答案:

  • EclipseLink Moxy是JAXB规范的一个实现。它引入了一个扩展,使您能够使用XPath在XML中导航。
    看看这个答案:


要获得真正的答案,需要精确的JAXBEException消息,但我注意到,您的映射中似乎缺少
root
元素。非常感谢!这就解决了问题。我现在明白了它的工作原理:)