Advertisement

02.13.2008 at 08:33AM PST, ID: 23160072
[x]
Attachment Details

JAXB unmarshall annotations question

Asked by pzaprianov in Extensible Markup Language (XML), Java Programming Language, JAXP & SAX (XML APIs)

Tags:

Hi all,

I am trying to unmarshall and XML file but when it has a complex type, i don't know how to put the JaxB annotations on my class, in the example that I post SubClass is the ComplexType in my XML.

TksStart Free Trial
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:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
@XmlRootElement 
class Foo {
  @XmlElement
  String a;
  @XmlElement(required=true)
  String b;
 
protected SubClass c;
 
public SubClass getSubClass(){
     return c;
}
 
public SubClass setSubClass(SubClass sc){
    this.c = sc;
}                           
 
}
 
-------------------------------------------------------
public class SubClass
-------------------------------------------------------
protected long[] local_long ;
 
public  long[] get_long(){
   return local_long;
 }
 
public void set_long(long[] param){
   this.local_long = param;                         
}                        
 
-------------------------------------------------------
myJaxb.java
-------------------------------------------------------
 
    JAXBContext jc = JAXBContext.newInstance(new Class[] {jaxb.Foo.class});
      
          Unmarshaller u = jc.createUnmarshaller();
      
          Foo fooObj = (Foo)u.unmarshal(
              new FileInputStream( "foo.xml" ) ); 
 
---------------------------------------
foo.xml
----------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<foo>
         <a>1</a>
         <b>2</b>
         <subClass>
               <long>13</long>
               <long>44</long>
         </subClass>
</foo>
[+][-]02.13.2008 at 11:43PM PST, ID: 20891653

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Extensible Markup Language (XML), Java Programming Language, JAXP & SAX (XML APIs)
Tags: java
Sign Up Now!
Solution Provided By: CEHJ
Participating Experts: 1
Solution Grade: B
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628