Nirali Gandhi
Nirali Gandhi

Reputation: 43

how to marshall more than one pojo's in a single xml?

Is it possible to marshall all pojo (of same type) in a single xml using o/x mappers? like I want to generate the following xml file

<xml>
<record1>
<id>1</id>
<name>abc</name>
</record1>
<record2>
<id>2</id>
<name>xyz</name>
</record2>
</xml>

here record1 and record 2 are same type of objects.Means I want to write first record1 object in xml file than record2 object in that same xml file.

Upvotes: 1

Views: 629

Answers (3)

Andreas Dolk
Andreas Dolk

Reputation: 114797

Spring documentation clearly says:

Within the field of O/X mapping, a marshaller is responsible for serializing an object (graph) to XML.

So the direct answer is no. We marshal one object to one xml document. If you need more then one instance in a single document, you'll have to implement some sort of wrapper class. A class containing a simple collection (a list or a set) which stores your marshallables and providing some get/add methods should be sufficient.

Upvotes: 0

Talha Ahmed Khan
Talha Ahmed Khan

Reputation: 15453

How about composition. A Pojo class contains other pojo's objects as xml entity.

Upvotes: 0

Adeel Ansari
Adeel Ansari

Reputation: 39907

As Merlyn Morgan-Graham said as a comment to your question, You can aggregate them into a separate class, and serialize that one.

Upvotes: 2

Related Questions