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.In schema-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:* user@xmlbeans.apache.org
> *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

Reply via email to