How to create XML with multiple levels of depth using Castor?

I know how to creat an XML file using Castor's Marshaller.marshal(obj, file) method (where obj stores the person detail, and file specifis the name of xml file). but the xml created is only 1 level, as shown below:

<?xml version="1.0" encoding="UTF-8"?>
<person>
      <ssn>222-222-2222</ssn>
      <address>123 Foo Street</address>
      <work-phone>(123)123-1234</work-phone>
      <home-phone>(123)123-1234</home-phone>
      <email>bob@harris.org</email>
      <name>Bob Harris</name>
</person>

does anyone know how to create xml file with more levels, for example,

<?xml version="1.0" encoding="UTF-8"?>
<person>
                <address>
                  <home_address>123 Foo Street</home_address>
                            <work_address>456 Bar Street</work_address>
                </address>
                <phone>
                  <work_phone>(123)123-1234</work_phone>
                  <home_phone>(123)123-1234</home_phone>
                </phone>
      <email>bob@harris.org</email>
      <name>Bob Harris</name>
</person>


Is it possible to use castor methods? if not, what other methods are there?

thansk


tombomanAsked:
Who is Participating?
 
aozarovCommented:
For the schema generator look at: http://www.castor.org/sourcegen.html
Basically you create an XML schema that describe the XML structure you are looking for and
then you run this tool with the given schema as input. The tool will generate the Java objects
for you (like Person and Address). Those object will have marshall and unmarshall functions that you
can use.

This link http://www.castor.org/xml-mapping.html describe how to use XML mapping file
to provide more infomation to the castor marshelling/unmarshalling framework regarding your objects.

You should use one or the other.
0
 
aozarovCommented:
You can do that by either generate your java classes (that you want to marshel) using castor schema generator
(and make your schema have the required levels) or defing your own xml mapping which will use person and address classes.
0
 
tombomanAuthor Commented:
Hi aozarov,

Can you describe the steps in detail? or do you know any website that shows this in detail?

Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.