Hi, AFAIK, a setter method internally does a copy of the entire xmlobject passed to it as argument and hence this behaviour. Try this to get your desired behaviour: *<old>* BusinessDescription.Contact contact = BusinessDescription.Contact.Factory.newInstance(); *businessDescription.setContact(contact);* contact.setAddress(address); *</old>
**<new>* BusinessDescription.Contact contact = BusinessDescription.Contact.Factory.newInstance(); *contact = businessDescription.addNewContact().set(contact);* contact.setAddress(address); *</new>* Regards, Srinath/ On Jan 13, 2008 1:40 AM, Paul French <[EMAIL PROTECTED]> wrote: > Okay thanks. > > My lack of understanding is what is being stored internally. I assumed it > was object pointers i.e doing a setXXX and then a getXXX would return the > same XXX object. > > Thanks for clearing that up. > > ------------------------------ > *From:* Jacob Danner [mailto:[EMAIL PROTECTED] > *Sent:* 12 January 2008 02:19 > > *To:* [email protected] > *Subject:* Re: [newbie] difference between addNew and (Factory.newInstance+ > set) > > Hi Paul, > This is actually working as expected :) I just spaced and missed it the > first time around. > > try something like: > BusinessDescription.Contact contact = > BusinessDescription.Contact.Factory.newInstance(); > contact.setAddress(address); > businessDescription.setContact (contact); > > Its the difference between copy and reference in java. > A simpler example: > String a = "1"; > String b = a; > > //b actually has the same memory address as a > > a = "2"; > // the memory address for a and b are now different > > The same is happening in your use of the API. > businessDescription.setContact(contact) does not maintain the reference to > the contact object once it changes because it is now a 'new' object. > The addNewXXX() works differently. If you take a peek at the java source > (scomp -src <dir> yourXSDs) things should get a little clearer :) > > Hope this helps, > -jacobd > > > > On Jan 11, 2008 2:11 PM, Paul French <[EMAIL PROTECTED]> wrote: > > > Thanks for the reply I really appreciate your help. > > > > I understand what you say about the > > BusinessDescription.Contact.Factory.newInstance() creating an > > unassociated contact. Hence I assume you can associate it to a > > BusinessDescription instance by calling > > > > businessDescription.setContact(contact) > > > > Now if I add additional properties to the contact e.g contact.setXXXX they > > cannot be seen by calling > > > > businessDescription.getContact().getXXXX > > > > The schemas are public ones so I have attached them. Look at the > > BusinessDescription_v3_1.xsd schema. I looked at sending a small snippet but > > couldn't see how.... > > > > Have a good weekend!! > > > > > > ------------------------------ > > *From:* Jacob Danner [mailto:[EMAIL PROTECTED] > > *Sent:* 11 January 2008 18:56 > > > > *To:* [email protected] > > *Subject:* Re: [newbie] difference between addNew and ( > > Factory.newInstance + set) > > > > Thought I should take a crack at the question in the subject line real > > quick. > > > > The differences in addNew versus newInstance is that addNew is adding > > the type to an existing element. The newInstance is just that, a brand new > > instance, not attached to anything. > > In your case below, addNewContact() is being called on the > > BusinesDescription Class, which adds a new ContactType to the > > BusinessDescription > > BusinessDescription.Contact contact = businessDescription.addNewContact > > (); > > > > In the Factory.newInstance > > BusinessDescription.Contact contact = > > BusinessDescription.Contact.Factory.newInstance(); > > a new Type is create that is NOT associated with the BusinessDescription > > element that 'encloses/wraps' the contact type in the schema.Inschema-ish > > terms, the contact type is local to the BusinessDescription > > element. > > When contact is used like it is above, it will be wrapped by > > <xml-fragment /> because it is not associated with an element. > > > > In simple terms, thats how I know those API to work, and the differences > > between them. > > > > The addNewXXX() methods exist because of the way the schema is defined, > > so not all the schemas you may work with will have this structure. > > Does this help you understanding? > > > > As to why, one usage returns null and the other one does not, I'm not > > sure. I'm curious how things might be defined in your XSD. Can you give us a > > peek at a schema snippet? > > > > Thanks, > > -jacobd > > > > > > On Jan 11, 2008 10:23 AM, Paul French <[EMAIL PROTECTED]> wrote: > > > > > The Address has it's own type and is imported into the main schema > > > via another schema. > > > > > > I did as you said: > > > > > > BusinessDescription.Contact contact = > > > businessDescription.addNewContact(); > > > contact.setAddress(address); > > > > > > contact.xmlText() or businessDescription.getContact().xmlText() gives > > > > > > <rol:BS7666Address xmlns:rol=" > > > http://www.rol.co.uk"><bs7:AdministrativeArea > > > xmlns:bs7="http://www.govtalk.gov.uk/people/bs7666">TEST</bs7:AdministrativeArea></rol:BS7666Address > > > > > > <http://www.govtalk.gov.uk/people/bs7666%22%3ETEST%3C/bs7:AdministrativeArea%3E%3C/rol:BS7666Address> > > > > > > > However for: > > > > > > BusinessDescription.Contact contact = > > > BusinessDescription.Contact.Factory.newInstance(); > > > businessDescription.setContact(contact); > > > contact.setAddress(address); > > > > > > contact.xmlText() gives > > > > > > <rol:BS7666Address > > > xmlns:rol="http://www.rol.co.uk"><bs7:AdministrativeArea > > > xmlns:bs7="http://www.govtalk.gov.uk/people/bs7666">TEST</bs7:AdministrativeArea></rol:BS7666Address > > > > > > <http://www.govtalk.gov.uk/people/bs7666%22%3ETEST%3C/bs7:AdministrativeArea%3E%3C/rol:BS7666Address> > > > > > > > > > > However, businessDescription.getContact().xmlText() gives > > > > > > <xml-fragment/> > > > > > > I have seen the same behaviour with DateTime. > > > > > > I was doing: > > > > > > contact.setDateTime(Calendar.getInstance()) // line 1 > > > contact.getDateTime().setTime(someDate); // line 2 > > > > > > If I call contact.getDateTime() now it will still hold the same date > > > and time set in line 1 > > > > > > > > > I had to change the code to do: > > > > > > Calendar cal = Calendar.getInstance(); > > > cal.setTime(someDate); > > > contact.setDateTime(someDate); > > > > > > I assumed this must be due to XMLBeans passing back a copy of the > > > Calendar object when calling contact.getDateTime() > > > > > > > > > > > > > > > > > > ------------------------------ > > > *From:* Jacob Danner [mailto:[EMAIL PROTECTED] > > > *Sent:* 11 January 2008 17:52 > > > *To:* [email protected] > > > *Subject:* Re: [newbie] difference between addNew and ( > > > Factory.newInstance + set) > > > > > > Hi Paul, > > > Can you do a > > > System.out.println(contact.xmlText()); > > > in each of the scenarios below? > > > How is Address defined in the schema? > > > Thanks, > > > -jacobd > > > > > > On Jan 11, 2008 9:29 AM, Paul French < [EMAIL PROTECTED]> wrote: > > > > > > > Hello, > > > > > > > > I have created a library using scomp from an xml schema. > > > > > > > > I am building a XML document using this library. I have come across > > > > some behaviour I do not understand. For this example a > > > > BusinessDescription > > > > may have a Contact and a Contact may have an Address. > > > > > > > > > > > > If I do the following: > > > > > > > > BusinessDescription.Contact contact = > > > > businessDescription.addNewContact(); > > > > contact.setAddress(address); > > > > > > > > if (businessDescription.getContact().getAddress() != null) > > > > { > > > > System.out.println("Address is not null"); > > > > } > > > > else > > > > { > > > > System.out.println("Address is null"); > > > > } > > > > > > > > My code prints out "Address is not null" as expected > > > > > > > > > > > > However If I do the following: > > > > > > > > BusinessDescription.Contact contact = > > > > BusinessDescription.Contact.Factory.newInstance(); > > > > businessDescription.setContact(contact); > > > > contact.setAddress(address); > > > > > > > > if (businessDescription.getContact().getAddress() != null) > > > > { > > > > System.out.println("Address is not null"); > > > > } > > > > else > > > > { > > > > System.out.println("Address is null"); > > > > } > > > > > > > > My code prints out "Address is null". > > > > > > > > For some reason setting the address on the contact object after the > > > > contact object is set on the businessDescription is ignored. XMLBeans > > > > must > > > > be cloning objects internally?? This behaviour is unintuitive to me? Can > > > > someone explain. > > > > > > > > Thanks > > > > > > > > > > > > > > > > -- > > > I'm competing in a Half-Ironman distance triathlon to raise money for > > > the fight against cancer! > > > Please help support my efforts by going to: > > > http://www.active.com/donate/tntwaak/jacobd > > > > > > > > > > > -- > > I'm competing in a Half-Ironman distance triathlon to raise money for > > the fight against cancer! > > Please help support my efforts by going to: > > http://www.active.com/donate/tntwaak/jacobd > > > > --------------------------------------------------------------------- > > To unsubscribe, e-mail: [EMAIL PROTECTED] > > For additional commands, e-mail: [EMAIL PROTECTED] > > > > > > -- > I'm competing in a Half-Ironman distance triathlon to raise money for the > fight against cancer! > Please help support my efforts by going to: > http://www.active.com/donate/tntwaak/jacobd >

