You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by eaudet <ea...@jarics.com> on 2011/03/10 22:23:25 UTC

How to get String values out of a JAXBElement object

Hi,

I am facing an issue with wsdl2java tool. I read in several other threads
that you can use a binding to get rid of JAXBElement classes. My wsdl is
using a complex HL7 schema. wsdl2java generates all the classes without
problems. All classes reflects the schema but at some points, the  element
is a JAXBElement. For some reason, wsdl2java did not generate a class Value
with children Family and Given classes. Can someone help in getting rid of
those JAXBElement classes or can tell me how to get the value "toto" and
"tata" from my objects?

Here's a part of my xml message:
....

  
    
      toto
      tata
    
  

...


Here's the Java code that handles the message:

PersonName wPersonName = ...queryByParameter().getValue().getPersonName();
wPersonName.getValue().getContent(); //Returns a List of JAXBElement objects


I would like to know how to retrieve "toto" from the message.

Thanks,
- Erick


--
View this message in context: http://cxf.547215.n5.nabble.com/How-to-get-String-values-out-of-a-JAXBElement-object-tp3424129p3424129.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: How to get String values out of a JAXBElement object

Posted by Erick Audet <ea...@jarics.com>.
Hi Dan,
I will do what you are proposing. I will be back with the outcome.
Meanwhile, if you can look at these two small eclipse projects. They contain
the problem.
Import these two small eclipse projects in your workspace. Simply run
the JAXBTesterProb
class.
/SIPMIWsFiles/JAXBProblem/jaxb_problem.xsd is a reduced version of HL7
schema. It contains only the types and element in problem.
/SIPMIWsFiles/JAXBProblem/prob.xml is a sample xml file I use to investigate
the problem.

- Thanks



On Wed, Mar 16, 2011 at 2:24 PM, Daniel Kulp <dk...@apache.org> wrote:

>
>
> Without the full schemas and message, it's hard to diagnose.  One thing you
> could do though, since you have the schema, is to use the
> javax.xml.validation.SchemaFactory API's to create a Schema object from
> your
> xsd and then, before calling unmarshall, call the
> unmarshaller.setSchema(..)
> method with it to turn on complete schema validation.   You can also call
> unmarshaller.setEventHandler with a ValidationEventHandler that prints some
> information out or similar.  JAXB, by default, ignores a lot of things it
> doesn't understand.   If you set a ValidationEventHandler and/or turn on
> the
> schema validation, you may get some exceptions or extra information about
> what
> it is doing that is really invalid or similar.
>
> Dan
>
>
>
>
>
> On Wednesday 16 March 2011 10:23:28 AM eaudet wrote:
> > Here's a much more precise explanation. I have scalled down to pure JAXB
> to
> > understand to problem.
> >
> > I have generated my java classes using this command:
> >
> >    xjc -d ./src MySchema.xsd
> >
> >
> > Everything compiles. Nice!. Here's a part of my xsd in problem:
> >
> >  ...
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >  .....
> >
> >
> >
> > Here's the generated **EN** class:
> >
> > ....
> > @XmlAccessorType(XmlAccessType.FIELD)
> > @XmlType(name = "EN", propOrder = {
> >    "content"
> > })
> > @XmlSeeAlso({
> >    ON.class,
> >    PN.class,
> >    TN.class
> > })
> > public class EN {
> >
> >    @XmlElementRefs({
> >        @XmlElementRef(name = "delimiter", namespace =
> > "urn:hl7-org:v3", type = JAXBElement.class),
> >        @XmlElementRef(name = "validTime", namespace =
> > "urn:hl7-org:v3", type = JAXBElement.class),
> >        @XmlElementRef(name = "suffix", namespace = "urn:hl7-org:v3",
> > type = JAXBElement.class),
> >        @XmlElementRef(name = "prefix", namespace = "urn:hl7-org:v3",
> > type = JAXBElement.class),
> >        @XmlElementRef(name = "family", namespace = "urn:hl7-org:v3",
> > type = JAXBElement.class),
> >        @XmlElementRef(name = "given", namespace = "urn:hl7-org:v3",
> > type = JAXBElement.class)
> >    })
> >    @XmlMixed
> >    protected List content;
> > ......
> >
> >
> >
> > Here's the related XML example file in problem (fits with the above
> > XSD) :
> >
> >
> >
> > ...
> >
> >
> >                                        Smith
> >                                        John
> >
> >
> > ...
> >
> >
> >
> >
> >
> > Here's the java code doing the unmarshalling in problem:
> >
> >
> >
> >
> > ...
> >                for (Serializable s :
> > pPersonName.getValue().getContent()) {
> >                        if (s instanceof String) {
> >                                System.out.print((String) s);
> >                        } else {
> >                                String tag = ((JAXBElement)
> > s).getName().getLocalPart();
> >                                if (s instanceof EnFamily) {
> >                                        EnFamily chunk = (EnFamily)
> > ((JAXBElement) s).getValue();
> >                                        System.out.print("(" + tag +
> > ":");
> >                                }
> >                        }
> >                }
> > ...
> >
> >
> >
> > And here's my problem: The class **EnFamily** extracted from a
> > JAXBElement does not contain any values. It should contain "Smith". It
> > seems it is not unmarshalling correctly.
> >
> > Any help appreciated!
> >
> > --
> > View this message in context:
> >
> http://cxf.547215.n5.nabble.com/How-to-get-String-values-out-of-a-JAXBElem
> > ent-object-tp3424129p3782616.html Sent from the cxf-user mailing list
> > archive at Nabble.com.
>
> --
> Daniel Kulp
> dkulp@apache.org
> http://dankulp.com/blog
> Talend - http://www.talend.com
>



-- 
Erick Audet M.Sc.
site: www.jarics.com
email: eaudet@jarics.com
Skype:erick.audet
LinkedIn: http://www.linkedin.com/in/erickaudet
mob.: 418.657.9712

Re: How to get String values out of a JAXBElement object

Posted by Daniel Kulp <dk...@apache.org>.

Without the full schemas and message, it's hard to diagnose.  One thing you 
could do though, since you have the schema, is to use the 
javax.xml.validation.SchemaFactory API's to create a Schema object from your 
xsd and then, before calling unmarshall, call the unmarshaller.setSchema(..) 
method with it to turn on complete schema validation.   You can also call 
unmarshaller.setEventHandler with a ValidationEventHandler that prints some 
information out or similar.  JAXB, by default, ignores a lot of things it 
doesn't understand.   If you set a ValidationEventHandler and/or turn on the 
schema validation, you may get some exceptions or extra information about what 
it is doing that is really invalid or similar.

Dan





On Wednesday 16 March 2011 10:23:28 AM eaudet wrote:
> Here's a much more precise explanation. I have scalled down to pure JAXB to
> understand to problem.
> 
> I have generated my java classes using this command:
> 
>    xjc -d ./src MySchema.xsd
> 
> 
> Everything compiles. Nice!. Here's a part of my xsd in problem:
> 
>  ...
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
>  .....
> 
> 
> 
> Here's the generated **EN** class:
> 
> ....
> @XmlAccessorType(XmlAccessType.FIELD)
> @XmlType(name = "EN", propOrder = {
>    "content"
> })
> @XmlSeeAlso({
>    ON.class,
>    PN.class,
>    TN.class
> })
> public class EN {
> 
>    @XmlElementRefs({
>        @XmlElementRef(name = "delimiter", namespace =
> "urn:hl7-org:v3", type = JAXBElement.class),
>        @XmlElementRef(name = "validTime", namespace =
> "urn:hl7-org:v3", type = JAXBElement.class),
>        @XmlElementRef(name = "suffix", namespace = "urn:hl7-org:v3",
> type = JAXBElement.class),
>        @XmlElementRef(name = "prefix", namespace = "urn:hl7-org:v3",
> type = JAXBElement.class),
>        @XmlElementRef(name = "family", namespace = "urn:hl7-org:v3",
> type = JAXBElement.class),
>        @XmlElementRef(name = "given", namespace = "urn:hl7-org:v3",
> type = JAXBElement.class)
>    })
>    @XmlMixed
>    protected List content;
> ......
> 
> 
> 
> Here's the related XML example file in problem (fits with the above
> XSD) :
> 
> 
> 
> ...
> 
> 
>                                        Smith
>                                        John
> 
> 
> ...
> 
> 
> 
> 
> 
> Here's the java code doing the unmarshalling in problem:
> 
> 
> 
> 
> ...
>                for (Serializable s :
> pPersonName.getValue().getContent()) {
>                        if (s instanceof String) {
>                                System.out.print((String) s);
>                        } else {
>                                String tag = ((JAXBElement)
> s).getName().getLocalPart();
>                                if (s instanceof EnFamily) {
>                                        EnFamily chunk = (EnFamily)
> ((JAXBElement) s).getValue();
>                                        System.out.print("(" + tag +
> ":");
>                                }
>                        }
>                }
> ...
> 
> 
> 
> And here's my problem: The class **EnFamily** extracted from a
> JAXBElement does not contain any values. It should contain "Smith". It
> seems it is not unmarshalling correctly.
> 
> Any help appreciated!
> 
> --
> View this message in context:
> http://cxf.547215.n5.nabble.com/How-to-get-String-values-out-of-a-JAXBElem
> ent-object-tp3424129p3782616.html Sent from the cxf-user mailing list
> archive at Nabble.com.

