Jince Martin
Jince Martin

Reputation: 311

Mapping of XML data to/from a java object(s) using Dozer

Test mapping of XML data to/from a java object(s) using dozer.

That is, given a sample XML file , map this to new java classes with appropriate get/set methods for the various elements

A sample XML file is given below:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<cXML payloadID="1272549644772.1050674118.000000197@2Vmg9c/TnwT1ZqGdAuiHQvbDmNc=" timestamp="2010-04-29T07:00:44-07:00" version="1.2.020">
        <Header>
                <From>
                        <Credential domain="Test_Code">
                                <Identity>ID_SYSTEM</Identity>
                        </Credential>
                </From>
                <To>
                        <Credential domain="Test_Code">
                                <Identity>ID_SYSTEM</Identity>
                        </Credential>
                </To>
                <Sender>
                        <Credential domain="AribaNetworkUserId">
                                <Identity>[email protected]</Identity>
                                <SharedSecret>sigma123</SharedSecret>
                        </Credential>
                        <UserAgent>Sender Application 1.0</UserAgent>
                </Sender>
        </Header>
</cXML>

I need to map this to new java class

Please help me.

Upvotes: -2

Views: 3828

Answers (2)

shashi
shashi

Reputation: 400

sample dozerMapping.xml file. http://dozer.sourceforge.net/schema/beanmapping.xsd">

<mapping map-id="a">
    <class-a>com.java.beans.Employee</class-a>
    <class-b>com.java.beans.Address</class-b>
    <field>
        <a>streetNumber</a>
        <b>streetNumber</b>
    </field>
    <field>
        <a>city</a>
        <b>city</b>
    </field>
    <field>
        <a>state</a>
        <b>state</b>
    </field>
    <field>
        <a>country</a>
        <b>country</b>
    </field>
</mapping>

Upvotes: 0

Jordi Castilla
Jordi Castilla

Reputation: 26961

Basic dozer mapping is really easy, if the attributes have same name / type it will map auto:

<mapping>
    <class-a>org.dozer.vo.TestObject</class-a>
    <class-b>org.dozer.vo.TestObjectPrime</class-b>
</mapping>

If you have some different named attributes:

<mapping> 
    <class-a>org.dozer.vo.TestObject</class-a>
    <class-b>org.dozer.vo.TestObjectPrime</class-b>   
    <field>
      <a>one</a>
      <b>onePrime</b>
    </field>
</mapping>  

For more info you can find in the user guide and examples of XML mappings

Upvotes: 1

Related Questions