简体   繁体   中英

JAXB unmarshalling works on Java 1.8 but returns null values in Java 1.7

I need my server to unmarshal xml files. I have implemented the class to do this on my own computer running Java 1.8 and it works perfectly. However when I run the exact same code on the server, which runs Java 1.7, the object is created but I get a null value for the 'domain' element.

My code may not be ideal practice when using JAXB, but as I said on java 1.8 it does exactly what I need it to. Does anybody have an idea as to the cause of this behaviour? Any help would be greatly appreciated.

This is an example xml file which I am trying to unmarshal:

<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>

This is the java file:

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;
            }
        }
    }
}
}

And the package file:

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

package server;

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

Your XML is incorrect according to your schema as defined by your java objects. Everything in your schema is under the http://planning.com namespace but in your XML the only element defined in that namespace is metadata . You need either:

  • Set http://planning.com to the default namespace.

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

  • Use the prefix on all the elements

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


Alternativly if the XML can be assumed to be correct but your Java classes are incorrect. In this case just remove the @XmlSchema annotation from your package-info.java and make your Java read:

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

You dont really have a question in your post as a commentered mentioned so I am unsure if your asking why this works in 1.8 and not in 1.7 and are aware that the XML is incorrect or just didnt know you have some bad XML.

As to why this works in 1.8 is still a mystery to me. I will do a little bit more poking at it and try to find out why out of my own curiosity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM