Monday, 21 September 2015

JAXB - XML to Java in 2 lines of Code!

A couple of weeks ago I was presented with a bunch of xml files for which I was also given the xsd document.  The task was to calculate some metrics based on properties of the xml files.

Not too complicated.  All I had to do was:

  1. Convert the xml files to Java objects.
  2. Write some code to perform a calculation on said objects.
I hadn't actually tried this before but I challenged myself to do this (at least the boilerplate of point 1) in 2 lines of code. Here's how I did it:

Step 1 - Create a Java data model

I created a project in IntelliJ and dropped my xsd document in a resources directory.

I then highlighted the xsd file and clicked Tools->JAXB->Generate Java Code


I was presented with a dialogue box as below


Click OK and hey presto your whole data model has been created into Java objects.

Step 2 - Deserialise the xml file into Java Objects  

//line 1 create an Unmarshaller for the object type you are reading from the xml file
Unmarshaller um = JAXBContext.newInstance(DataObject.class).createUnmarshaller();
//line 2 deserialise the xml file into a java object
DataObject dataOject = (DataObject)um.unmarshal(new FileReader("DataObject.xml"));
//now run the calculation on dataObject

So that's it - only took 2 lines!


59 comments:

  1. I have read your blog its very attractive and impressive. I like it your blog.

    Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

    Java Online Training Java Online Training JavaEE Training in Chennai Java EE Training in Chennai

    ReplyDelete
  2. Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.

    Java Training in Bangalore

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Thanks for one marvelous posting! I enjoyed reading it; you are a great author. I will make sure to bookmark your blog and may come back someday. I want to encourage that you continue your great posts, have a nice weekend!


    Java Training in Chennai


    Java Training in Bangalore


    Java Training in Bangalore

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. • Nice and good article. It is very useful for me to learn and understand easily. Thanks for sharing your valuable information and time. Please keep updating. Power Bi Online Training Bangalore

    ReplyDelete
  7. Those guidelines additionally worked to become a good way to
    recognize that other people online have the identical fervor like mine
    to grasp great deal more around this condition.


    java Training in Bangalore

    ReplyDelete
  8. I and my friends were going through the nice, helpful tips from the blog then the sudden came up with an awful suspicion I never expressed respect to the website owner for those secrets.

    Devops Training in Bangalore

    ReplyDelete
  9. I really enjoyed while reading your article, the information you have delivered in this post was damn good. Keep sharing your post with efficient news.
    ME/M.Tech Project Center in Chennai | ME/M.Tech Project Center in Velachery

    ReplyDelete
  10. Nice and good article. It is very useful for me to learn and understand easily. Thanks for sharing your valuable information and time. Please keep updating.
    oracle training in chennai|| hadoop training in chennai

    ReplyDelete
  11. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging…
    Regards,
    hadoop training in chennai || selenium training in chennai




    ReplyDelete
  12. you have done a good task buddy. just keep posting thi type of articles here. or if want to learn DevOps TRaining in Gurgaon then visit us.

    ReplyDelete
  13. Those guidelines additionally worked to become a good way to recognize that other people online have the identical fervor like mine to grasp great deal more around this condition.
    safety course in chennai

    ReplyDelete
  14. well! Thanks for providing a good stuff related to DevOps Explination is good, nice Article
    anyone want to learn advance devops tools or devops online training
    DevOps Online Training

    ReplyDelete
  15. Amazing blog about the various informative information on the programming languages... Java Training in Chennai | Python Training in Chennai

    ReplyDelete
  16. Thanks for posting useful information. You have provided a nice article, Thank you very much for this one... Java Training in Chennai | Pega Training in Chennai

    ReplyDelete
  17. Hello,
    Thank You Rational Java for the post update regularly. KernelTraining is one of the leading DevOps training institutes. Recently i completed my software course in KernelTraining, good Institute where you can get good knowledge by real-time experts. I would strongly recommend KernelTraining. for more details please go through the link: DevOps Training In Hyderabad

    ReplyDelete
  18. well! Thanks for providing a good stuff related to DevOps Explination is good, nice Article.
    DevOps Online Training

    ReplyDelete
  19. Thanks for the info on Rational Java JAXB - XML to Java in 2 lines of Code!
    DevOps Training

    ReplyDelete
  20. An astounding web diary I visit this blog, it's inconceivably magnificent. Strangely, in this current blog's substance made point of fact and sensible. The substance of information is instructive.
    A bewildering web journal I visit this blog, it's unfathomably heavenly. Oddly, in this present blog's substance made purpose of actuality and reasonable. The substance of data is informative
    Oracle Fusion Financials Online Training
    Oracle Fusion HCM Online Training
    Oracle Fusion SCM Online Training

    ReplyDelete
  21. Thank you for sharing wonderful information with us to get some idea about jaxb to xml content. check it once through Devops Training | DevOps Training in Hyderabad | DevOps Online Course

    ReplyDelete
  22. Thanks for sharing this useful information.
    python training

    ReplyDelete
  23. Your info is really amazing with impressive content..Excellent blog with informative concept. Really I feel happy to see this useful blog, Thanks for sharing such a nice blog..
    If you are looking for any Data science Related information please visit our website Data Science Course In Bangalore page!

    ReplyDelete
  24. I just loved your article on the beginners guide to starting a blog.If somebody take this blog article seriously in their life, he/she can earn his living by doing blogging.thank you for thizs article. best devops online training

    ReplyDelete
  25. There's definately a great deal to find out about this subject. I like all the points you made. oneplus 7 pro display replacement bangalore You made some good points there. I looked on the net to learn more about the issue and found most people will go along with your views on this web site. vivo V17 pro display replacement marathahalli

    ReplyDelete
  26. Howdy! This article couldn’t be written any better! Looking at this article reminds me of my previous roommate! He constantly kept talking about this. I am going to send this article to him. Pretty sure he's going to have a very good read. I appreciate you for sharing! iphone 11 pro display replacement marathahalli Greetings, There's no doubt that your website may be having web browser compatibility problems. When I take a look at your website in Safari, it looks fine however, when opening in I.E., it has some overlapping issues. I just wanted to provide you with a quick heads up! Other than that, fantastic website! samsung M30s display replacement

    ReplyDelete
  27. Your style is unique in comparison to other people I've read stuff from. I appreciate you for posting when you have the opportunity, Guess I'll just bookmark this site. redmi note 8 pro display replacement bangalore I was able to find good information from your content. realme x display replacement

    ReplyDelete