• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Error while unmarshalling xml

 
Ranch Hand
Posts: 75
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I am facing issue while unmarshalling an xml to create the corresponding java object




On running the above program, I am getting below error

 
Khuzema Dharwala
Ranch Hand
Posts: 75
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the xsd files

PDNotification-20180914.xsd

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
Khuzema Dharwala
Ranch Hand
Posts: 75
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,

I am facing issue while unmarshalling the xml file using below code




Getting following error while running the code




The xml file is

 
Khuzema Dharwala
Ranch Hand
Posts: 75
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Attaching the first xsd file

InfoMessageBroker-v2016.xsd

 
Khuzema Dharwala
Ranch Hand
Posts: 75
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Attaching the second xsd file

PDNotification-20180914.xsd

 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Khuzema Dharwala wrote:


So apparently the unmarshal method doesn't directly return the object, but instead JAXBElement wrapping the object. Cast to JAXBElement<EventMessageType> instead, then retrieve its value.

To get rid of the unchecked cast warning you'll get, you can also use a StreamResource to get a more type-safe way:
 
Khuzema Dharwala
Ranch Hand
Posts: 75
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rob
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.
 
Khuzema Dharwala
Ranch Hand
Posts: 75
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rob,

Now I am getting following error when I am trying to unmarshal the xml

Not sure why it is looking for pdBranchCustomerNotification in namespace "http://www.db.com/2016/InfoMessageBroker/Types"


 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In previous examples of your XML I noticed that you have the n1 prefix defined in both the root element and the element that is giving you problems. My guess is that the XML that is giving you issues doesn't have the redefinition in the pdBranchCustomerNotification element, so it uses the definition from the root element.
 
Khuzema Dharwala
Ranch Hand
Posts: 75
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Stephan

'pdBranchCustomerNotification' is defined in the xsd PDNotification-20180914.xsd having targetNameSpace = "http://www.example.org/PDNotification" but unmarshaller is not taking it.

I am using jrocket 1.6 which has jaxb implementation  vJAXB 2.1.10
 
Ranch Hand
Posts: 734
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is how you should do.

[1] In the InfoMessageBroker-v2016.xsd, add the import (add path to the schemaLocation, if necessary) and modify the root. This is the first two lines followed by the definition of the element EventMessage as written in your original xsd.


[2] Then inside the same schema, change the type to the EventBody line


[3] Furthermore, to reinforced validation, you should really change the processContents attribute from skip to lax, or preferrably to strict, since you take the trouble to construct the schema for the namespace pbn surely for a purpose, and to import it.


[3.1] If you keep the processContents skip, you don't even need to import the PDNotification-20180914.xsd into InfoMessageBroker-v2016.xsd and the validation of the message will go through without any problem at that level already.

That's all you need to do.

ps: This should also be the answer to your another thread
   https://coderanch.com/t/702056/languages/Suggestion-tag-xsd
which is practically the same as this one.
 
Khuzema Dharwala
Ranch Hand
Posts: 75
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks tsuji,

I followed the steps but now I am getting this error when trying to generate the java pojos  from the changed xsd "InfoMessageBroker-v2016.xsd"

parsing a schema...
[ERROR] src-resolve: Cannot resolve the name 'pdn:EventBodyType' to a(n) 'type definition' component.
 line 18 of file:/C:/Users/dharkhu/eclipse/temp/myTest/TestJAXB/src/InfoMessageBroker-v2016.xsd


This is because EventBodyType is not defined inside "PDNotification-20180914.xsd"
 
g tsuji
Ranch Hand
Posts: 734
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I see. The error message is quite correct. I have not read the schema too carefully. Just restore [2] to the original as such.

[2] Amendment: restore to the original - EventBodyType is defined in imb.
 
g tsuji
Ranch Hand
Posts: 734
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me see. In my section [3], I was referring to the schema of Broker using xs:any and xs:anyAttribute shown in your other thread 702056 that I was reading.

In the present thread here, you are not using that kind of constructions. It is fine, but the answer needs some adjustment. Here is how.

You've already import the Notification schema, prefixed tns (which I was referring as pbn in [1]). Though you can do that but it is kind of confusing as tns commonly referring to targetNamespace. Ok, I take it as you have written, fine. So to answer to the problem in the schema set as presented in this thread (rather than referring to that in 702056), this is the complete rundown of modifications.

[I] Change the corresponding lines here.


and it seems to be enough.

For my previous posts, sections [1], [2] (amended) and [3], all apply to your other thread 702056, with xs:any and xs:anyAttribute constructions. I hope this is clearer now.
 
Khuzema Dharwala
Ranch Hand
Posts: 75
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tsuji,

Thanks now I am able to unmarshall the xml (without doing schema validation), but when I am trying to validate the xml by creating the schema object from the InfoMessageBroker-v2016.xsd and setting it into the Unmarshaller object, it is giving me following error

My main method is as below





The error which I am getting while trying to unmarshal with schema validation.





 
g tsuji
Ranch Hand
Posts: 734
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



The only reason it can occur is that you haven't made the change according to [I] where @ref is use. Have you made that change properly and accordingly?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic