users@jaxb.java.net

Re: JAXB2 - Problem with marshaling

From: Ed Mooney <Ed.Mooney_at_Sun.COM>
Date: Thu, 17 Nov 2005 13:56:15 -0500

Hi Yavuz,

Attached class does what you want:

     java test
     <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
     <ProcessSpecification
xmlns="http://docs.oasis-open.org/ebxmlbp/ebbp-2.0">
         <BusinessCollaboration>
         </BusinessCollaboration>
     </ProcessSpecification>

   -- Ed

Yavuz Gürcan wrote:
> Hello,
>
> I am currently working on an editor that uses the model of an XSD [1]
> to create an XML representation of a class.
> However, there is a problem. I am trying to create a
> "ProcessSpecification" class and using its functions add a
> "BusinessCollaboration" to it. When I marshal the ProcessSpecification
> before adding anything the XML turns out to be:
> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
>
> Well, almost nothing I guess.
> After adding items i get:
> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
> <BusinessCollaboration name="Untitled" nameID="specify-ID"
> xmlns:ns2="http://docs.oasis-open.org/ebxmlbp/ebbp-2.0"/>
>
> So, the parent does not appear at all. When I add anything else to it
> it becomes something like:
> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
> <BusinessCollaboration name="Untitled" nameID="specify-ID"
> xmlns:ns2="http://docs.oasis-open.org/ebxmlbp/ebbp-2.0"/>
> <BusinessCollaboration name="Untitled2" nameID="specify-ID2"
> xmlns:ns2="http://docs.oasis-open.org/ebxmlbp/ebbp-2.0"/>
>
> This is also a problem, as it does not have a single root.
>
> I would appreciate if anyone could guide me on how to overcome this,
> or provide some possible causes, or even tell me a way to debug this.
>
> Another question is about JAXBElement. This was not present in JAXB 1
> and functions taking specific object were clear (for example
> BusinessCollaboration as input). In JAXB 2, however, almost all
> elements are taken as JAXBElement classes. Is there something I have
> missed? Is this a problem?
>
> Finally, in JAXB 1, the createMarshaller and createUnmarshaller
> functions were available in ObjectFactory. These are not present.
> Instead I am using the marshaller/unmarshaller directly from the JAXB
> libraries. I guess this is the new convention?
>
> The reason to switch to JAXB 2 is that, I had a problem with
> marshaling/unmarshaling of incomplete classes with incomplete (and
> required) fields. Any other suggestion to overcome this situation?
>
> In case you need more information, the jar I used, the src that was
> created with JAXB 2, etc. I can provide these as well.
>
> [1] ebbp 2.0.1 -
> http://www.oasis-open.org/committees/download.php/13907/ebxmlbp-v2.0.1-Schema-cd-en.zip
>
> Thanks in advance.
>
> Best regards,
> --
> Yavuz Gurcan
> Software Engineer
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>


import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;

import org.oasis_open.docs.ebxmlbp.ebbp_2.*;

public class test {
    public static final void main(final String[] args) {
        try {
            JAXBContext jc =
                JAXBContext.newInstance("org.oasis_open.docs.ebxmlbp.ebbp_2");
            Marshaller m = jc.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            ObjectFactory of = new ObjectFactory();
            BusinessCollaborationType bctv =
                of.createBusinessCollaborationType();
            JAXBElement<BusinessCollaborationType> bct =
                of.createBusinessCollaboration(bctv);
            ProcessSpecification pst = of.createProcessSpecification();
            ((ProcessSpecificationType)pst).getPackageOrBusinessDocumentOrBusinessTransactionHead().add(bct);
            m.marshal(pst, System.err);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}