1

Saying my xml should look like the following:

<!-- even I move <A id="2">...</A> here, still has this error-->
<A id="1">
    ...
       ... (many levels of nested element)
       <C>
           <B ref="2"/>
       </C> 
    ...
</A>
<A id="2">...</A>

So I define the following xsd file:

<xs:complexType name="A" abstract="true">
    <xs:attribute name="id" type="xs:ID" use="required"/>
    ...
</xs:complexType>
<xs:complexType name="B">
    <xs:attribute name="ref" type="xs:IDREF" use="required"/>
</xs:complexType>

But When I want to parse this xml file using JAXB, It always complains that <B ref="2"/> has a error of:

Undefined ID "".

My code:

public class A {
  private String id;

  @XmlAttribute
  @XmlID
  public String getId() {
    return id;
  }

  ...
}

public class C {
  private A b;

  @XmlIDREF
  @XmlElement
  public A getB() {
    return b;
  }
}

So what's the problem?

I have already read this blog, this blog, question, not found any clue of this error.

Community
  • 1
  • 1
Tony
  • 5,972
  • 2
  • 39
  • 58

1 Answers1

0

Through one whole days search and trials, I finally found the error and solve it:

What class C actually has is not directly a A's ref, but a class B which only has a ref. So the solution is clear now:

Either to change the xsd to:

<xs:complexType name="C">
    <xs:choice>
        <xs:element name="B" type="xs:IDREF"/>
    </xs:choice>
</xs:complexType>

Or to change the java code:

public class C {
  private B b;
  ...
}
// addint a B class which contains the reference
public class B {
  private A b;
  @XmlIDREF
  @XmlElement
  public A getB() {
    return b;
  }
}

If you are interested how I solve this, you can refer to my blog.

Tony
  • 5,972
  • 2
  • 39
  • 58