• Post Reply Bookmark Topic Watch Topic
  • New Topic

Cannot get XML output in simple JAXB example  RSS feed

 
sid smith
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using a bunch of broken tutorials to learn JAXB. I have one at - https://jaxb.java.net/tutorial/section_1_3-Hello-World.html

I am unable to get any XML output. I am unable to figure out how to send the contents of System.out to a String that can be printed. I saw an SO post and tried using ByteOutputStreamArray, but that did not work. Please help me to get the XML in output.

Thanks.

XML Schema -



Java code -

 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marshal the object to a StringWriter, not to System.out. Then get the String from that StringWriter.
 
Ron McLeod
Bartender
Posts: 1603
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your error is in line 36: JAXBContext jc = JAXBContext.newInstance("jaxbKenaiDemo");

It should specify the contextPath JAXB classes, not your application. Change it to: JAXBContext jc = JAXBContext.newInstance("jaxbKenaiTutorial");

Also, it you want to make the output prettier, you can add this: m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
 
sid smith
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ron McLeod wrote:Your error is in line 36: JAXBContext jc = JAXBContext.newInstance("jaxbKenaiDemo");

It should specify the contextPath JAXB classes, not your application. Change it to: JAXBContext jc = JAXBContext.newInstance("jaxbKenaiTutorial");

Also, it you want to make the output prettier, you can add this: m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);


Thanks. Got it !
 
sid smith
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Marshal the object to a StringWriter, not to System.out. Then get the String from that StringWriter.


Can you please tell me why ? Thank you.
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why? Well, that was my guess at an answer to your question:

I am unable to figure out how to send the contents of System.out to a String that can be printed.


Now that I read it again, I see that it isn't well-formed. After all the "contents" of System.out -- by which I think you meant everything which had already been sent to System.out -- have already been "printed" for some meaning of that word. So it's hard to really tell what you meant by that question. If my assumption that you wanted to marshal the XML data to a String was correct, then what I said is the answer to that. Of course all Strings can be "printed", so that part of the question is unnecessary.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!