Alignment: Chaotic Java

Saturday, December 24, 2005

XML to POJOs Example - Contacts List

Showing off X2J I created the following XSD example, which is a simple definition for a contact list which can store connections between the contacts:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="">
  <xs:element name="contactList">
        <xs:element name="contact" minOccurs="0" maxOccurs="unbounded">
              <xs:element name="friend-of" type="xs:string" minOccurs="0"
            <xs:attribute name="name" type="xs:string" />
      <xs:attribute name="owner" type="xs:string"/>

You can see I'm using "friend-of" as a list of names. When an XML binding generator creates classes from this, it is likely to produce a String[] getFriendOfArray() or something similar.

Check out the corresponding X2J code:

01 @Element
02 public class Contact {
03   @Attribute
04   @PrimaryKey
05   String getName() {..}
07   @InnerElement
08   @ArrayElement(label=false)
09   Contact[] getFriendOf() {..}
11 }


You can see how I can access the Contact instances who are friends with a certain Contact instance immediately; Also, when an XML conforming to this X2J annotated class is parsed, any contact on the "friend-of" list will be checked for existence as part of validation testing, something that (to my humble knowledge) just wouldn't happen in a regular XSD generated class.

I'd love to hear comments, remarks, bashings, suggestions... Anything.

Related Topics
XML to POJOs alpha 0.1
Possible Advantages of X2J: Primary Keys
X2J on the firing line
Interesting Topics
XML to POJOs Advantages - @Converter


Post a Comment

Links to this post:

Create a Link

<< Home