Store JAXB objects in multiple XML files

Situation

I have been given several XSD files A.xsd

, B.xsd

and C.xsd

that reference each other using XInclude, using IDREF

and ID

without circular dependencies. A.xsd

is my root file in the hierarchy.

With XJB and bind files, I was able to generate coherent Java code from XSD.

After successfully creating Java objects a

, b

and c

, I am trying to marshal them to XML files. This is where I am stuck.

Problem

When marshaling a

to a file a.xml

, b

and are c

not saved anywhere, and a.xml

does not contain links to them.

How to save all objects and links successfully?

Approaches

I have the following approaches, but they cannot be used:

  • Including types b

    and c

    directly in mine A.xsd

    instead of IDREF

    . Doesn't work because I want multiple XML files at the end.
  • Write Java code to navigate the object a

    and find all instances of b

    and c

    . Then marshal everything b

    and c

    spereratly and use XInclude to link to the resulting files. This seems out of place because I don't want my storage engine to know all the internals of all classes. I just want to keep mine a

    and the JAXB marshalling should handle as much of the dependency as possible.

Comparable questions and anders

The following questions are related to this question in the sense that they want to create multiple XML files. But none of them consider the information contained in XSD files and XJC binding files, and therefore requires manipulation of generated Java code, some non-trivial programming overhead, and some kind of duplication of information.

+3


source to share


1 answer


@XmlID

and @XmlIDREF

- facilitating links within a single XML document.



If you have a model generated from multiple XML schemas in multiple packages, then you need to make sure you JAXBContext

create it to keep up with all of these classes. One way to do this is to create JAXBContext

a delimited colon String

for the package names.

JAXBContext.newInstance("com.example.pkg1:com.example.pkg2:com.example.pkg3");

      

+2


source







All Articles