如何使用 JAXB 解析嵌套节点中的属性?
How to parse attributes in a nested node with 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="tt2322441"
type="movie" />
</root>
JAXB 使用的 class 如下所示:
@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;
}
}
这一直给我 JAXBException
s。我的 JAXB 注释有什么问题?
XML 层次结构是这样的 root/movie
。
对于 JAXB,您必须重新创建此层次结构。这就是@XmlRootElement(name = "movie")
在这里无效的原因。
但是,只要属性和字段共享相同的名称,JAXB 允许您自动将属性从 XML 源映射到 class 字段。
总结前面的所有要点,这里是输入 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;
}
}
Movie.java
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
class可以是静态的classIMDBInfo
class.
看到这个答案:
EclipseLink Moxy 是 JAXB 规范的一个实现。它引入了一个扩展,使您能够使用 XPath 在 XML.
中导航
看到这个答案:
我正在尝试解析使用 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>
JAXB 使用的 class 如下所示:
@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;
}
}
这一直给我 JAXBException
s。我的 JAXB 注释有什么问题?
XML 层次结构是这样的 root/movie
。
对于 JAXB,您必须重新创建此层次结构。这就是@XmlRootElement(name = "movie")
在这里无效的原因。
但是,只要属性和字段共享相同的名称,JAXB 允许您自动将属性从 XML 源映射到 class 字段。
总结前面的所有要点,这里是输入 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;
}
}
Movie.java
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
class可以是静态的classIMDBInfo
class.
看到这个答案:EclipseLink Moxy 是 JAXB 规范的一个实现。它引入了一个扩展,使您能够使用 XPath 在 XML.
中导航 看到这个答案: