How to Populate Binding Objects

View: New views
7 Messages — Rating Filter:   Alert me  

How to Populate Binding Objects

by sia :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Hello,

I have created a XSD document with many complex types, and hve used the XML Code generator (SourceGeneratorMain) to create java binding files. This is all good and works well.

Now, I need to somehow (this is where my question lies) "instantiate" the binding files, populate them, and then marshal the objects to XML. I am not sure how this is accomplished? Could someone give me some insight as to how this works?

My workflow will be like so:
Once the binding files are created (which they are) I have the need to instantiate the objects, populate them with different data and then marshall the data to xml.


Thanks,
Scott




CONFIDENTIALITY NOTICE
This e-mail message and any attachments are only for the use of the intended recipient and may contain information that is privileged, confidential or exempt from disclosure under applicable law. If you are not the intended recipient, any disclosure, distribution or other use of this e-mail message or attachments is prohibited. If you have received this e-mail message in error, please delete and notify the sender immediately. Thank you.

Re: How to Populate Binding Objects

by Werner Guttmann :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Scott,

Scott Purcell wrote:
> Hello,
>
> I have created a XSD document with many complex types, and have used the
> XML Code generator (SourceGeneratorMain) to create java binding files.
> This is all good and works well.
Hmm ... let me try to understand one thing here. When you have used the
XML code generator, you have turned off creastion of descriptor classes
and turned on creation of a mapping file, correct ? If that's the case,
why ? It should be the other way around. See below .....

Please note, that a binding file is something completely different.
The term 'binding file' is used in the context of code generation to
overcome naming collisions.

>
> Now, I need to somehow (this is where my question lies) "instantiate" the
> binding files, populate them, and then marshal the objects to XML. I am
> not sure how this is accomplished? Could someone give me some insight as
> to how this works?
>
> My workflow will be like so:
> Once the binding files are created (which they are) I have the need to
> instantiate the objects, populate them with different data and then
> marshall the data to xml.

Assuming that you have generated source code (including descriptor
classes, that is) from your XML schema, here's what you have to do to
marshal an object instance to XML.

<code>
XMLContext context = new XMLContext();

Marsdaller marshaller = context.createMarshaller();

YourObject yourObject = .... ;

marshaller.marshal(yourObject);
</code>

If (and only if) you have the descriptor classes on your classpath,
Castor will just marshal XML according to your definitions in your XMl
schema.

Does this make sense ?

Werner

>
> Thanks,
> Scott
>
>
>
>
>
> CONFIDENTIALITY NOTICE
> This e-mail message and any attachments are only for the use of the intended recipient and may contain information that is privileged, confidential or exempt from disclosure under applicable law. If you are not the intended recipient, any disclosure, distribution or other use of this e-mail message or attachments is prohibited. If you have received this e-mail message in error, please delete and notify the sender immediately. Thank you.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: How to Populate Binding Objects

by sia :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Thanks for the info, still trying to get my head around this.

The actual example I am working with is the "invoice.xsd" that is included in the source. < found here: http://www.castor.org/srcgen-example.html#Running-the-XML-code-generator> It was analogous to what I am trying to achieve. So I took the invoice.xsd and simply ran source generator command like so:
<


java -cp %CP% org.exolab.castor.builder.SourceGeneratorMain -i invoice.xsd -package test





That command created java files for each of the complex types and a directory named "descriptors" which also have java files in there.

Could someone explain the difference between the src and the descriptors? What is each of their jobs? This is my first run with XML to java binding.

Secondly, I decided to try and marshall this, so I created a Invoice item, and added ShipTo, Customer, Item, ItemAttributes, etc. objects to the invoice in an attempt to marshall the data like so: (see test class), but I am unable to get the "duration" to work. I get errors like so:
see (error below).

// test class
   public static void main(String[] args) {
     
       
        Item item = new Item();
        item.setQuantity(100000);
        item.setId("1");
        item.setInStock(false);
        item.setCategory("my_category");
        item.setPrice(new BigDecimal("100.00"));
       
        Address address = new Address();
        address.setCity("my_city");
        address.setState("MO");
        address.setStreet1("76777777 mockingbird");
        address.setStreet2("other street");
        address.setZipCode("77779");
       
        Customer customer = new Customer();
        customer.setName("Scott");
        customer.setPhone("300-300-3333");
        customer.setAddress(address);
       
        ShipTo shipto = new ShipTo();
        shipto.setCustomer(customer);
       
        ShippingMethod shipmethod = new ShippingMethod();
        shipmethod.setCarrier("carrier");
        shipmethod.setOption("UPS");
       
        Invoice inv = new Invoice();
        inv.setShippingMethod(shipmethod);
        inv.setShipTo(shipto);
        inv.addItem(item);
       
       
        try {
            FileWriter writer = new FileWriter("invoice.xml");
            Marshaller.marshal(inv, writer);
        } catch (Exception e) {
            System.err.println(e.getMessage());
            e.printStackTrace(System.err);
        }
    }



error:
The following exception occured while validating field: _shippingMethod of class: com.Invoice: The field '_estimatedDelivery' (whose xml name is 'estimated-delivery') is a required field of class 'com.ShippingMethod
ValidationException: The following exception occured while validating field: _shippingMethod of class: com.Invoice: The field '_estimatedDelivery' (whose xml name is 'estimated-delivery') is a required field of class 'com.ShippingMethod;
   - location of error: XPATH: /invoice
The field '_estimatedDelivery' (whose xml name is 'estimated-delivery') is a required field of class 'com.ShippingMethod
        at org.exolab.castor.xml.FieldValidator.validate(FieldValidator.java:278)
        at org.exolab.castor.xml.util.XMLClassDescriptorImpl.validate(XMLClassDescriptorImpl.java:1065)
        at org.exolab.castor.xml.Validator.validate(Validator.java:135)
        at org.exolab.castor.xml.Marshaller.validate(Marshaller.java:2594)
        at org.exolab.castor.xml.Marshaller.marshal(Marshaller.java:826)
        at org.exolab.castor.xml.Marshaller.staticMarshal(Marshaller.java:798)
        at org.exolab.castor.xml.Marshaller.marshal(Marshaller.java:727)
        at com.ExerciseInvoice.main(ExerciseInvoice.java:58)
Caused by: ValidationException: The field '_estimatedDelivery' (whose xml name is 'estimated-delivery') is a required field of class 'com.ShippingMethod;
   - location of error: XPATH: /shipping-method
        at org.exolab.castor.xml.FieldValidator.validate(FieldValidator.java:210)
        at org.exolab.castor.xml.util.XMLClassDescriptorImpl.validate(XMLClassDescriptorImpl.java:1065)
        at org.exolab.castor.xml.Validator.validate(Validator.java:135)
        at org.exolab.castor.xml.FieldValidator.validateInstance(FieldValidator.java:326)
        at org.exolab.castor.xml.FieldValidator.validate(FieldValidator.java:271)
        ... 7 more
Caused by: ValidationException: The field '_estimatedDelivery' (whose xml name is 'estimated-delivery') is a required field of class 'com.ShippingMethod;
   - location of error: XPATH: /shipping-method
        at org.exolab.castor.xml.FieldValidator.validate(FieldValidator.java:210)
        at org.exolab.castor.xml.util.XMLClassDescriptorImpl.validate(XMLClassDescriptorImpl.java:1065)
        at org.exolab.castor.xml.Validator.validate(Validator.java:135)
        at org.exolab.castor.xml.FieldValidator.validateInstance(FieldValidator.java:326)
        at org.exolab.castor.xml.FieldValidator.validate(FieldValidator.java:271)
        at org.exolab.castor.xml.util.XMLClassDescriptorImpl.validate(XMLClassDescriptorImpl.java:1065)
        at org.exolab.castor.xml.Validator.validate(Validator.java:135)
        at org.exolab.castor.xml.Marshaller.validate(Marshaller.java:2594)
        at org.exolab.castor.xml.Marshaller.marshal(Marshaller.java:826)
        at org.exolab.castor.xml.Marshaller.staticMarshal(Marshaller.java:798)
        at org.exolab.castor.xml.Marshaller.marshal(Marshaller.java:727)
        at com.ExerciseInvoice.main(ExerciseInvoice.java:58)


Any help???
Thanks,
Scott




Werner Guttmann <werner.guttmann@...>

07/04/2008 02:15 AM
Please respond to
user@...

To
user@...
cc
Subject
Re: [castor-user] How to Populate Binding Objects





Scott,

Scott Purcell wrote:
> Hello,
>
> I have created a XSD document with many complex types, and have used the
> XML Code generator (SourceGeneratorMain) to create java binding files.
> This is all good and works well.
Hmm ... let me try to understand one thing here. When you have used the
XML code generator, you have turned off creastion of descriptor classes
and turned on creation of a mapping file, correct ? If that's the case,
why ? It should be the other way around. See below .....

Please note, that a binding file is something completely different.
The term 'binding file' is used in the context of code generation to
overcome naming collisions.

>
> Now, I need to somehow (this is where my question lies) "instantiate" the
> binding files, populate them, and then marshal the objects to XML. I am
> not sure how this is accomplished? Could someone give me some insight as
> to how this works?
>
> My workflow will be like so:
> Once the binding files are created (which they are) I have the need to
> instantiate the objects, populate them with different data and then
> marshall the data to xml.

Assuming that you have generated source code (including descriptor
classes, that is) from your XML schema, here's what you have to do to
marshal an object instance to XML.

<code>
XMLContext context = new XMLContext();

Marsdaller marshaller = context.createMarshaller();

YourObject yourObject = .... ;

marshaller.marshal(yourObject);
</code>

If (and only if) you have the descriptor classes on your classpath,
Castor will just marshal XML according to your definitions in your XMl
schema.

Does this make sense ?

Werner

>
> Thanks,
> Scott
>
>
>
>
>
> CONFIDENTIALITY NOTICE
> This e-mail message and any attachments are only for the use of the intended recipient and may contain information that is privileged, confidential or exempt from disclosure under applicable law. If you are not the intended recipient, any disclosure, distribution or other use of this e-mail message or attachments is prohibited. If you have received this e-mail message in error, please delete and notify the sender immediately. Thank you.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email






CONFIDENTIALITY NOTICE
This e-mail message and any attachments are only for the use of the intended recipient and may contain information that is privileged, confidential or exempt from disclosure under applicable law. If you are not the intended recipient, any disclosure, distribution or other use of this e-mail message or attachments is prohibited. If you have received this e-mail message in error, please delete and notify the sender immediately. Thank you.

Re: How to Populate Binding Objects

by Godmar Back :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Scott,

read the error message. It says it all.

A field is required, and you didn't set it.

 - Godmar


On Mon, Jul 7, 2008 at 9:31 AM, Scott Purcell
<scott_purcell@...> wrote:

>
> Thanks for the info, still trying to get my head around this.
>
> The actual example I am working with is the "invoice.xsd" that is included
> in the source. < found here:
> http://www.castor.org/srcgen-example.html#Running-the-XML-code-generator> It
> was analogous to what I am trying to achieve. So I took the invoice.xsd and
> simply ran source generator command like so:
> <
>
> java -cp %CP% org.exolab.castor.builder.SourceGeneratorMain -i invoice.xsd
> -package test
>
>
>
>
> That command created java files for each of the complex types and a
> directory named "descriptors" which also have java files in there.
>
> Could someone explain the difference between the src and the descriptors?
> What is each of their jobs? This is my first run with XML to java binding.
>
> Secondly, I decided to try and marshall this, so I created a Invoice item,
> and added ShipTo, Customer, Item, ItemAttributes, etc. objects to the
> invoice in an attempt to marshall the data like so: (see test class), but I
> am unable to get the "duration" to work. I get errors like so:
> see (error below).
>
> // test class
>    public static void main(String[] args) {
>
>
>         Item item = new Item();
>         item.setQuantity(100000);
>         item.setId("1");
>         item.setInStock(false);
>         item.setCategory("my_category");
>         item.setPrice(new BigDecimal("100.00"));
>
>         Address address = new Address();
>         address.setCity("my_city");
>         address.setState("MO");
>         address.setStreet1("76777777 mockingbird");
>         address.setStreet2("other street");
>         address.setZipCode("77779");
>
>         Customer customer = new Customer();
>         customer.setName("Scott");
>         customer.setPhone("300-300-3333");
>         customer.setAddress(address);
>
>         ShipTo shipto = new ShipTo();
>         shipto.setCustomer(customer);
>
>         ShippingMethod shipmethod = new ShippingMethod();
>         shipmethod.setCarrier("carrier");
>         shipmethod.setOption("UPS");
>
>         Invoice inv = new Invoice();
>         inv.setShippingMethod(shipmethod);
>         inv.setShipTo(shipto);
>         inv.addItem(item);
>
>
>         try {
>             FileWriter writer = new FileWriter("invoice.xml");
>             Marshaller.marshal(inv, writer);
>         } catch (Exception e) {
>             System.err.println(e.getMessage());
>             e.printStackTrace(System.err);
>         }
>     }
>
>
>
> error:
> The following exception occured while validating field: _shippingMethod of
> class: com.Invoice: The field '_estimatedDelivery' (whose xml name is
> 'estimated-delivery') is a required field of class 'com.ShippingMethod
> ValidationException: The following exception occured while validating field:
> _shippingMethod of class: com.Invoice: The field '_estimatedDelivery' (whose
> xml name is 'estimated-delivery') is a required field of class
> 'com.ShippingMethod;
>    - location of error: XPATH: /invoice
> The field '_estimatedDelivery' (whose xml name is 'estimated-delivery') is a
> required field of class 'com.ShippingMethod
>         at
> org.exolab.castor.xml.FieldValidator.validate(FieldValidator.java:278)
>         at
> org.exolab.castor.xml.util.XMLClassDescriptorImpl.validate(XMLClassDescriptorImpl.java:1065)
>         at org.exolab.castor.xml.Validator.validate(Validator.java:135)
>         at org.exolab.castor.xml.Marshaller.validate(Marshaller.java:2594)
>         at org.exolab.castor.xml.Marshaller.marshal(Marshaller.java:826)
>         at
> org.exolab.castor.xml.Marshaller.staticMarshal(Marshaller.java:798)
>         at org.exolab.castor.xml.Marshaller.marshal(Marshaller.java:727)
>         at com.ExerciseInvoice.main(ExerciseInvoice.java:58)
> Caused by: ValidationException: The field '_estimatedDelivery' (whose xml
> name is 'estimated-delivery') is a required field of class
> 'com.ShippingMethod;
>    - location of error: XPATH: /shipping-method
>         at
> org.exolab.castor.xml.FieldValidator.validate(FieldValidator.java:210)
>         at
> org.exolab.castor.xml.util.XMLClassDescriptorImpl.validate(XMLClassDescriptorImpl.java:1065)
>         at org.exolab.castor.xml.Validator.validate(Validator.java:135)
>         at
> org.exolab.castor.xml.FieldValidator.validateInstance(FieldValidator.java:326)
>         at
> org.exolab.castor.xml.FieldValidator.validate(FieldValidator.java:271)
>         ... 7 more
> Caused by: ValidationException: The field '_estimatedDelivery' (whose xml
> name is 'estimated-delivery') is a required field of class
> 'com.ShippingMethod;
>    - location of error: XPATH: /shipping-method
>         at
> org.exolab.castor.xml.FieldValidator.validate(FieldValidator.java:210)
>         at
> org.exolab.castor.xml.util.XMLClassDescriptorImpl.validate(XMLClassDescriptorImpl.java:1065)
>         at org.exolab.castor.xml.Validator.validate(Validator.java:135)
>         at
> org.exolab.castor.xml.FieldValidator.validateInstance(FieldValidator.java:326)
>         at
> org.exolab.castor.xml.FieldValidator.validate(FieldValidator.java:271)
>         at
> org.exolab.castor.xml.util.XMLClassDescriptorImpl.validate(XMLClassDescriptorImpl.java:1065)
>         at org.exolab.castor.xml.Validator.validate(Validator.java:135)
>         at org.exolab.castor.xml.Marshaller.validate(Marshaller.java:2594)
>         at org.exolab.castor.xml.Marshaller.marshal(Marshaller.java:826)
>         at
> org.exolab.castor.xml.Marshaller.staticMarshal(Marshaller.java:798)
>         at org.exolab.castor.xml.Marshaller.marshal(Marshaller.java:727)
>         at com.ExerciseInvoice.main(ExerciseInvoice.java:58)
>
>
> Any help???
> Thanks,
> Scott
>
>
>
>
> Werner Guttmann <werner.guttmann@...>
>
> 07/04/2008 02:15 AM
>
> Please respond to
> user@...
> To
> user@...
> cc
> Subject
> Re: [castor-user] How to Populate Binding Objects
>
>
>
>
> Scott,
>
> Scott Purcell wrote:
>> Hello,
>>
>> I have created a XSD document with many complex types, and have used the
>> XML Code generator (SourceGeneratorMain) to create java binding files.
>> This is all good and works well.
> Hmm ... let me try to understand one thing here. When you have used the
> XML code generator, you have turned off creastion of descriptor classes
> and turned on creation of a mapping file, correct ? If that's the case,
> why ? It should be the other way around. See below .....
>
> Please note, that a binding file is something completely different.
> The term 'binding file' is used in the context of code generation to
> overcome naming collisions.
>
>>
>> Now, I need to somehow (this is where my question lies) "instantiate" the
>> binding files, populate them, and then marshal the objects to XML. I am
>> not sure how this is accomplished? Could someone give me some insight as
>> to how this works?
>>
>> My workflow will be like so:
>> Once the binding files are created (which they are) I have the need to
>> instantiate the objects, populate them with different data and then
>> marshall the data to xml.
>
> Assuming that you have generated source code (including descriptor
> classes, that is) from your XML schema, here's what you have to do to
> marshal an object instance to XML.
>
> <code>
> XMLContext context = new XMLContext();
>
> Marsdaller marshaller = context.createMarshaller();
>
> YourObject yourObject = .... ;
>
> marshaller.marshal(yourObject);
> </code>
>
> If (and only if) you have the descriptor classes on your classpath,
> Castor will just marshal XML according to your definitions in your XMl
> schema.
>
> Does this make sense ?
>
> Werner
>>
>> Thanks,
>> Scott
>>
>>
>>
>>
>>
>> CONFIDENTIALITY NOTICE
>> This e-mail message and any attachments are only for the use of the
>> intended recipient and may contain information that is privileged,
>> confidential or exempt from disclosure under applicable law. If you are not
>> the intended recipient, any disclosure, distribution or other use of this
>> e-mail message or attachments is prohibited. If you have received this
>> e-mail message in error, please delete and notify the sender immediately.
>> Thank you.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>
>
>
>
> CONFIDENTIALITY NOTICE
> This e-mail message and any attachments are only for the use of the intended
> recipient and may contain information that is privileged, confidential or
> exempt from disclosure under applicable law. If you are not the intended
> recipient, any disclosure, distribution or other use of this e-mail message
> or attachments is prohibited. If you have received this e-mail message in
> error, please delete and notify the sender immediately. Thank you.
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: How to Populate Binding Objects

by sia :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Ok, making some headway. But as I play more questions arise.

First off, this is a follow-up to the email I sent a bit ago. I am working on the invoice.xsd which is included in the src directory for Castor. I executed the Source Generator like so:



java -cp %CP% org.exolab.castor.builder.SourceGeneratorMain -i invoice.xsd -package test





I was able to created objects and set objects to the invoice. This is where my next question comes from:

There is a class called
  ShippingDate shipDate = new ShippingDate();
        shipDate.setDate(new Date());
        shipDate.setTime(new Time());
       
Which gets created from the XSD. The Date and Time are of org.exolab.castor.types.Date.Date() and also org.exolab.castor.Time.Time(). So am I suposed to create Objects of that type? as opposed to normal Java Date objects? It would appear that this would couple me tightly to the Castor product? Can I use simple Date objects as opposed to this? And if so, how would this be accomplished? Do I need to alter the xsd?

Any help would be greatly appreciated.

Sincerely
Scott







Werner Guttmann <werner.guttmann@...>

07/04/2008 02:15 AM
Please respond to
user@...

To
user@...
cc
Subject
Re: [castor-user] How to Populate Binding Objects





Scott,

Scott Purcell wrote:
> Hello,
>
> I have created a XSD document with many complex types, and have used the
> XML Code generator (SourceGeneratorMain) to create java binding files.
> This is all good and works well.
Hmm ... let me try to understand one thing here. When you have used the
XML code generator, you have turned off creastion of descriptor classes
and turned on creation of a mapping file, correct ? If that's the case,
why ? It should be the other way around. See below .....

Please note, that a binding file is something completely different.
The term 'binding file' is used in the context of code generation to
overcome naming collisions.

>
> Now, I need to somehow (this is where my question lies) "instantiate" the
> binding files, populate them, and then marshal the objects to XML. I am
> not sure how this is accomplished? Could someone give me some insight as
> to how this works?
>
> My workflow will be like so:
> Once the binding files are created (which they are) I have the need to
> instantiate the objects, populate them with different data and then
> marshall the data to xml.

Assuming that you have generated source code (including descriptor
classes, that is) from your XML schema, here's what you have to do to
marshal an object instance to XML.

<code>
XMLContext context = new XMLContext();

Marsdaller marshaller = context.createMarshaller();

YourObject yourObject = .... ;

marshaller.marshal(yourObject);
</code>

If (and only if) you have the descriptor classes on your classpath,
Castor will just marshal XML according to your definitions in your XMl
schema.

Does this make sense ?

Werner

>
> Thanks,
> Scott
>
>
>
>
>
> CONFIDENTIALITY NOTICE
> This e-mail message and any attachments are only for the use of the intended recipient and may contain information that is privileged, confidential or exempt from disclosure under applicable law. If you are not the intended recipient, any disclosure, distribution or other use of this e-mail message or attachments is prohibited. If you have received this e-mail message in error, please delete and notify the sender immediately. Thank you.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email






CONFIDENTIALITY NOTICE
This e-mail message and any attachments are only for the use of the intended recipient and may contain information that is privileged, confidential or exempt from disclosure under applicable law. If you are not the intended recipient, any disclosure, distribution or other use of this e-mail message or attachments is prohibited. If you have received this e-mail message in error, please delete and notify the sender immediately. Thank you.

Re: How to Populate Binding Objects

by Werner Guttmann :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Scott,

let me try to address som eof your questions piece by piece ...

Werner

Scott Purcell wrote:

> Thanks for the info, still trying to get my head around this.
>
> The actual example I am working with is the "invoice.xsd" that is included
> in the source. < found here:
> http://www.castor.org/srcgen-example.html#Running-the-XML-code-generator>
> It was analogous to what I am trying to achieve. So I took the invoice.xsd
> and simply ran source generator command like so:
> <
>
>
>
>
> java -cp %CP% org.exolab.castor.builder.SourceGeneratorMain -i invoice.xsd
> -package test

You are aware that Castor offers integration for both Ant as well as
Maven, right ? Personally, using a command line interface appears a bit
complicated to me when I do have the richness of Ant task definitions
and Maven plugins.
>
> That command created java files for each of the complex types and a
> directory named "descriptors" which also have java files in there.
>
> Could someone explain the difference between the src and the descriptors?
> What is each of their jobs?
>This is my first run with XML to java binding.
Well, let's start wit the wording, if you do not mind .. ;-).

You just used the XML code generator to create Java source code from
your XML schema. As you have noticed yourself, the artifacts that have
been produced by this source generation (step) are two-fold:

a) Java domain classes, more or less one for each XMl schema artifact
(e.g. a Java class for a complex type definition, etc.).

b) A descriptor class for each domain class generated. These classes
partially share the name with the domain class, i.e. for a class Stock
being generated you'll find the corresponding StockDescriptor in the
descriptors package.

The Java domain class simply is the Java representation of your XML
schema artifact. The descriptor class is a class used by Castor
internally to facilitate XML data binding (unmarshalling and
marshalling) between the domain classes just generated any XML document
instances.

I don't know whether you have written a mapping file already to map an
(existing) set of java classes to XML (or not), but in any case Castor
(at startup) will analyze your mappings and produce what we call
descriptors for each class (and field) being mapped.

In your case, you are generating your Java source code from an XML
schema. Rather than asking you to supply us (Castor) with a mapping, we
use a different strategy here. Remember, the Castor XML code generator
takes your XML schema and geenrates domain classes. Well, it creates
descriptor classes as well, deriving all required information (node
names, namespace URIs, namespace prefices, ....) from your XML schema

Later on, when you are using the generated clases for (un-)marshalling,
Castor will be able to use those descriptor classes and e.g. produce XML
that exactly matches your XML schema (which happened to be at the start
of the process).

Does this make more sense now ?

>
> Secondly, I decided to try and marshall this, so I created a Invoice item,
> and added ShipTo, Customer, Item, ItemAttributes, etc. objects to the
> invoice in an attempt to marshall the data like so: (see test class), but
> I am unable to get the "duration" to work. I get errors like so:
> see (error below).
>
> // test class
>    public static void main(String[] args) {
>  
>  
>         Item item = new Item();
>         item.setQuantity(100000);
>         item.setId("1");
>         item.setInStock(false);
>         item.setCategory("my_category");
>         item.setPrice(new BigDecimal("100.00"));
>  
>         Address address = new Address();
>         address.setCity("my_city");
>         address.setState("MO");
>         address.setStreet1("76777777 mockingbird");
>         address.setStreet2("other street");
>         address.setZipCode("77779");
>  
>         Customer customer = new Customer();
>         customer.setName("Scott");
>         customer.setPhone("300-300-3333");
>         customer.setAddress(address);
>  
>         ShipTo shipto = new ShipTo();
>         shipto.setCustomer(customer);
>  
>         ShippingMethod shipmethod = new ShippingMethod();
>         shipmethod.setCarrier("carrier");
>         shipmethod.setOption("UPS");
>  
>         Invoice inv = new Invoice();
>         inv.setShippingMethod(shipmethod);
>         inv.setShipTo(shipto);
>         inv.addItem(item);
>  
>  
>         try {
>             FileWriter writer = new FileWriter("invoice.xml");
>             Marshaller.marshal(inv, writer);
>         } catch (Exception e) {
>             System.err.println(e.getMessage());
>             e.printStackTrace(System.err);
>         }
>     }
>
>
>
> error:
> The following exception occured while validating field: _shippingMethod of
> class: com.Invoice: The field '_estimatedDelivery' (whose xml name is
> 'estimated-delivery') is a required field of class 'com.ShippingMethod

If you look at this exception in detail, you'll come to see (after
having used Castor more frequently) that Castor tries to validate your
object instance before marshalling, something no other tool is capable
of doing (to my knowledge). And as it happens, your XML schema defines
that there needs to be a value for the property 'estimatedDelivery' of
ShippingMethod, as you have defined it to be required in your XML schema.

Simply add a

{code}
shipMethod.setEstimatedDelivery(...);
{code}

