XML.com: XML From the Inside Out
oreilly.comSafari Bookshelf.Conferences.

advertisement

XML Data-Binding: Comparing Castor to .NET
by Niel Bornstein | Pages: 1, 2, 3, 4

Castor also generated isValid() and validate() methods, which use the Schema to validate any data when it is marshaled. And it has created marshal() and unmarshal() methods.

Now that we've generated the code, it's a simple matter to create data. Here's a short program which instantiates a few of the relevant objects and serializes them to an XML file.

package org.dogshow;

import java.io.FileWriter;
import java.util.Date;

public class MakeDogShow {
    public static void main(String [] args) {
	try {
	    Dog [] dog = new Dog [2];
	    dog[0] = new Dog();
	    dog[0].setId(1);
	    dog[0].setName("Wil-Orion's Angus Highlander");
	    
	    dog[1] = new Dog();
	    dog[1].setId(2);
	    dog[1].setName("LenLear's Webmaster");
	    
	    Breed breed = new Breed();
	    breed.setId(1);
	    breed.setName("English Springer Spaniel");
	    breed.setDog(dog);

	    Judge judge = new Judge();
	    judge.setId(1);
	    judge.setFirstName("John");
	    judge.setLastName("Smith");
	    
	    ShowRing showRing = new ShowRing();
	    showRing.setId(1);
	    showRing.setName("1");
	    
	    Judging [] judging = new Judging [] {new Judging()};
	    judging[0].setJudge(judge);
	    judging[0].setBreed(breed);
	    judging[0].setShowRing(showRing);
	    judging[0].setDateTime(new Date());
	    
	    Show show = new Show();
	    show.setId(1);
	    show.setName("O'Reilly Invitational Dog Show");
	    show.setJudging(judging);
	    
	    FileWriter writer = new FileWriter("show.xml");
	    show.marshal(writer);
	    writer.close();
	} catch (Exception e) {
	    e.printStackTrace(System.err);
	}
    }
}

We can compile all our code and run MakeDogShow to produce the following XML file:

<?xml version="1.0" encoding="UTF-8"?>
<Show><id>1</id><name>O'Reilly Invitational 
Dog Show</name><judging><breed><id>1</id>
<name>English Springer Spaniel</name><dog>
<id>1</id><name>Wil-Orion's Angus Highlander</name>
</dog><dog><id>2</id><name>LenLear's 
Webmaster</name></dog></breed><judge><id>1</id>
<firstName>John</firstName><lastName>Smith</lastName>
</judge><showRing><id>1</id><name>1</name>
</showRing><dateTime>2002-07-13T13:51:37.112-04:00
</dateTime></judging></Show>

They Thought of That

The .NET Framework SDK ships with a handy little tool called xsd, the W3C XML Schema Definition Tool, which does for .NET what Castor's SourceGenerator does for Java. The following command line generates the C# source code for our DogShow schema:

xsd /c /l:cs DogShow.xsd

And here's the single generated source file:

//---------------------------------------------------------
// <autogenerated>
//     This code was generated by a tool.
//     Runtime Version: 1.0.3705.209
//
//     Changes to this file may cause incorrect behavior and 
//     will be lost if the code is regenerated.
// </autogenerated>
//---------------------------------------------------------

// 
// This source code was auto-generated by xsd, Version=1.0.3705.209.
// 
using System.Xml.Serialization;


/// <remarks/>
[System.Xml.Serialization.XmlRootAttribute("Show", 
    Namespace="", IsNullable=false)]
public class ShowType {
    
    /// <remarks/>
    public long id;
    
    /// <remarks/>
    public string name;
    
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("judging")]
    public JudgingType[] judging;
}

/// <remarks/>
[System.Xml.Serialization.XmlRootAttribute("Judging", 
    Namespace="", IsNullable=true)]
public class JudgingType {
    
    /// <remarks/>
    public BreedType breed;
    
    /// <remarks/>
    public JudgeType judge;
    
    /// <remarks/>
    public ShowRingType showRing;
    
    /// <remarks/>
    public System.DateTime dateTime;
}

/// <remarks/>
[System.Xml.Serialization.XmlRootAttribute("Breed", 
    Namespace="", IsNullable=true)]
public class BreedType {
    
    /// <remarks/>
    public long id;
    
    /// <remarks/>
    public string name;
    
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("dog")]
    public DogType[] dog;
}

/// <remarks/>
[System.Xml.Serialization.XmlRootAttribute("Dog", 
    Namespace="", IsNullable=true)]
public class DogType {
    
    /// <remarks/>
    public long id;
    
    /// <remarks/>
    public string name;
}

/// <remarks/>
[System.Xml.Serialization.XmlRootAttribute("ShowRing", 
    Namespace="", IsNullable=true)]
public class ShowRingType {
    
    /// <remarks/>
    public long id;
    
    /// <remarks/>
    public string name;
}

/// <remarks/>
[System.Xml.Serialization.XmlRootAttribute("Judge", 
    Namespace="", IsNullable=true)]
public class JudgeType {
    
    /// <remarks/>
    public long id;
    
    /// <remarks/>
    public string firstName;
    
    /// <remarks/>
    public string lastName;
}

We should note that the generated code contains attributes which we haven't seen before in this series. These are not the same thing as XML attributes; in C#, attributes are special constructs that decorate sections of code, such as assemblies, modules, types, members, return values, and parameters. They define additional information about the section they're attached to. For example, this attribute is attached to the ShowType type:

[System.Xml.Serialization.XmlRootAttribute("Show", Namespace="", IsNullable=false)]

XmlRootAttribute indicates that this type's element is called "Show", that it has no namespace, and that it may not be null (nillable, in W3C XML Schema).

You'll also notice that the generated code in C# is much smaller than that generated for Java. This is because the JavaBean accessor methods have no equivalent in the C# code, and the marshal and unmarshal methods are unnecessary in C#, as we shall see shortly.

Pages: 1, 2, 3, 4

Next Pagearrow