-- 
Daniel Kulp
dkulp@apache.org
http://dankulp.com/blog
Talend - http://www.talend.com

Re: How to get String values out of a JAXBElement object

Posted by eaudet <ea...@jarics.com>.
Here's a much more precise explanation. I have scalled down to pure JAXB to
understand to problem.

I have generated my java classes using this command:

   xjc -d ./src MySchema.xsd


Everything compiles. Nice!. Here's a part of my xsd in problem:

 ...
 
  
    
      
        
          
          
          
          
          
        
        
      
      
    
  
 
 .....



Here's the generated **EN** class:

....
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "EN", propOrder = {
   "content"
})
@XmlSeeAlso({
   ON.class,
   PN.class,
   TN.class
})
public class EN {

   @XmlElementRefs({
       @XmlElementRef(name = "delimiter", namespace =
"urn:hl7-org:v3", type = JAXBElement.class),
       @XmlElementRef(name = "validTime", namespace =
"urn:hl7-org:v3", type = JAXBElement.class),
       @XmlElementRef(name = "suffix", namespace = "urn:hl7-org:v3",
type = JAXBElement.class),
       @XmlElementRef(name = "prefix", namespace = "urn:hl7-org:v3",
type = JAXBElement.class),
       @XmlElementRef(name = "family", namespace = "urn:hl7-org:v3",
type = JAXBElement.class),
       @XmlElementRef(name = "given", namespace = "urn:hl7-org:v3",
type = JAXBElement.class)
   })
   @XmlMixed
   protected List content;
......



Here's the related XML example file in problem (fits with the above
XSD) :



...
                       
                               
                                       Smith
                                       John
                               
                       
...





Here's the java code doing the unmarshalling in problem:




...
               for (Serializable s :
pPersonName.getValue().getContent()) {
                       if (s instanceof String) {
                               System.out.print((String) s);
                       } else {
                               String tag = ((JAXBElement)
s).getName().getLocalPart();
                               if (s instanceof EnFamily) {
                                       EnFamily chunk = (EnFamily)
((JAXBElement) s).getValue();
                                       System.out.print("(" + tag +
":");
                               }
                       }
               }
...



And here's my problem: The class **EnFamily** extracted from a
JAXBElement does not contain any values. It should contain "Smith". It
seems it is not unmarshalling correctly.

Any help appreciated!

--
View this message in context: http://cxf.547215.n5.nabble.com/How-to-get-String-values-out-of-a-JAXBElement-object-tp3424129p3782616.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: How to get String values out of a JAXBElement object

Posted by Daniel Kulp <dk...@apache.org>.
We'd likely need  to see a testcase.   Usually, this kind of thing is due to a 
namespace issue, but without an example, it's hard to tell.

Dan


On Monday 14 March 2011 3:45:28 PM eaudet wrote:
> Hi Again,
> It's seems like JAXB unmarshalling process did not handle the values of my
> xml message:
> ...
> tototata...
> 
> Is it possible? When it is not a JAXBElement, all values from my XML
> message are unmarshelled correclty.
> 
> - Erick
> 
> --
> View this message in context:
> http://cxf.547215.n5.nabble.com/How-to-get-String-values-out-of-a-JAXBElem
> ent-object-tp3424129p3595666.html Sent from the cxf-user mailing list
> archive at Nabble.com.