statement to your test case above and you should be fine (given that
there's no other problems).


>The following exception occured while validating
> field: _shippingMethod of class: com.Invoice: The field
> '_estimatedDelivery' (whose xml name is 'estimated-delivery') is a
> required field of class 'com.ShippingMethod;
>    - location of error: XPATH: /invoice
> The field '_estimatedDelivery' (whose xml name is 'estimated-delivery') is
> a required field of class 'com.ShippingMethod
>         at
> org.exolab.castor.xml.FieldValidator.validate(FieldValidator.java:278)
>         at
> org.exolab.castor.xml.util.XMLClassDescriptorImpl.validate(XMLClassDescriptorImpl.java:1065)
>         at org.exolab.castor.xml.Validator.validate(Validator.java:135)
>         at org.exolab.castor.xml.Marshaller.validate(Marshaller.java:2594)
>         at org.exolab.castor.xml.Marshaller.marshal(Marshaller.java:826)
>         at
> org.exolab.castor.xml.Marshaller.staticMarshal(Marshaller.java:798)
>         at org.exolab.castor.xml.Marshaller.marshal(Marshaller.java:727)
>         at com.ExerciseInvoice.main(ExerciseInvoice.java:58)
> Caused by: ValidationException: The field '_estimatedDelivery' (whose xml
> name is 'estimated-delivery') is a required field of class
> 'com.ShippingMethod;
>    - location of error: XPATH: /shipping-method
>         at
> org.exolab.castor.xml.FieldValidator.validate(FieldValidator.java:210)
>         at
> org.exolab.castor.xml.util.XMLClassDescriptorImpl.validate(XMLClassDescriptorImpl.java:1065)
>         at org.exolab.castor.xml.Validator.validate(Validator.java:135)
>         at
> org.exolab.castor.xml.FieldValidator.validateInstance(FieldValidator.java:326)
>         at
> org.exolab.castor.xml.FieldValidator.validate(FieldValidator.java:271)
>         ... 7 more
> Caused by: ValidationException: The field '_estimatedDelivery' (whose xml
> name is 'estimated-delivery') is a required field of class
> 'com.ShippingMethod;
>    - location of error: XPATH: /shipping-method
>         at
> org.exolab.castor.xml.FieldValidator.validate(FieldValidator.java:210)
>         at
> org.exolab.castor.xml.util.XMLClassDescriptorImpl.validate(XMLClassDescriptorImpl.java:1065)
>         at org.exolab.castor.xml.Validator.validate(Validator.java:135)
>         at
> org.exolab.castor.xml.FieldValidator.validateInstance(FieldValidator.java:326)
>         at
> org.exolab.castor.xml.FieldValidator.validate(FieldValidator.java:271)
>         at
> org.exolab.castor.xml.util.XMLClassDescriptorImpl.validate(XMLClassDescriptorImpl.java:1065)
>         at org.exolab.castor.xml.Validator.validate(Validator.java:135)
>         at org.exolab.castor.xml.Marshaller.validate(Marshaller.java:2594)
>         at org.exolab.castor.xml.Marshaller.marshal(Marshaller.java:826)
>         at
> org.exolab.castor.xml.Marshaller.staticMarshal(Marshaller.java:798)
>         at org.exolab.castor.xml.Marshaller.marshal(Marshaller.java:727)
>         at com.ExerciseInvoice.main(ExerciseInvoice.java:58)
>
>
> Any help???
> Thanks,
> Scott
>
>
>
>
>
> Werner Guttmann <werner.guttmann@...>
> 07/04/2008 02:15 AM
> Please respond to
> user@...
>
>
> To
> user@...
> cc
>
> Subject
> Re: [castor-user] How to Populate Binding Objects
>
>
>
>
>
>
> Scott,
>
> Scott Purcell wrote:
>> Hello,
>>
>> I have created a XSD document with many complex types, and have used the
>
>> XML Code generator (SourceGeneratorMain) to create java binding files.
>> This is all good and works well.
> Hmm ... let me try to understand one thing here. When you have used the
> XML code generator, you have turned off creastion of descriptor classes
> and turned on creation of a mapping file, correct ? If that's the case,
> why ? It should be the other way around. See below .....
>
> Please note, that a binding file is something completely different.
> The term 'binding file' is used in the context of code generation to
> overcome naming collisions.
>
>> Now, I need to somehow (this is where my question lies) "instantiate"
> the
>> binding files, populate them, and then marshal the objects to XML. I am
>> not sure how this is accomplished? Could someone give me some insight as
>
>> to how this works?
>>
>> My workflow will be like so:
>> Once the binding files are created (which they are) I have the need to
>> instantiate the objects, populate them with different data and then
>> marshall the data to xml.
>
> Assuming that you have generated source code (including descriptor
> classes, that is) from your XML schema, here's what you have to do to
> marshal an object instance to XML.
>
> <code>
> XMLContext context = new XMLContext();
>
> Marsdaller marshaller = context.createMarshaller();
>
> YourObject yourObject = .... ;
>
> marshaller.marshal(yourObject);
> </code>
>
> If (and only if) you have the descriptor classes on your classpath,
> Castor will just marshal XML according to your definitions in your XMl
> schema.
>
> Does this make sense ?
>
> Werner
>> Thanks,
>> Scott
>>
>>
>>
>>
>>
>> CONFIDENTIALITY NOTICE
>> This e-mail message and any attachments are only for the use of the
> intended recipient and may contain information that is privileged,
> confidential or exempt from disclosure under applicable law. If you are
> not the intended recipient, any disclosure, distribution or other use of
> this e-mail message or attachments is prohibited. If you have received
> this e-mail message in error, please delete and notify the sender
> immediately. Thank you.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>
>
>
>
>
>
> CONFIDENTIALITY NOTICE
> This e-mail message and any attachments are only for the use of the intended recipient and may contain information that is privileged, confidential or exempt from disclosure under applicable law. If you are not the intended recipient, any disclosure, distribution or other use of this e-mail message or attachments is prohibited. If you have received this e-mail message in error, please delete and notify the sender immediately. Thank you.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: How to Populate Binding Objects

by Werner Guttmann :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Scott,

Scott Purcell wrote:
> Ok, making some headway. But as I play more questions arise.
>
> First off, this is a follow-up to the email I sent a bit ago. I am working
> on the invoice.xsd which is included in the src directory for Castor. I
> executed the Source Generator like so:

> java -cp %CP% org.exolab.castor.builder.SourceGeneratorMain -i invoice.xsd
> -package test
>
> I was able to created objects and set objects to the invoice. This is
> where my next question comes from:
>
> There is a class called
>   ShippingDate shipDate = new ShippingDate();
>         shipDate.setDate(new Date());
>         shipDate.setTime(new Time());
>  
> Which gets created from the XSD. The Date and Time are of
> org.exolab.castor.types.Date.Date() and also
> org.exolab.castor.Time.Time(). So am I suposed to create Objects of that
> type?

Yes. Please bear in mind that the syntax for XML dates and times can be
far more complex than what e.g. java.util.Date offers, and as such you
will have to use Castor-specific types (in this case only).

This shouldn't be too hard as the Date class offers the following
constructor, though:

public Date(java.util.Date dateRef);

> as opposed to normal Java Date objects? It would appear that this
> would couple me tightly to the Castor product? Can I use simple Date
> objects as opposed to this? And if so, how would this be accomplished? Do
> I need to alter the xsd?
You could try to override the default type being used using a binding
file as an input to the source generation, but you would loose some
benefits (incl. e.g. validations)

>
> Any help would be greatly appreciated.
>
> Sincerely
> Scott
>
>
>
>
>
>
>
>
> Werner Guttmann <werner.guttmann@...>
> 07/04/2008 02:15 AM
> Please respond to
> user@...
>
>
> To
> user@...
> cc
>
> Subject
> Re: [castor-user] How to Populate Binding Objects
>
>
>
>
>
>
> Scott,
>
> Scott Purcell wrote:
>> Hello,
>>
>> I have created a XSD document with many complex types, and have used the
>
>> XML Code generator (SourceGeneratorMain) to create java binding files.
>> This is all good and works well.
> Hmm ... let me try to understand one thing here. When you have used the
> XML code generator, you have turned off creastion of descriptor classes
> and turned on creation of a mapping file, correct ? If that's the case,
> why ? It should be the other way around. See below .....
>
> Please note, that a binding file is something completely different.
> The term 'binding file' is used in the context of code generation to
> overcome naming collisions.
>
>> Now, I need to somehow (this is where my question lies) "instantiate"
> the
>> binding files, populate them, and then marshal the objects to XML. I am
>> not sure how this is accomplished? Could someone give me some insight as
>
>> to how this works?
>>
>> My workflow will be like so:
>> Once the binding files are created (which they are) I have the need to
>> instantiate the objects, populate them with different data and then
>> marshall the data to xml.
>
> Assuming that you have generated source code (including descriptor
> classes, that is) from your XML schema, here's what you have to do to
> marshal an object instance to XML.
>
> <code>
> XMLContext context = new XMLContext();
>
> Marsdaller marshaller = context.createMarshaller();
>
> YourObject yourObject = .... ;
>
> marshaller.marshal(yourObject);
> </code>
>
> If (and only if) you have the descriptor classes on your classpath,
> Castor will just marshal XML according to your definitions in your XMl
> schema.
>
> Does this make sense ?
>
> Werner
>> Thanks,
>> Scott
>>
>>
>>
>>
>>
>> CONFIDENTIALITY NOTICE
>> This e-mail message and any attachments are only for the use of the
> intended recipient and may contain information that is privileged,
> confidential or exempt from disclosure under applicable law. If you are
> not the intended recipient, any disclosure, distribution or other use of
> this e-mail message or attachments is prohibited. If you have received
> this e-mail message in error, please delete and notify the sender
> immediately. Thank you.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>
>
>
>
>
>
> CONFIDENTIALITY NOTICE
> This e-mail message and any attachments are only for the use of the intended recipient and may contain information that is privileged, confidential or exempt from disclosure under applicable law. If you are not the intended recipient, any disclosure, distribution or other use of this e-mail message or attachments is prohibited. If you have received this e-mail message in error, please delete and notify the sender immediately. Thank you.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


LightInTheBox - Buy quality products at wholesale price