|
GenerationOfXml
how to generate XML from models
IntroductionOften 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 modelHere is code that will provide stock XML from a model: Person p = (Person)Person.findById(1); String xml = p.toXml(2, true); The XML produced will look something like this: <?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>this of course depends on the table structure. 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 XMLA variation on the example above is to provide a list of attributes that you are interested in, so that only these attributes are included: 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: <?xml version="1.0" encoding="UTF-8" ?>
<person>
<dob>1934-12-01</dob>
<last_name>Smith</last_name>
</person>Inclusion of dependenciesWhen a model is has relationships, the generated XML will loop through them to include their XML into the parent XML as well: List<User> personList = User.findAll().orderBy("id").include(Address.class);
User u = personList.get(0);
String xml = u.toXml(2, true);result: <?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 resultsetGenerating XML from a LazyList is equally easy: LazyList<User> personList = User.findAll().orderBy("id").include(Address.class);
String xml = personList.toXml(2, true);An example of generated XML: <?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>
as you can see, the two users were generated. Since the include was used, the corresponding children from the ADDRESSES table were queried too for their XML. Back to Features | |
Any way to do this in the other direction -- to take the output XML and put together a model for it? In particular it would be really nice if the 'Model.fromXml(String xmldoc)' would connect to the DB and grab the 'live' record if it's available, and perform a new() if not...
this is not supported at the time. One of the reasons is that ActiveJDBC almost has no dependencies on third party libraries, and adding support for hydration from XML/Json will invariably require this. However, I feel this is a good thing to have, will log this as an enhancement for future development