-- 
Daniel Kulp
dkulp@apache.org
http://dankulp.com/blog
Talend - http://www.talend.com

Re: How to get String values out of a JAXBElement object

Posted by eaudet <ea...@jarics.com>.
Hi Again,
It's seems like JAXB unmarshalling process did not handle the values of my
xml message:
...
tototata...

Is it possible? When it is not a JAXBElement, all values from my XML message
are unmarshelled correclty.

- Erick

--
View this message in context: http://cxf.547215.n5.nabble.com/How-to-get-String-values-out-of-a-JAXBElement-object-tp3424129p3595666.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: How to get String values out of a JAXBElement object

Posted by eaudet <ea...@jarics.com>.
Hi guys,

Tried several approaches with adding binding information to wsdl2java
command but no improvements.

I don't mind playing with JAXBElement objects but I can't get my values out
of it. Here's my java code:

...
		QUPAMT101103CAPersonName wPersonName = pCandidatesReqWS
				.getQUPAIN101103CA().getControlActProcess()
				.getQueryByParameter().getValue().getPersonName();
		PN wPn = wPersonName.getValue();
		StringBuffer wCandidateName = new StringBuffer();
		for (Serializable wSerializable : wPersonName.getValue().getContent()) {
			System.out.println(wSerializable);
		}
...

The for loop is actually looping on JAXBElements. The object is there but
all attributes are null! When I debug in eclipse and inspect the objects, I
can't see my values (toto and tata) that I have in my xml.

...
 
  
    toto
    tata
  

...

Is there a helper class to unmarshal the value out of a JAXBElement object?

- Erick

Glen Mazza-3 wrote:
> 
> On 10.03.2011 16:28, KARR, DAVID (ATTSI) wrote:
>>> -----Original Message-----
>>> From: eaudet [mailto:eaudet@jarics.com]
>>> Sent: Thursday, March 10, 2011 1:23 PM
>>> To: users@cxf.apache.org
>>> Subject: How to get String values out of a JAXBElement object
>>>
>>> Hi,
>>>
>>> I am facing an issue with wsdl2java tool. I read in several other
>>> threads
>>> that you can use a binding to get rid of JAXBElement classes. My wsdl
>>> is
>>> using a complex HL7 schema. wsdl2java generates all the classes
>> without
>>> problems. All classes reflects the schema but at some points, the
>>> element
>>> is a JAXBElement. For some reason, wsdl2java did not generate a class
>>> Value
>>> with children Family and Given classes. Can someone help in getting
>> rid
>>> of
>>> those JAXBElement classes or can tell me how to get the value "toto"
>>> and
>>> "tata" from my objects?
>> In my experience, the reason xjc "punts" on the code generation is when
>> it finds constructs that can't map directly from schema to Java.  For
>> instance, it's legal inside an XML schema element to define two elements
>> with the same name. Once you do that, however, xjc cannot generate
>> strongly-typed properties in the class representing that element.  I
>> suppose it's possible you could integrate xjc bindings that would
>> override this, but I'm not sure.
> 
> Information on JAXB customizations (if that turns out to be the solution):
> http://www.jroller.com/gmazza/entry/customizing_jaxb_artifacts
> 
> Glen
> 
> -- 
> Glen Mazza
> Software Engineer, Talend (http://www.talend.com)
> blog: http://www.jroller.com/gmazza
> 

Glen Mazza-3 wrote:
> 
> On 10.03.2011 16:28, KARR, DAVID (ATTSI) wrote:
>>> -----Original Message-----
>>> From: eaudet [mailto:eaudet@jarics.com]
>>> Sent: Thursday, March 10, 2011 1:23 PM
>>> To: users@cxf.apache.org
>>> Subject: How to get String values out of a JAXBElement object
>>>
>>> Hi,
>>>
>>> I am facing an issue with wsdl2java tool. I read in several other
>>> threads
>>> that you can use a binding to get rid of JAXBElement classes. My wsdl
>>> is
>>> using a complex HL7 schema. wsdl2java generates all the classes
>> without
>>> problems. All classes reflects the schema but at some points, the
>>> element
>>> is a JAXBElement. For some reason, wsdl2java did not generate a class
>>> Value
>>> with children Family and Given classes. Can someone help in getting
>> rid
>>> of
>>> those JAXBElement classes or can tell me how to get the value "toto"
>>> and
>>> "tata" from my objects?
>> In my experience, the reason xjc "punts" on the code generation is when
>> it finds constructs that can't map directly from schema to Java.  For
>> instance, it's legal inside an XML schema element to define two elements
>> with the same name. Once you do that, however, xjc cannot generate
>> strongly-typed properties in the class representing that element.  I
>> suppose it's possible you could integrate xjc bindings that would
>> override this, but I'm not sure.
> 
> Information on JAXB customizations (if that turns out to be the solution):
> http://www.jroller.com/gmazza/entry/customizing_jaxb_artifacts
> 
> Glen
> 
> -- 
> Glen Mazza
> Software Engineer, Talend (http://www.talend.com)
> blog: http://www.jroller.com/gmazza
> 


--
View this message in context: http://cxf.547215.n5.nabble.com/How-to-get-String-values-out-of-a-JAXBElement-object-tp3424129p3583222.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: How to get String values out of a JAXBElement object

Posted by Glen Mazza <gm...@talend.com>.
On 10.03.2011 16:28, KARR, DAVID (ATTSI) wrote:
>> -----Original Message-----
>> From: eaudet [mailto:eaudet@jarics.com]
>> Sent: Thursday, March 10, 2011 1:23 PM
>> To: users@cxf.apache.org
>> Subject: How to get String values out of a JAXBElement object
>>
>> Hi,
>>
>> I am facing an issue with wsdl2java tool. I read in several other
>> threads
>> that you can use a binding to get rid of JAXBElement classes. My wsdl
>> is
>> using a complex HL7 schema. wsdl2java generates all the classes
> without
>> problems. All classes reflects the schema but at some points, the
>> element
>> is a JAXBElement. For some reason, wsdl2java did not generate a class
>> Value
>> with children Family and Given classes. Can someone help in getting
> rid
>> of
>> those JAXBElement classes or can tell me how to get the value "toto"
>> and
>> "tata" from my objects?
> In my experience, the reason xjc "punts" on the code generation is when
> it finds constructs that can't map directly from schema to Java.  For
> instance, it's legal inside an XML schema element to define two elements
> with the same name. Once you do that, however, xjc cannot generate
> strongly-typed properties in the class representing that element.  I
> suppose it's possible you could integrate xjc bindings that would
> override this, but I'm not sure.

Information on JAXB customizations (if that turns out to be the solution):
http://www.jroller.com/gmazza/entry/customizing_jaxb_artifacts

Glen

-- 
Glen Mazza
Software Engineer, Talend (http://www.talend.com)
blog: http://www.jroller.com/gmazza



RE: How to get String values out of a JAXBElement object

Posted by "KARR, DAVID (ATTSI)" <dk...@att.com>.
> -----Original Message-----
> From: eaudet [mailto:eaudet@jarics.com]
> Sent: Thursday, March 10, 2011 1:23 PM
> To: users@cxf.apache.org
> Subject: How to get String values out of a JAXBElement object
> 
> Hi,
> 
> I am facing an issue with wsdl2java tool. I read in several other
> threads
> that you can use a binding to get rid of JAXBElement classes. My wsdl
> is
> using a complex HL7 schema. wsdl2java generates all the classes
without
> problems. All classes reflects the schema but at some points, the
> element
> is a JAXBElement. For some reason, wsdl2java did not generate a class
> Value
> with children Family and Given classes. Can someone help in getting
rid
> of
> those JAXBElement classes or can tell me how to get the value "toto"
> and
> "tata" from my objects?

In my experience, the reason xjc "punts" on the code generation is when
it finds constructs that can't map directly from schema to Java.  For
instance, it's legal inside an XML schema element to define two elements
with the same name. Once you do that, however, xjc cannot generate
strongly-typed properties in the class representing that element.  I
suppose it's possible you could integrate xjc bindings that would
override this, but I'm not sure.