In order not to have side effects on its argument, add(FooType foo)
methods add a copy of foo rather than the foo you supply. Subsequent
modifications of foo will then have no effect on the copy. The
particular change would be changing the parent xml object. I find
using add much more convenient.
thanks
david jencks
On Oct 17, 2005, at 4:21 PM, rabidgremlin wrote:
Hi All,
I have a curious problem. I am porting some existing code that uses
another XML binding tool. This code uses uses "set" methods to
associate different objects together. The port was thus pretty easy as
there was a close correlation between the structure of the old code
and the code that uses XmlBeans.
However when I do a test run, my XML is not being created correctly.
After a couple of tests I have determined that the order in which you
do the "sets" are important. This seems very odd to me (not to mention
very fragile and error prone).
I have a small example:
With the following XSD:
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://testschema" xmlns:TestSchema="
http://testschema">
<complexType name="Song">
<sequence>
<element name="Title" type="string"></element>
<element name="length" type="duration"></element>
</sequence>
</complexType>
<element name="Album">
<complexType>
<sequence>
<element name="Songs" type="TestSchema:Song"
minOccurs="1" maxOccurs="unbounded">
</element>
</sequence>
<attribute name="Title" type="string"
use="required"></attribute>
<attribute name="Year" type="string"
use="required"></attribute>
</complexType>
</element>
</schema>
and the following code:
import org.apache.xmlbeans.GDuration;
import testschema.AlbumDocument;
import testschema.Song;
import testschema.AlbumDocument.Album;
public class TestSchema
{
public static void main(String[] args)
{
try
{
testWithAdd();
testWithSet1();
testWithSet2();
}
catch(Exception e)
{
System.out.println("Oops -> " + e);
}
}
public static void testWithAdd()throws Exception
{
AlbumDocument doc = AlbumDocument.Factory.newInstance();
Album album = doc.addNewAlbum();
album.setTitle("FloodLand");
album.setYear("1990");
Song tempSong = album.addNewSongs();
tempSong.setTitle("Dominion/Mother Russia");
tempSong.setLength( new GDuration(1,2,4,5,10,12,15,null) );
tempSong = album.addNewSongs();
tempSong.setTitle("Flood I");
tempSong.setLength( new GDuration(1,2,2,5,10,12,15,null) );
System.out.println("Add: isValid=" + doc.validate());
System.out.println("Add: " + doc.xmlText());
}
public static void testWithSet1()throws Exception
{
AlbumDocument doc = AlbumDocument.Factory.newInstance();
Album album = Album.Factory.newInstance();
// NOTE SET HERE
doc.setAlbum(album);
album.setTitle("FloodLand");
album.setYear("1990");
Song[] songs = new Song[2];
songs[0] = Song.Factory.newInstance();
songs[0].setTitle("Dominion/Mother Russia");
songs[0].setLength( new GDuration(1,2,4,5,10,12,15,null) );
songs[1] = Song.Factory.newInstance();
songs[1].setTitle("Flood I");
songs[1].setLength( new GDuration(1,2,2,5,10,12,15,null) );
album.setSongsArray(songs);
System.out.println("Set1: isValid=" + doc.validate());
System.out.println("Set1: " + doc.xmlText());
}
public static void testWithSet2()throws Exception
{
AlbumDocument doc = AlbumDocument.Factory.newInstance();
Album album = Album.Factory.newInstance();
album.setTitle("FloodLand");
album.setYear("1990");
Song[] songs = new Song[2];
songs[0] = Song.Factory.newInstance();
songs[0].setTitle("Dominion/Mother Russia");
songs[0].setLength( new GDuration(1,2,4,5,10,12,15,null) );
songs[1] = Song.Factory.newInstance();
songs[1].setTitle("Flood I");
songs[1].setLength( new GDuration(1,2,2,5,10,12,15,null) );
album.setSongsArray(songs);
// NOTE SET HERE
doc.setAlbum(album);
System.out.println("Set2: isValid=" + doc.validate());
System.out.println("Set2: " + doc.xmlText());
}
}
I get the following results:
Add: isValid=true
Add: <tes:Album Title="FloodLand" Year="1990"
xmlns:tes="http://testschema"><Songs><Title>Dominion/Mother
Russia</Title><length>P2Y4M5DT10H12M15S</length></
Songs><Songs><Title>Flood
I</Title><length>P2Y2M5DT10H12M15S</length></Songs></tes:Album>
Set1: isValid=false
Set1: <tes:Album xmlns:tes="http://testschema"/>
Set2: isValid=true
Set2: <tes:Album Title="FloodLand" Year="1990"
xmlns:tes="http://testschema"><Songs><Title>Dominion/Mother
Russia</Title><length>P2Y4M5DT10H12M15S</length></
Songs><Songs><Title>Flood
I</Title><length>P2Y2M5DT10H12M15S</length></Songs></tes:Album>
Note how the output of the testWithSet1() method is not valid where
as the output of testWithSet2() is. The only difference is the
placement of the "doc.setAlbum(album);" line.
Anyone have any idea as to why this is and is there something I am
not doing ?
Jonathan
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]