Easy XML with Java - XML Binding
Recently in a Java project that I’m currently working on it was necessary to read remote XML data. Since my Java skills aren’t too strong and that I’ve never used Java and XML together I hit the road to search for help. I quickly found some articles and tutorials about dealing with XML within Java. I was trying to understand some concepts from one of the articles when I glimpsed the article year and to my surprise: 1996. Wow! I wasn’t looking for an history class so I moved on.
I have found other older articles in the way and even have tried on some code until I was pointed to the right direction by some buddies. That’s a big point of confusion to beginners and inexperienced Java developers in my opinion. There is a ton of information about Java out there and we must take care not to get caught by some old and deprecated functionality. But since I got it right let’s stop complaining and go strait to the point!
We are going to use the Java Architecture for XML Binding (JAXB). What is so special about it? Well, it takes an XML Schema and automatically creates all necessary classes with setters and getters to allow easy manipulation of the XML data. It binds the XML file to our code in such a way that we don’t have to know much about XML itself. The JAXB is already included in the new Java 1.6!
First we need an example XML file so we can generate the XML Schema but if you prefer you can write it by yourself! For this example I’m using the books.xml file which contains a list of books (you can download all files at the end of this post). Now that we have the XML sample we need a tool to generate the schema for us. I recommend this site: XML-2-XSD. It’s quick and easy. Input the .xml file and catch the resulting .xsd.
The second step is to generate the class files from the .xsd file. We are going to use a JAXB tool called ‘xjc’. It’s also very easy, just type the following in your command prompt to get all files:
$ xjc -p myxml books.xsd
The above command will generate all necessary class files and put them into a package called ‘myxml’. Now you can take a look into the generated source code to have an idea of what is coming next.
Note: I’m assuming that you have the Java bin folder in your system path. Otherwise you should access the ‘xjc’ tool from there.
The final step is the code that will take any XML file and bind it to our application. The act of getting the XML data is called ‘unmarshaling’. In the example I’m getting data from a local file but you can easily get it from a remote source passing an URL to the unmarshal method instead.
// starts the binding process JAXBContext jc = JAXBContext.newInstance("myxml"); Unmarshaller u = jc.createUnmarshaller(); // unmarshal the xml file Books books = (Books) u.unmarshal( getClass().getResource("books.xml") );
Did you get it? See how now we have a Books type? It is the root node from where we get everything else. We also have a type for each XML node. This is due to the code generated by JAXB. Let’s keep moving!
// get the list of books List<Books.Book> bookList = books.getBook(); // iterate through the list to get each book for(int i=0; i < bookList.size(); i++){ // get book Books.Book book = bookList.get(i);
From the code above you can see that we use the getter method getBook() to retrieve the List of books. Then we iterate through it to get each book node. We store each node in the book variable which is a Books.Book type. Directly from the Books.Book type we have access to all book nodes as follows:
// get data String description = book.getDescription(); String format = book.getFormat(); String isbn = book.getISBN(); String itemId = book.getItemId(); String language = book.getLanguage(); String name = book.getName(); String pages = book.getPages(); String publication = book.getPublicationDate(); String publisher = book.getPublisher();
Pretty easy! If an XML tag have other child tags we need to loop through it to get each node like we did with the root node. And again we use a loop to get the authors.
// get the list of authors List<Books.Book.Authors> authorList = book.getAuthors(); // iterate through the list to get each author for(int y=0; y < authorList.size(); y++){ // get author Books.Book.Authors author = authorList.get(y); // get data String authorName = author.getAuthorName(); } }
And that’s all. A lot of talking for a small amount of code! In this example I have just stored the XML data into variables but you should be able to do much more. The key point is to understand the types generated by JAXB and use the getters to retrieve the information. Here I used lists to get all nodes but if you already know which node you need to access you can get it directly like this:
Books.Book.Authors author = book.getAuthors().get(0).getAuthorName();
If you have any doubt feel free to leave a message. I’ll be glad to help whenever I can.
Files:
EasyXML Code
books.xml
books.xsd
May 18th, 2007 at 8:32 am
One can add that there is a nice Eclipse-Plugin on https://jaxb-workshop.dev.java.net/plugins/eclipse/xjc-plugin.html for generating the JavaBeans.
Christian
May 18th, 2007 at 9:26 am
Nice one! thx
May 18th, 2007 at 10:22 am
Thank you for the tip Christian!
July 28th, 2007 at 1:26 pm
gr8 help buddy thanks. keep it up
July 31st, 2007 at 12:07 pm
Hello! Good Site! Thanks you! omkuzszgba
August 6th, 2007 at 2:49 am
A nice learning tanks for this.
But just wanted to know one more thing if i can get to know how to convert .xml file to .xsd one thru java programming?
August 6th, 2007 at 11:46 am
Hello Ankita,
I’m sorry but I do not remember any java tool for the job but you can always create your own code for that. A bit of work indeed but if a search can’t show you anything that’s the way to go.
December 7th, 2007 at 6:59 am
Concise, efficient write-up. I implemented this method in no time and took maybe 30 lines of code to 8. Thanks, champ, keep up the good work.
February 2nd, 2008 at 12:56 am
Thank you for the tip Christian!
March 13th, 2008 at 8:54 am
Thanks for the concise writeup, really helped!
June 7th, 2009 at 9:54 pm
Thanks for the help fromvega! Initially when I faced the prospect of having to use XML for my project, I was dismayed by all the JAXP stuff online that left me braindead. But then I searched for “easy java xml” and I hit upon your site! … I got my work done in less than 10 minutes!
Really appreciate you help fromvega!
I wish more tech how to articles are written like in your style!
Cheers!
Unni
November 13th, 2010 at 3:44 pm
i run the code from EasyXML.java but these errors shown ….
——
Exception in thread “main” javax.xml.bind.JAXBException: “myxml” doesnt contain ObjectFactory.class or jaxb.index
at com.sun.xml.internal.bind.v2.ContextFactory.createContext(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at javax.xml.bind.ContextFinder.newInstance(Unknown Source)
at javax.xml.bind.ContextFinder.find(Unknown Source)
at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
at EasyXML.(EasyXML.java:25)
at EasyXML.main(EasyXML.java:73)
——————————————–
how can i solve it
———— moreover i need a code that can add a new elemnt in an existing .xml file have u done this type of code with java if so then please give me an example