Java – Using SIMPLE to Store Business Rule
By admin on Sep 20, 2007 in Java, Programming
In my previous blog, Using XStream to Store Business Rule, I showed you how to use XStream to store business rule. Here I rewrote the code using SIMPLE. SIMPLE is a high performance XML serialization and configuration framework for Java.
Below is the code to serialize and deserialize the objects.
PersonRule person = new PersonRule();person.setName(“Jackson”);
person.setAge(32);
person.setOccupation(“Programmer”);
List<Account> accounts = new ArrayList<Account>(2);
Account account = new Account();
account.setAccountNo(“1234”);
account.setPassword(“pass1”);
accounts.add(account);
account = new Account();
account.setAccountNo(“4567”);
account.setPassword(“pass2”);
accounts.add(account);
person.setAccounts(accounts);
String xmlString = person.toXML();
System.out.println(xmlString);
PersonRule newPerson = (PersonRule)BaseRule.fromXML(xmlString, PersonRule.class);
System.out.println(newPerson.getName());
System.out.println(newPerson.getAge());
System.out.println(newPerson.getOccupation());
for (Account acc : newPerson.getAccounts()) {
System.out.println(acc.getAccountNo());
System.out.println(acc.getPassword());
}
The serialized XML string is as below:
<Person>
<name>Jackson</name>
<age>32</age>
<occupation>Programmer</occupation>
<accounts class=”java.util.ArrayList”>
<Account>
<accountNo>1234</accountNo>
<password>pass1</password>
</Account>
<Account>
<accountNo>4567</accountNo>
<password>pass2</password>
</Account>
</accounts>
</Person>
Jim McGregor | Sep 21, 2007 | Reply
The SIMPLE implementation looks a lot cleaner than the XStream implementation.
thoughtworks | Sep 21, 2007 | Reply
Yes. Personally I preferred it. Both XStream and Simple are used in quite a number of other open source project, e.g. XStream is used in JBoss Rule