Often developers need to generate XML from models. In more complicated situation, they probably would write some XML generation code. However under simple condition, classes Model and LazyList already provide the basics.

Generate simple XML from a model

Here is code that will provide stock XML from a model:

1
2
Person p  = (Person)Person.findById(1);
String xml = p.toXml(2, true);

The XML produced will look something like this:

1
2
3
4
5
6
7
8
9
10
<?xml version="1.0" encoding="UTF-8" ?>
  <person>
    <updated_at>2010-11-09 19:02:11.0</updated_at>
    <created_at>2010-11-09 19:02:11.0</created_at>
    <dob>1934-12-01</dob>
    <last_name>Smith</last_name>
    <graduation_date>1954-12-01</graduation_date>
    <name>John</name>
    <id>1</id>
  </person>

The first parameter (2) is a number of spaces for indent, and second whether to add an XML declaration or not.

Include attributes into generated XML

A variation on the example above is to provide a list of attributes that you are interested in, so that only these attributes are included:

1
2
Person p  = (Person)Person.findById(1);
String xml = p.toXml(2, true, "last_name", "dob");

The resulting XML will have nothing but the attributes specified:

1
2
3
4
5
<?xml version="1.0" encoding="UTF-8" ?>
<person>
    <dob>1934-12-01</dob>
    <last_name>Smith</last_name>
</person>

Inclusion of dependencies

When a model has relationships, the generated XML will loop through them to include their XML into the parent XML as well:

1
2
3
List<User> personList = User.findAll().orderBy("id").include(Address.class);
User u = personList.get(0);
String xml = u.toXml(2, true);

result:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="UTF-8" ?>
  <user>
    <addresses>
      <address>
        <user_id>1</user_id>
        <address2>apt 31</address2>
        <state>IL</state>
        <zip>60606</zip>
        <address1>123 Pine St.</address1>
        <id>1</id>
        <city>Springfield</city>
      </address>
      <address>
        <user_id>1</user_id>
   ...
    truncated for brevity
   ...
    </addresses>
    <email>mmonroe@yahoo.com</email>
    <last_name>Monroe</last_name>
    <id>1</id>
    <first_name>Marilyn</first_name>
  </user>

Generate XML from a resultset

Generating XML from a LazyList is equally easy:

1
2
LazyList<User> personList = User.findAll().orderBy("id").include(Address.class);
String xml = personList.toXml(2, true);

An example of generated XML:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?xml version="1.0" encoding="UTF-8" ?>
<users>
  <user>
    <addresses>
      <address>
        <user_id>1</user_id>
        <address2>apt 31</address2>
        <state>IL</state>
        <zip>60606</zip>
        <address1>123 Pine St.</address1>
        <id>1</id>
        <city>Springfield</city>
      </address>
      <address>
        <user_id>1</user_id>
        <address2>apt 21</address2>
        <state>IL</state>
        <zip>60606</zip>
        <address1>456 Brook St.</address1>
        <id>2</id>
        <city>Springfield</city>
      </address>
      <address>
        <user_id>1</user_id>
        <address2>apt 32</address2>
        <state>IL</state>
        <zip>60606</zip>
        <address1>23 Grove St.</address1>
        <id>3</id>
        <city>Springfield</city>
      </address>
    </addresses>
    <email>mmonroe@yahoo.com</email>
    <last_name>Monroe</last_name>
    <id>1</id>
    <first_name>Marilyn</first_name>
  </user>
  <user>
    <addresses>
      <address>
        <user_id>2</user_id>
        <address2>apt 34</address2>
        <state>IL</state>
        <zip>60606</zip>
        <address1>143 Madison St.</address1>
        <id>4</id>
        <city>Springfield</city>
      </address>
      <address>
        <user_id>2</user_id>
        <address2>apt 35</address2>
        <state>IL</state>
        <zip>60606</zip>
        <address1>153 Creek St.</address1>
        <id>5</id>
        <city>Springfield</city>
      </address>
      <address>
        <user_id>2</user_id>
        <address2>apt 36</address2>
        <state>IL</state>
        <zip>60606</zip>
        <address1>163 Gorge St.</address1>
        <id>6</id>
        <city>Springfield</city>
      </address>
      <address>
        <user_id>2</user_id>
        <address2>apt 37</address2>
        <state>IL</state>
        <zip>60606</zip>
        <address1>173 Far Side.</address1>
        <id>7</id>
        <city>Springfield</city>
      </address>
    </addresses>
    <email>jdoe@gmail.com</email>
    <last_name>Doe</last_name>
    <id>2</id>
    <first_name>John</first_name>
  </user>
</users>

The two users were generated. Since the include was used, the corresponding children from the ADDRESSES table were queried too for their XML.


How to comment

The comment section below is to discuss documentation on this page.

If you have an issue, or discover bug, please follow instructions on the Support page