• Post Reply Bookmark Topic Watch Topic
  • New Topic

Unable to get value of XML element  RSS feed

 
Bubba Johnson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using JAXB, I can get parent element <name> but not value of <pages>

CODE:



XML:

I have the pojos if you need them. I would like someone to take a shot at the pojos needed as I believe that is where is issue resides.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code won't compile, because of duplicate variable declarations for jaxbContext at lines 9 and jaxbUnmarshaller at line 11.
You didn't mention these as compilation problems you yourself encountered, so I'm going to assume these are copy-paste errors.
The problem you indicated at line 25 occurs, because the getBook() method retuns a List containing Book objects; not a Book (based on line 17).
That means you cannot invoke the getPages() method, because List has no such method. you should probably iterate the List like you did at line 19, and for each Book invoke the getPages() method.
Then, assuming your binding classes are correct, your program should compile and run.
 
Bubba Johnson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jelle for reformatting my questions and your response. And 'yes' the compilation errors were ultimately due to my copy-and-paste "expertise"... LOL
 
Bubba Johnson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I cleaned up my code and implemented your suggestions Jelle and it compiles but I get a runtime error:

So this points to an error in my POJOs, which are included toward the end of this reply.

If you could lend a hand, 'bartender', that would be a great help to this 'greenhorn' and would be appreciated.

CODE:


XML:


POJOs:

Library



Book



Pages

 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I suppose you would be seeing that error. By default a compliant JAXB implementation looks for pubic members (XmlAccessType.PUBLIC_MEMBER), which are public fields and JavaBean style getters/setters. From the JavaDoc of XmlAccessType
XmlAccessType API wrote:
public static final XmlAccessType PUBLIC_MEMBER

Every public getter/setter pair and every public field will be automatically bound to XML, unless annotated by XmlTransient. Fields or getter/setter pairs that are private, protected, or defaulted to package-only access are bound to XML only when they are explicitly annotated by the appropriate JAXB annotations.

If you apply that to the binding classes you provided, you'll notice that Library has a public field book and a public getter/setter pair. The same goes for the pages member of the Book class. Also, if you take the above quote into account there are some superfluous annotations you could remove.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!