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!


74 comments:

  1. Replies
    1. IEEE Final Year projects Project Centers in Chennai are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes. IEEE Final Year project centers ground for all fragments of CSE & IT engineers hoping to assemble.Final Year Projects for CSE

      Spring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Spring Framework Corporate TRaining .

      Specifically, Spring Framework provides various tasks are geared around preparing data for further analysis and visualization. Spring Training in Chennai

      The Angular Training covers a wide range of topics including Angular Directives, Angular Services, and Angular programmability.Angular Training

      Delete
  2. 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
  3. 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
  4. This comment has been removed by the author.

    ReplyDelete
  5. 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
  6. This comment has been removed by the author.

    ReplyDelete
  7. • 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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
  16. Amazing blog about the various informative information on the programming languages... Java Training in Chennai | Python Training in Chennai

    ReplyDelete
  17. 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
  18. 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
  19. well! Thanks for providing a good stuff related to DevOps Explination is good, nice Article.
    DevOps Online Training

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

    ReplyDelete
  21. 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
  22. 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
  23. Thanks for sharing this useful information.
    python training

    ReplyDelete
  24. 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
  25. 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
  26. 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
  27. 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
  28. 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
  29. Thank you for sharing the article. The data that you provided in the blog is informative and effective.

    Tableau Training in Hyderabad

    ReplyDelete
  30. Thank you for sharing the article. The data that you provided in the blog is informative and effective.
    PostgreSQL Training

    ReplyDelete
  31. Salesforce communities have an important role in improving the connection among the customers, channel partners and internal employees of a company. lightning interview questions 2021

    ReplyDelete

  32. I am really happy to say it’s an interesting post to read . I learn new information from your article , you are doing a great job . Keep it up

    Devops Training in Hyderabad

    Hadoop Training in Hyderabad

    Python Training in Hyderabad

    Tableau Training in Hyderabad

    Selenium Training in Hyderabad

    ReplyDelete
  33. I believe there are many more pleasurable opportunities ahead for
    individuals that looked at your site.
    oracle dba training in Chennai
    best java training in Chennai
    node js certification training in Chennai

    ReplyDelete
  34. Thanks for the valuable information shared here, Keep sharing https://powerbipros.in/

    ReplyDelete
  35. Thank you for sharing such a nice information. iFuture Technologies Private Limited is computer courses in thane & Kalyan

    ReplyDelete
  36. There will be best course in Hyderabad, duck creek policy program,duck creek can earn a salary 130000 per annum.it is useful to students career in the field you have interest visit my website link.thanks youhttps://duckcreektraining.com/

    ReplyDelete