嗨,我是JAXB Conversions的新手。

我正在将XML解组到Java对象中。对于单次出现部分,没有问题,但是对于多次出现无法正确映射。每次我获得多次出现部分的空列表时。

请建议我任何有用的网址或建议我进行更改。

XML ::

<?xml version="1.0" encoding="UTF-8"?>
<designTheory>
    <Administartor>
        <circuitId>67565675476</circuitId>
        <processId>567855</processId>
        <version>01</version>
        <circuitReferenceValue>ciruit-0001</circuitReferenceValue>
        <property>cal-circuit</property>
    </Administartor>
    <designSec>
        <priloc>priloc</priloc>
        <secloc>secloc</secloc>
        <remarks>remarks field</remarks>
    </designSec>
    <designNotes>
        <notesNumber>1</notesNumber>
        <notes>designNotes 1</notes>
    </designNotes>
    <designNotes>
        <notesNumber>2</notesNumber>
        <notes>designNotes 2</notes>
    </designNotes>
    <designNotes>
        <notesNumber>3</notesNumber>
        <notes>designNotes 3</notes>
    </designNotes>
    <designNotes>
        <notesNumber>4</notesNumber>
        <notes>designNotes 4</notes>
    </designNotes>
</designTheory>


代码片段如下:

DesignTheory.java

package org.manjunath.jaxbconversions.beans;

import java.util.List;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "designTheory")
public class DesignTheory {

    @XmlElement(name = "Administartor", required = true)
    private Administartor admin;

    @XmlElement(name = "designSec", required = true)
    private DesignSec designSec;

    @XmlElementWrapper
    @XmlElementRef(name = "designNotes")
    private List<JAXBElement<DesignNotes>> designNotesList;

    public void setAdministartor(Administartor admin){
        this.admin = admin;
    }

    public Administartor getAdministartor() {
        return admin;
    }

    public DesignSec getDesignSec() {
        return designSec;
    }

    public void setDesignSec(DesignSec designSec) {
        this.designSec = designSec;
    }


    public List<JAXBElement<DesignNotes>> getDlrnotes() {
        return designNotesList;
    }

    public void setDlrnotes(List<JAXBElement<DesignNotes>> designNotesList) {
        this.designNotesList = designNotesList;
    }
}


Administartor.java

package org.manjunath.jaxbconversions.beans;

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

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "Administartor")
public class Administartor {

    @XmlElement(name = "circuitId")
    private String circuitId;

    @XmlElement(name = "processId")
    private String processId;

    @XmlElement(name = "version")
    private String version;

    @XmlElement(name = "circuitReferenceValue")
    private String circuitReferenceValue;

    @XmlElement(name = "property")
    private String property;

    public String getcircuitId() {
        return circuitId;
    }

    public void setcircuitId(String circuitId) {
        this.circuitId = circuitId;
    }

    public String getprocessId() {
        return processId;
    }

    public void setprocessId(String processId) {
        this.processId = processId;
    }

    public String getVer() {
        return version;
    }

    public void setVer(String version) {
        this.version = version;
    }

    public String getcircuitReferenceValue() {
        return circuitReferenceValue;
    }

    public void setcircuitReferenceValue(String circuitReferenceValue) {
        this.circuitReferenceValue = circuitReferenceValue;
    }

    public String getproperty() {
        return property;
    }

    public void setproperty(String property) {
        this.property = property;
    }
}


DesignSec.java

package org.manjunath.jaxbconversions.beans;

import java.util.ArrayList;
import java.util.List;

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

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "designSec")
public class DesignSec {

    @XmlElement (name = "priloc")
    private String priloc;

    @XmlElement (name = "secloc")
    private String secloc;

    @XmlElement (name = "remarks")
    private String remarks;

    public String getpriloc() {
        return priloc;
    }

    public void setpriloc(String priloc) {
        this.priloc = priloc;
    }

    public String getSecloc() {
        return secloc;
    }

    public void setSecloc(String secloc) {
        this.secloc = secloc;
    }

    public String getremarks() {
        return remarks;
    }

    public void setEcspc(String remarks) {
        this.remarks = remarks;
    }
}


DesignNotes.java

package org.manjunath.jaxbconversions.beans;

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

@XmlRootElement(name = "designNotes")
@XmlAccessorType(XmlAccessType.FIELD)
public class DesignNotes {

    @XmlElement (name = "notesNumber")
    private String notesNumber;

    @XmlElement (name = "notes")
    private String notes;

    public String getnotesNumber() {
        return notesNumber;
    }

    public void setnotesNumber(String notesNumber) {
        this.notesNumber = notesNumber;
    }

    public String getNotes() {
        return notes;
    }

    public void setNotes(String notes) {
        this.notes = notes;
    }
}


我发现@XmlRegistry和@XmlElementDecl可以解决我的问题。
但是我对这些注释不是很好,但是我尝试使用ObjectFactory.java类。不使用此类

ObjectFactory.java

package org.manjunath.jaxbconversions.factory;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlElementDecl;
import javax.xml.bind.annotation.XmlRegistry;
import javax.xml.namespace.QName;

import org.manjunath.jaxbconversions.beans.DesignNotes;

@XmlRegistry
public class ObjectFactory {

    private final static QName _DesignNotes_QNAME = new QName("designNotes");

    public ObjectFactory(){

    }

    @XmlElementDecl(name="designNotes")
    public JAXBElement<DesignNotes> createDesignNotes(DesignNotes value) {
        return new JAXBElement<DesignNotes>(_DesignNotes_QNAME, DesignNotes.class, value);
    }

}


请建议我解决这个问题

最佳答案

在您的课程DesignTheory中,定义

@XmlElementWrapper
@XmlElementRef(name = "designNotes")
private List<JAXBElement<DesignNotes>> designNotesList;


是错的。
在您的XML中

<designNotes>
  ...
</desinNotes>
<designNotes>
  ...
</designNotes>
...


但是您不必像这样围绕这些<designNotes>附加包装器

<designNotesList>
  <designNotes>
    ...
  </desinNotes>
  <designNotes>
    ...
  </designNotes>
  ...
<designNotesList>


因此,您需要删除@XmlElementWrapper批注。

并且您应该从List<JAXBElement<DesignNotes>>更改其类型
List<DesignNotes>。所以你最终会

@XmlElementRef(name = "designNotes")
private List<DesignNotes> designNotesList;


同时从List<JAXBElement<DesignNotes>>更改关联的getter和setter
List<DesignNotes>

然后,您就不再需要ObjectFactory并可以将其完全删除。

我用您的XML和以下测试代码验证了更正的类

@Test
public void testUnmarshall() throws Exception {
    JAXBContext context = JAXBContext.newInstance(DesignTheory.class);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    File file = new File("design-theory.xml");
    DesignTheory designTheory = (DesignTheory) unmarshaller.unmarshal(file);
    Assert.assertNotNull(designTheory.getDlrnotes());
    Assert.assertEquals(4, designTheory.getDlrnotes().size());
}


正确地将未编组的designTheory具有包含4个元素的非null List<DesignNotes>

07-25 23:21