3

是否有任何方法可以使用 @XmlDescriminatorNode/@XmlDescrimintatorValue 注释解组下一个 XML,或任何解决方法:

<assets>
    <asset type="full">
        <data_file role="source">
            <locale name="ru-RU"/>
        </data_file>
        <data_file role="extension">
            <locale name="ru-RU"/>
        </data_file>
        <data_file>
            <locale name="ru-RU"/>
        </data_file>
    </asset>
</assets>

我的映射类:

@XmlRootElement(name="data_file")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlDiscriminatorNode("@role")
public abstract class BaseDataFile implements Serializable {

    @XmlPath("@role")
    @XmlAttribute(name = "role")
    private String role;

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }
}
@XmlRootElement(name="data_file")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlDiscriminatorValue("source")
public class SourceDataFile extends BaseDataFile {

}
@XmlRootElement(name="data_file")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlDiscriminatorValue("source")
public class SourceDataFile extends BaseDataFile {

}
@XmlRootElement(name="data_file")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlDiscriminatorValue("extension")
public class SourceDataFile extends BaseDataFile {

}
@XmlRootElement(name="asset")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlDiscriminatorNode("@type")
public abstract class BaseAsset implements Serializable {

    @XmlPath("@type")
    @XmlAttribute(name = "type")
    private String type;

    @XmlPath("data_file")
    private List<BaseDataFile> dataFiles;

    public List<BaseDataFile> getDataFiles() {
        return dataFiles;
    }

    public void setDataFiles(List<BaseDataFile> dataFiles) {
        this.dataFiles = dataFiles;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}

如果 XML 包含如下元素,但没有“type”属性,则会发生错误:

<data_file>
  <locale name="ru-RU"/>
</data_file>

提前致谢

4

1 回答 1

3

以下内容应该有所帮助:

JAVA模型

超级班 ( BaseDataFile)

以下是您的BaseDataFile课程的简化版本。由于您已将 XML 属性映射role为继承指示符,因此您无需将其也映射到对象模型中的属性。

import java.io.Serializable;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlDiscriminatorNode;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlSeeAlso({SourceDataFile.class, ExtensionDataFile.class})
@XmlDiscriminatorNode("@role")
public abstract class BaseDataFile implements Serializable {

}

如果您真的想将roleXML 属性映射到对象模型中的属性,您应该使用 MOXy 的@XmlReadOnly属性来防止它被编组到 XML 文档(它已经被写为继承指示符)。

import java.io.Serializable;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlDiscriminatorNode;
import org.eclipse.persistence.oxm.annotations.XmlReadOnly;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlSeeAlso({SourceDataFile.class, ExtensionDataFile.class})
@XmlDiscriminatorNode("@role")
public abstract class BaseDataFile implements Serializable {

    @XmlAttribute
    @XmlReadOnly
    String role;

}

子类 ( SourceDataFile)

import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlDiscriminatorValue;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlDiscriminatorValue("source")
public class SourceDataFile extends BaseDataFile {

}

缺少继承指标

基类不是抽象的

如果您的基类 ( BaseDataFile) 不是抽象的,那么如果缺少继承指示符,则会创建一个基类的实例。

基类是抽象的

由于您的基类是抽象的 MOXy 抱怨缺少继承指示符值:

Exception in thread "main" Local Exception Stack: 
Exception [EclipseLink-44] (Eclipse Persistence Services - @VERSION@.@QUALIFIER@): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: Missing class indicator field from database row [UnmarshalRecordImpl()].
Descriptor: XMLDescriptor(forum15597322.BaseDataFile --> [])
    at org.eclipse.persistence.exceptions.DescriptorException.missingClassIndicatorField(DescriptorException.java:957)
    at org.eclipse.persistence.internal.oxm.XMLRelationshipMappingNodeValue.processChild(XMLRelationshipMappingNodeValue.java:83)
    at org.eclipse.persistence.internal.oxm.XMLCompositeCollectionMappingNodeValue.startElement(XMLCompositeCollectionMappingNodeValue.java:184)
    at org.eclipse.persistence.internal.oxm.record.UnmarshalRecordImpl.startElement(UnmarshalRecordImpl.java:834)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:506)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:376)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2715)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:607)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:116)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:488)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:835)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:123)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1210)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:568)
    at org.eclipse.persistence.internal.oxm.record.XMLReader.parse(XMLReader.java:221)
    at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:895)
    at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:388)
    at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:366)
    at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:323)
    at org.eclipse.persistence.oxm.XMLUnmarshaller.unmarshal(XMLUnmarshaller.java:367)
    at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:123)
    at forum15597322.Demo.main(Demo.java:23)

忽略错误

JAXB (JSR-222)实现(包括 MOXy 报告在编组/解组到ValidationEventHandler默认值期间遇到的异常)将在遇到缺少继承指示符值时出错。下面是一个设置自定义的示例,该自定义ValidationEventHandler表示通过truehandleEvent方法返回永不出错。

    Unmarshaller unmarshaller = jc.createUnmarshaller();
    unmarshaller.setEventHandler(new ValidationEventHandler() {
        @Override
        public boolean handleEvent(ValidationEvent event) {
            return true;
        }

    });

将这个答案放在一起时,我发现了以下 MOXy 错误,由于执行上述操作,该错误会将无效的文本值作为集合中的一个项目。该修复针对 EclipseLink 2.5.1。

一旦修复进入无效条目,将被忽略。这是您正在寻找的行为吗?


了解更多信息

于 2013-03-25T14:34:44.340 回答