FanFan
FanFan

Reputation: 295

Jaxb multiple xml unmarshalling

I have 2 xsd files, one which declares authors and one which declares books and imports authors from the other xsd.

For the author:

    <xs:schema targetNamespace="ro.utcluj.cs.model.author"
               xmlns:xs="http://www.w3.org/2001/XMLSchema"
               elementFormDefault="qualified">

        <xs:element name="author">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="authorName" type="xs:string"/>
                    <xs:element name="authorAge" type="xs:integer"/>
                    <xs:element name="CNP" type="xs:string"/>
                </xs:sequence>
            </xs:complexType>
        </xs:element>

    </xs:schema>

For the books:

<xs:schema targetNamespace="ro.utcluj.cs.model.book"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:author="ro.utcluj.cs.model.author"
           xmlns:tns="ro.utcluj.cs.model.book"
           elementFormDefault="qualified">

    <xs:import namespace="ro.utcluj.cs.model.author" schemaLocation="author.xsd"/>

    <xs:element name="book">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="bookName" type="xs:string"/>
                <xs:element name="title" type="xs:string"/>
                <xs:element ref="author:author"/>
                <xs:element name="genre" type="tns:bookGenre"/>
                <xs:element name="quantity" type="xs:integer"/>
                <xs:element name="price" type="xs:integer"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:simpleType name="bookGenre">
        <xs:restriction base="xs:string">
            <xs:enumeration value="ACTION"/>
            <xs:enumeration value="HISTORY"/>
        </xs:restriction>
    </xs:simpleType>


</xs:schema>

I want to write some xml files and unmarshal them with Jaxb. Can I able to write the authors in one xml and the books in another xml, have Jaxb unmarshal them and then somehow merge them? Or due to the fact that books have a dependency on authors they both must be written in the same xml with their namespaces specified?

Upvotes: 1

Views: 927

Answers (1)

Lazar Lazarov
Lazar Lazarov

Reputation: 2532

I am not sure what you want to do but if you write two *.xml files and you want to unmarshal them you must know which of these should be validated against the "books.xsd" and which against the "authors.xsd".

Lets say you have generated classes from the xsds (if you are not you can read: https://springframework.guru/you-should-use-jaxb-generated-classes-for-restful-web-services/ OR http://www.beingjavaguys.com/2013/04/create-spring-web-services-using-maven.html)

Next you must know how to use the unmarshal/marshal functionality. (http://www.source4code.info/2013/07/jaxb-marshal-unmarshal-with-missing.html)

After you have your Objects from type Book and Author you can use reflection to set field "author" in your Book object(which is as I can see from type Author) to the unmarshaled object from type Author which has been formed from your author.xml.

Another solution can be to take your book.xml and author.xml files as Strings and manualy write a function which can find and replace elements in String. Then you can "put the author in the book" and simply when you unmarshal the String you will have the complete object from type Book with full info(from the author.xml) for the author.

I hope I gave you some directions good luck!

Upvotes: 1

Related Questions