3

Situation

I'm given multiple XSD-files A.xsd, B.xsd and C.xsd, which reference each others elements via XInclude using IDREF and ID without cyclic dependencies. A.xsd is my root file in the hierarchy.

With XJB and binding files I managed it to generate coherent Java Code from the XSDs.

After successfully creating Java objects a, b and c, I'm trying to marshal them into XML files. This is where i get stuck.

Problem

When marshaling a into a file a.xml, b and c are stored nowhere and a.xml contains no references to them.

How do I store all objects plus references successfully?

Approaches

I have the following approaches at hand, but they are not sutable:

  • Including the types of b and c directly in my a.xsd instead of using IDREF. Doesn't work because I want multiple XML files at the end.
  • Write Java code to navigate through the a object and find all instances of b and c. Then marshal all bs and cs spereratly and use XInclude to reference the resulting files. This seems inapproprite, because I don't want my storage mechanism to know all the internals of all classes. I just want to store my a and JAXB marshalling shall handle storing the dependencies on its own as far as possible.

Comparable Questions and Anwsers

The following questions are related to this question, in the point that they want to produce multiple XML files. But none of them considers the information given in the XSD-files and XJC-binding-files and thus require manipulation of the generated java code, some non-trivial programming overhead and some sort of information duplication.

Community
  • 1
  • 1
Waog
  • 7,127
  • 5
  • 25
  • 37

1 Answers1

2

@XmlID & @XmlIDREF is to facilitate references within a single XML document.

If you have a model generated from multiple XML schemas into multiple packages then you need to make sure the JAXBContext is created to be aware of all these classes. One way to do this is creating the JAXBContext on a colon delimited String of package names.

JAXBContext.newInstance("com.example.pkg1:com.example.pkg2:com.example.pkg3");
bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • Thanks, but I did this already, with the root classes instead of the packages. Now that I use the packages the referenced ID is stored properly in ``a``, but the referenced objects (i.e. ``b`` and ``c``) are still stored nowhere. I don't even know how to properly specify the locations where they shall be stored. – Waog Sep 02 '14 at 15:47
  • @Waog - Root classes isn't the same as package names. You can use `ObjectFactory` classes though. – bdoughan Sep 02 '14 at 15:50
  • Good to know. But it still doesn't solve my actual problem, which is that the referenced objects ``b`` and ``c`` are not stored at all. – Waog Sep 02 '14 at 15:57
  • @Waog - Not sure if you looked through the `@XmlIDREF` example I linked to? You need to make sure all objects are reachable through a nested relationship (i.e. `@XmlElement`). The first step is to get them to appear in the XML, the next is to get them referenced vi IDREF. – bdoughan Sep 02 '14 at 16:03
  • Yep I looked through the example. But the example ends with all data contained in one single XML file. My question is, how to store the data in multiple XML files. In your example that would be a second XML file containing `` ...`` which shall be correctly referenced from your first XML file. – Waog Sep 02 '14 at 16:13
  • @Waog - That's what JAXB does it produces & consumes 1 XML document. `IDREF` is a mechanism for linking within a document. Even XML Schema defines what a single XML document looks like (even though multiple XML Schemas may be involved. The following may be what you are looking for: http://stackoverflow.com/questions/5319024/using-jaxb-to-cross-reference-xmlids-from-two-xml-files/5327425#5327425 – bdoughan Sep 02 '14 at 16:20
  • I edited the question and added that I currently use XInclude to reference multiple XML files. I also read some answers of you, proposing the XmlAdapter. I'll try it, but it seems a like a lot of overhead to me. I also added my concerns with this approach to my question. – Waog Sep 03 '14 at 13:50