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!


47 comments:

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

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

    ReplyDelete
  4. 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
  5. 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
  6. 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
  7. 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
  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.
    safety course in chennai

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

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

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

    ReplyDelete
  14. 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
  15. 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
  16. 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
  17. 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
  18. 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
  19. Thank you for sharing the article. The data that you provided in the blog is informative and effective.
    PostgreSQL Training

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

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

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

    ReplyDelete