简体   繁体   中英

Error reading XML via JaxB

This is my class structure:

@XmlRootElement(name="System")
public class SystemDTO () {

  @XmlElement(name="ID")
  public void setId(String id) {
      this.id = id;
  }

  @XmlElement(name="Source")
  public void setSource(SourceDTO source) {
      this.source = source;
  }
}

@XmlRootElement(name="Source")
class SourceDTO {
  @XmlElement(name="Name")
  public void setName(String name) {
      this.name = name;
  }
}

This is my XML File:

<System>
   <ID>e5b160d0</ID>
   <Source>
     <Name>Kron</Name>
   </Source>
</System>

The problem is the Source is always null. I do not get a exception, it just comes out null. I've attempted to use just the Source tag and it picks up Name just fine, but when I add it as part of the System class it does not seem to work.

Additionally I attempted to do this and have a string member variable in System for name:

@XmlElementWrapper(name="Source")
@XmlElement(name="Name")

But that causes an exception. Any ideas?

The Test Class

    public class JaxbTest {
        public static void main(String[] args) {

            String xml = "<System>\n" +
                    "   <ID>e5b160d0</ID>\n" +
                    "   <Source>\n" +
                    "     <Name>Kron</Name>\n" +
                    "   </Source>\n" +
                    "</System>";
            SystemDTO systemDTO;
            try {

                JAXBContext jaxbContext = JAXBContext.newInstance(SystemDTO.class);

                StringReader reader = new StringReader(xml);
                Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
                systemDTO = (SystemDTO) jaxbUnmarshaller.unmarshal(reader);
                System.out.println(systemDTO.getSource().getName());

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

    }

Source DTO

    @XmlRootElement(name = "Source")
    class SourceDTO {
        private String name = null;

        public String getName() {
            return name;
        }

        @XmlElement(name = "Name")
        public void setName(String name) {
            this.name = name;
        }

    }

System DTO

    @XmlRootElement(name = "System")
    public class SystemDTO {

        private String id;
        private SourceDTO source;

        public String getId() {
            return id;
        }

        public SourceDTO getSource() {
            return source;
        }

        @XmlElement(name = "ID")
        public void setId(String id) {
            this.id = id;
        }

        @XmlElement(name = "Source")
        public void setSource(SourceDTO source) {
            this.source = source;
        }

    }

One possible problem that I see is that you have declared 2 "Root" elements in your XML. Try @XmlType on your Source class:

    @XmlType(name="Source")
    class SourceDTO {
       ...
    }

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