English 中文(简体)
日本宇宙航空研究开发机构对自我参照物体和低预测的麻烦
原标题:JAXB trouble with self-referential objects and downcasting
  • 时间:2011-11-24 12:31:22
  •  标签:
  • jaxb

I m在日本宇宙航空研究开发机构有退学问题,有自我参照的班级。

我的发言:

@XmlRootElement
class IdentifiableObject {
  @XmlID
  @XmlAttribute
  String id;

  @XmlAttribute
  String name;
}

@XmlRootElement
class Node extends IdentifiableObject {
  @XmlElement
  @XmlJavaAdapter(SimpleAdapterThatJustDowncastsToIdentifiableObject.class)
  Node parent;

  @XmlElement
  String aField;
}

我已用大量其他物体这样做,并做了罚款。 但是,当我使用指自己的一个类别时,它就没有工作。

我是否能够做些什么来解决这个问题? 我知道利用XmlID/XmlIDREF友好解决了这一问题,但我真的希望不仅仅是一个简单的回想(我希望和<>。 姓名不详

为了澄清这一点,我知道:

<nodes>
    <node id="49ad1cb6-f6fe-47f9-a544-4a1c6337c4a5" name="Node 1">
        <aField>This is Node 1</aField>
    </node>
    <node id="0a1d1895-49e1-4079-abc1-749c304cc5a2" name="Node 2">
        <parent xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="node" id="49ad1cb6-f6fe-47f9-a544-4a1c6337c4a5" name="Node 1">
            <aField>This is Node 1</aField>
        </parent>
        <aField>This is Node 2</aField>
    </node>
</nodes>

这是我想要的:

<nodes>
    <node id="49ad1cb6-f6fe-47f9-a544-4a1c6337c4a5" name="Node 1">
        <aField>This is Node 1</aField>
    </node>
    <node id="0a1d1895-49e1-4079-abc1-749c304cc5a2" name="Node 2">
        <parent id="49ad1cb6-f6fe-47f9-a544-4a1c6337c4a5" name="Node 1"/>
        <aField>This is Node 2</aField>
    </node>
</nodes>

UPDATE: 仅在此注一,化学剂实际上就是正确的。 因此,这或许是日本宇宙航空研究开发机构的一个丑闻。

Regards, Morten

最佳回答

您不需要<代码>XmlAdapter,以备您使用。 您可通过打上<代码>IdentifiableObject等同@Xml Transient :

Nodes

package forum8257098;

import java.util.List;

import javax.xml.bind.annotation.*;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Nodes {

    @XmlElement(name="node")
    private List<Node> nodes;

}

Node

package forum8257098;

import javax.xml.bind.annotation.*;

@XmlRootElement
public class Node extends IdentifiableObject {

    @XmlElement
    private Node parent;

    @XmlElement
    private String aField;

}

IdentifiableObject

package forum8257098;

import javax.xml.bind.annotation.*;

@XmlTransient
public class IdentifiableObject {

    @XmlID
    @XmlAttribute
    private String id;

    @XmlAttribute
    private String name;

}

Demo

package forum8257098;

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Nodes.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum8257098/input.xml");
        Nodes nodes = (Nodes) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(nodes, System.out);
    }

}

http://www.ohchr.org。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<nodes>
    <node name="Node 1" id="49ad1cb6-f6fe-47f9-a544-4a1c6337c4a5">
        <aField>This is Node 1</aField>
    </node>
    <node name="Node 2" id="0a1d1895-49e1-4079-abc1-749c304cc5a2">
        <parent name="Node 1" id="49ad1cb6-f6fe-47f9-a544-4a1c6337c4a5"/>
        <aField>This is Node 2</aField>
    </node>
</nodes>

<>更多信息>

问题回答

暂无回答




相关问题
JAXB minOccurs=0. Element exists or not?

I have an XML schema: <xsd:element name="Person"> <xsd:complexType> <xsd:sequence> <xsd:element name="name" type="xsd:string" /> <xsd:element name="lat" type="xsd:...

How to stream large Files using JAXB Marshaller?

The Problem I m facing is how to marshall a large list of objects into a single XML File, so large I can not marshall the complete list in one step. I have a method that returns these objects in ...

Jaxb2Marshaller and primitive types

Is it possible to create a web service operation using primitive or basic Java types when using the Jaxb2Marschaller in spring-ws? For example a method looking like this: @Override @PayloadRoot(...