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
10 Responses
Christian Ullenboom
May 18th, 2007 at 8:32 am
1One 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
TIll
May 18th, 2007 at 9:26 am
2Nice one! thx
fromvega
May 18th, 2007 at 10:22 am
3Thank you for the tip Christian!
Free Ebooks Collection
July 28th, 2007 at 1:26 pm
4gr8 help buddy thanks. keep it up
niedkumkua
July 31st, 2007 at 12:07 pm
5Hello! Good Site! Thanks you! omkuzszgba
Ankita Tapkire
August 6th, 2007 at 2:49 am
6A 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?
fromvega
August 6th, 2007 at 11:46 am
7Hello 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.
Pinky
December 7th, 2007 at 6:59 am
8Concise, 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.
java xml
February 2nd, 2008 at 12:56 am
9Thank you for the tip Christian!
Sparky
March 13th, 2008 at 8:54 am
10Thanks for the concise writeup, really helped!
RSS feed for comments on this post · TrackBack URI
Leave a reply