Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用JAXB根据属性创建引用对象_Java_Xml_Jaxb - Fatal编程技术网

Java 使用JAXB根据属性创建引用对象

Java 使用JAXB根据属性创建引用对象,java,xml,jaxb,Java,Xml,Jaxb,考虑以下xml: <Config> <Paths> <Path reference="WS_License"/> </Paths> <Steps> <Step id="WS_License" title="License Agreement" /> </Steps> </Config> <Config> <S

考虑以下xml:

<Config>
    <Paths>
        <Path reference="WS_License"/>
    </Paths>

    <Steps>
        <Step id="WS_License" title="License Agreement" />
    </Steps>
</Config>
<Config>
    <Step id="WS_License" title="License Agreement">
        <DetailPanelReference reference="DP_License" />
    </Step>

    <DetailPanels>
        <DetalPanel id="DP_License" title="License Agreement" />
    </DetailPanels>
</Config>

我不想将引用作为字符串存储在Path对象中,而是将其作为Step对象保存。这些对象之间的链接是引用和id属性。@XMLJavaTypeAdapter属性是正确的选择吗?有谁能提供一个正确用法的例子吗

谢谢

编辑:

我还想对一个元素使用同样的技术

考虑以下xml:

<Config>
    <Paths>
        <Path reference="WS_License"/>
    </Paths>

    <Steps>
        <Step id="WS_License" title="License Agreement" />
    </Steps>
</Config>
<Config>
    <Step id="WS_License" title="License Agreement">
        <DetailPanelReference reference="DP_License" />
    </Step>

    <DetailPanels>
        <DetalPanel id="DP_License" title="License Agreement" />
    </DetailPanels>
</Config>
步骤对象中的属性_detailPanels为空,链接似乎不起作用。是否有任何选项可以创建链接而不创建仅包含对DetailPanel的引用的新JAXB对象


再次感谢:)

您可以使用
@XmlID
将属性映射为键,并使用
@XmlIDREF
将引用映射到此用例的键

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

@XmlAccessorType(XmlAccessType.FIELD)
public class Step {

    @XmlID
    @XmlAttribute
    private String id;

    @XmlPath("DetailPanelReference/@reference")
    @XmlIDREF
    // private List<DetailPanel> _detailPanels; // WORKS
    private DetailPanel[] _detailPanels; // See bug:  http://bugs.eclipse.org/399293

}
步骤

@XmlAccessorType(XmlAccessType.FIELD)
public class Step {

    @XmlID
    @XmlAttribute
    private String _id;

}
路径

@XmlAccessorType(XmlAccessType.FIELD)
public class Path {

    @XmlIDREF
    @XmlAttribute
    private Step _reference;

}
了解更多信息


更新


谢谢!我完全错过了你的文章。我扩展了我的问题, 你知道这是否也可能吗?我不想创造 一个只有引用的类,我想把它存储在里面 阶梯类

注意:我是专家组的负责人和成员

如果您使用MOXy作为您的JAXB(JSR-222)提供者,那么您可以利用
@XmlPath
注释来处理您的用例

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

@XmlAccessorType(XmlAccessType.FIELD)
public class Step {

    @XmlID
    @XmlAttribute
    private String id;

    @XmlPath("DetailPanelReference/@reference")
    @XmlIDREF
    // private List<DetailPanel> _detailPanels; // WORKS
    private DetailPanel[] _detailPanels; // See bug:  http://bugs.eclipse.org/399293

}
import javax.xml.bind.annotation.*;
导入org.eclipse.persistence.oxm.annotations.XmlPath;
@XmlAccessorType(XmlAccessType.FIELD)
公共类步骤{
@XmlID
@XmlAttribute
私有字符串id;
@XmlPath(“DetailPanelReference/@reference”)
@XmlIDREF
//私有列表_detailPanels;//工作
private DetailPanel[]\u detailPanels;//请参阅错误:http://bugs.eclipse.org/399293
}
了解更多信息


谢谢!我完全错过了你的文章。我已经扩展了我的问题,你有任何线索,如果这是可能的吗?我不想创建一个只包含引用的类,我希望将其存储在step类中。@MarkvanVelthoven-我已经更新了我的答案,说明如果您使用MOXy作为JAXB(JSR-222)提供程序,如何支持此用例。