JAXB Tip

If you are using JAXB for XML to Java binding and geting javax.xml.bind.UnmarshalException: Unexpected element blah blah blah... the first thing you can do is to call setValidating(true) on Unmarshaller object. i.e.

   JAXBContext jc = JAXBContext.newInstance("com.sun.java.xml.ns.portlet.portlet_app_2_0");
   // create an Unmarshaller
   Unmarshaller u = jc.createUnmarshaller();
    u.setValidating(true); 

This will help in getting little more details about the problem. For example, if your schema has declared <sequence> constraints on certain elements and your XML is not following it, the validation handler will report that certain tag name (element) is NOT allowed at this place and what all possible elements can appear after certain element.

One caveat here - setValidating() is a deprecated method and should be used only for debugging purpose. 

Comments:

You work at sun? And you are recommending the use of deprecated methods which in 1.6 even cause UnsupportedOperationExceptions? Not really?

Posted by Michael on June 11, 2007 at 07:04 AM IST #

Thanks for your comment Michael. I know that setValidating() is deprecated but my intention was to tell a quick solution for finding about the actual problem. I added caveat at the end.

Posted by Manish on June 11, 2007 at 07:32 AM IST #

Post a Comment:
Comments are closed for this entry.
About

manishkg

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today