gpt4 book ai didi

java - JAXB 解码适用于 Java 1.8,但在 Java 1.7 中返回空值

转载 作者:行者123 更新时间:2023-11-30 10:55:01 25 4
gpt4 key购买 nike

我需要我的服务器来解码 xml 文件。我已经在我自己的运行 Java 1.8 的计算机上实现了这个类,并且它运行良好。但是,当我在运行 Java 1.7 的服务器上运行完全相同的代码时,该对象已创建,但我得到了“域”元素的空值。

我的代码在使用 JAXB 时可能不是理想的做法,但正如我在 Java 1.8 上所说的那样,它完全可以满足我的需要。有人知道这种行为的原因吗?任何帮助将不胜感激。

这是我要解码的示例 xml 文件:

<planning:metadata xmlns:planning="http://planning.com">
<domain id=numeric">
<title xml:lang="en">The numeric formulation</title>
<files_last_modified>2002-06-01T12:00:00</files_last_modified>
<metadata_last_modified>2014-04-02T11:31:17.471631</metadata_last_modified>
<published>2002-06-01T12:00:00</published>
<link>http://ipc.icaps.org/</link>
<requirements>
<typing/>
<fluents/>
</requirements>
<problems>
<problem domain_file="domain.pddl" number="1" problem_file="p01.pddl"/>
<problem domain_file="domain.pddl" number="2" problem_file="p02.pddl"/>
<problem domain_file="domain.pddl" number="3" problem_file="p03.pddl"/>
<problem domain_file="domain.pddl" number="4" problem_file="p04.pddl"/>
<problem domain_file="domain.pddl" number="5" problem_file="p05.pddl"/>
<problem domain_file="domain.pddl" number="6" problem_file="p06.pddl"/>
</problems>
</domain>
</planning:metadata>

这是java文件:

package server;

import javax.xml.bind.annotation.*;
import java.util.ArrayList;
import java.util.Date;

@XmlRootElement(name="metadata")
public class XmlDomain {

private XmlDomain.Domain domain;

public XmlDomain.Domain getDomain() {
return domain;
}

@XmlElement
public void setDomain(XmlDomain.Domain domain) {
this.domain = domain;
}

public static class Domain {
private String id;
private String title;
private Date filesModifiedDate;
private Date metaModifiedDate;
private Date publishedDate;
private String link;
private Requirements requirements;
private Problems problems;

public String getId() {
return id;
}

@XmlAttribute
public void setId(String id) {
this.id = id;
}

public String getTitle() {
return title;
}

@XmlElement
public void setTitle(String title) {
this.title = title;
}

public Date getFiles_last_modified() {
return filesModifiedDate;
}

@XmlElement
public void setFiles_last_modified(Date date) {
filesModifiedDate = date;
}

public Date getMetadata_last_modified() {
return metaModifiedDate;
}

@XmlElement
public void setMetadata_last_modified(Date date) {
metaModifiedDate = date;
}

public Date getPublished() {
return publishedDate;
}

@XmlElement
public void setPublished(Date date) {
publishedDate = date;
}

public String getLink() {
return link;
}

@XmlElement
public void setLink(String link) {
this.link = link;
}

public Requirements getRequirements() {
return requirements;
}

@XmlElement
public void setRequirements(Requirements requirements) {
this.requirements = requirements;
}

public Problems getProblems() {
return problems;
}

@XmlElement
public void setProblems(Problems problems) {
this.problems = problems;
}

public static class Requirements {

// Strings representing domain requirements
private String strips = null;
private String typing = null;
private String durative = null; // durative-actions
private String fluents = null;
private String timed = null; // time-initial-literal
private String equality = null;
private String inequalities = null; // duration_inequalities

public String getStrips() {
return strips;
}

@XmlElement(name = "strips")
public void setStrips(String strips) {
this.strips = strips;
}

public String getTyping() {
return typing;
}

@XmlElement(name = "typing")
public void setTyping(String typing) {
this.typing = typing;
}

public String getDurative() {
return durative;
}

@XmlElement(name = "durative-actions")
public void setDurative(String durative) {
this.durative = durative;
}

public String getFluents() {
return fluents;
}

@XmlElement(name = "fluents")
public void setFluents(String fluents) {
this.fluents = fluents;
}

public String getTimed() {
return timed;
}

@XmlElement(name = "timed-initial-literals")
public void setTimed(String timed) {
this.timed = timed;
}

public String getEquality() {
return equality;
}

@XmlElement(name = "equality")
public void setEquality(String equality) {
this.equality = equality;
}

public String getInequalities() {
return inequalities;
}

@XmlElement(name = "duration_inequalities")
public void setInequalities(String inequalities) {
this.inequalities = inequalities;
}
}

public static class Problems {

private ArrayList<Problem> problems;

public ArrayList<Problem> getProblem() {
return problems;
}

public void setProblem(ArrayList<Problem> problems) {
this.problems = problems;
}

public static class Problem {

private String domainFile;
private int number;
private String problemFile;

public String getDomain_file() {
return domainFile;
}

@XmlAttribute(name = "domain_file")
public void setDomain_file(String domainFile) {
this.domainFile = domainFile;
}

public int getNumber() {
return number;
}

@XmlAttribute
public void setNumber(int number) {
this.number = number;
}

public String getProblem_file() {
return problemFile;
}

@XmlAttribute(name = "problem_file")
public void setProblem_file(String problemFile) {
this.problemFile = problemFile;
}

public void addResult(Planner planner, double result) {
resultMap.put(planner, result);
}

public double getResult(Planner planner) {
return resultMap.get(planner);
}

public HashMap<Planner, Double> getResultMap() {
return resultMap;
}
}
}
}
}

和包文件:

@XmlSchema(
namespace = "http://planning.com",
elementFormDefault = XmlNsForm.QUALIFIED)

package server;

import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

最佳答案

根据您的 java 对象定义的架构,您的 XML 不正确。您架构中的所有内容都在 http://planning.com 下命名空间,但在您的 XML 中,该命名空间中定义的唯一元素是 metadata .您需要:

  • 设置http://planning.com到默认命名空间。

    <metadata xmlns="http://planning.com">
    <domain id=numeric">
    ...

  • 在所有元素上使用前缀

    <planning:metadata xmlns:planning="http://planning.com">
    <planning:domain id=numeric">
    ...


或者,如果可以假定 XML 是正确的,但您的 Java 类不正确。在这种情况下,只需删除 @XmlSchema来自您的注释 package-info.java并让你的 Java 读取:

@XmlRootElement(name="metadata", namespace="http://planning.com")
public class XmlDomain {
...

正如评论者所提到的,您在您的帖子中并没有真正提出问题,所以我不确定您是否在问为什么这在 1.8 而不是 1.7 中有效并且知道 XML 不正确或只是没有知道您有一些糟糕的 XML。

至于为什么这在 1.8 中有效对我来说仍然是个谜。我会做更多的研究,并出于我自己的好奇心尝试找出原因。

关于java - JAXB 解码适用于 Java 1.8,但在 Java 1.7 中返回空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33457982/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com