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

Posted on 2005-04-17
Last Modified: 2010-03-31
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"?>
      <address>123 Foo Street</address>
      <name>Bob Harris</name>

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

<?xml version="1.0" encoding="UTF-8"?>
                  <home_address>123 Foo Street</home_address>
                            <work_address>456 Bar Street</work_address>
      <name>Bob Harris</name>

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


Question by:tomboman
    LVL 15

    Expert Comment

    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.

    Author Comment

    Hi aozarov,

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

    LVL 15

    Accepted Solution

    For the schema generator look at:
    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 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.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
    Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
    Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
    Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now