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!


23 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