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="http://www.w3.org/2001/XMLSchema">
  <xs:element name="contactList">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="contact" minOccurs="0" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="friend-of" type="xs:string" minOccurs="0"
       maxOccurs="unbounded"/>
            </xs:sequence>
            <xs:attribute name="name" type="xs:string" />
          </xs:complexType>
        </xs:element>
      </xs:sequence>
      <xs:attribute name="owner" type="xs:string"/>
    </xs:complexType>
  </xs:element>
</xs:schema>



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() {..}
06   
07   @InnerElement
08   @ArrayElement(label=false)
09   Contact[] getFriendOf() {..}
10 
11 }



Java2html




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

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home