【问题标题】:JAXB: XMLIDRef in attribute?JAXB:属性中的 XMLIDRef?
【发布时间】:2021-10-27 08:46:52
【问题描述】:

我有一个如下所示的 XML:

<MyClass id="abc-123">
   <SomeAttribute idref="cde-456" />
</MyClass>

<SomeOtherClass id="cde-456">

并想使用 JAXB / Moxy 绑定它。到目前为止我能达到的最好成绩是

 @XmlIDREF
 @XmlElement(name="SomeAttribute ")
 //@XmlPath("SomeAttribute /@idref")
 protected SomeOtherClass someAttribute ;

但这只给出&lt;SomeAttribute&gt;cde-456&lt;/SomeAttribute&gt;

任何想法,idref 属性的绑定可能如何工作? @XmlPath 不起作用。

【问题讨论】:

  • 以下解决方案对您有用吗?

标签: java xml jaxb moxy


【解决方案1】:

这应该适合你:

XML:

<root>
    <MyClass id="abc-123">
        <SomeAttribute idref="cde-456"/>
    </MyClass>
    <SomeOtherClass id="cde-456"/>
</root>

根:

@XmlRootElement(name = "root")
@XmlAccessorType(XmlAccessType.FIELD)
@Data
public class Root {
    private MyClass MyClass;
    private SomeOtherClass SomeOtherClass;
}

我的班级:

@XmlAccessorType(XmlAccessType.FIELD)
@Data
public class MyClass {
    @XmlAttribute
    private String id;
    private SomeAttribute SomeAttribute;
}

一些属性:

@XmlAccessorType(XmlAccessType.FIELD)
@Data
public class SomeAttribute {
    @XmlAttribute
    private String idref;
}

SomeOtherClass:

@XmlAccessorType(XmlAccessType.FIELD)
@Data
public class SomeOtherClass {
    @XmlAttribute
    private String id;
}

主要:

public class Main {
    public static void main(String[] args) throws JAXBException, XMLStreamException {
        final InputStream inputStream = Main.class.getClassLoader().getResourceAsStream("sample.xml");
        final XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
        final Unmarshaller unmarshaller = JAXBContext.newInstance(Root.class).createUnmarshaller();
        final Root root = unmarshaller.unmarshal(xmlStreamReader, Root.class).getValue();
        System.out.println(root.toString());

        Marshaller marshaller = JAXBContext.newInstance(Root.class).createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.marshal(root, System.out);
    }
}

输出:

Root(MyClass=MyClass(id=abc-123, SomeAttribute=SomeAttribute(idref=cde-456)), SomeOtherClass=SomeOtherClass(id=cde-456))
<root>
   <MyClass id="abc-123">
      <SomeAttribute idref="cde-456"/>
   </MyClass>
   <SomeOtherClass id="cde-456"/>
</root>

【讨论】:

    【解决方案2】:

    现在我解决了一个小例子,现实世界还有待完成:-)

    1. 使用 Moxy 并将 jaxb.properties 添加到该包的资源中。 javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
    2. 根类
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Root {
        @XmlElement(name="MyClass")
        MyClass myClass;
    
        @XmlElement(name="SomeOtherClass")
        List<SomeOtherClass> someOtherClasses = new ArrayList<>();
    }
    
    1. 我的班级
    @XmlAccessorType(XmlAccessType.FIELD)
    public class MyClass {
        @XmlID
        @XmlPath("@id")
        String id = UUID.randomUUID().toString();
        @XmlIDREF
        @XmlPath("SomeAttribute/@idref")
        SomeOtherClass someAttribute;
        @XmlElement(name = "Name")
        String name;
    }
    
    1. SomeOtherClass
    @XmlSeeAlso({DerivedClass.class})
    @XmlAccessorType(XmlAccessType.FIELD)
    public class SomeOtherClass {
        @XmlID
        @XmlPath("@id")
        String id = UUID.randomUUID().toString();
        @XmlElement(name="Name")
        String name;
    }
    
    1. 派生类
    @XmlType(name = "DerivedClass")
    @XmlAccessorType(XmlAccessType.FIELD)
    public class DerivedClass extends SomeOtherClass{
        String inDerived;
    }
    

    现在我可以加载和写这个了:

    <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
       <MyClass id="64c80ae9-1d2b-4b79-98d1-0dfe0915f6a2">
          <SomeAttribute idref="335f0edb-6873-4acc-8c49-2c92cf6b3fcd"/>
          <Name>CDE</Name>
       </MyClass>
       <SomeOtherClass id="23a0dee0-fe06-45cc-a280-efb4a4bad345">
          <Name>ABC</Name>
       </SomeOtherClass>
       <SomeOtherClass id="335f0edb-6873-4acc-8c49-2c92cf6b3fcd" xsi:type="DerivedClass">
          <Name>FGH</Name>
          <inDerived>IJK</inDerived>
       </SomeOtherClass>
    </root>
    

    【讨论】:

      猜你喜欢
      • 2012-05-07
      • 1970-01-01
      • 2010-10-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多