aspose file tools*
The moose likes Web Services and the fly likes Opinions on parsing XML to JSON design Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "Opinions on parsing XML to JSON design" Watch "Opinions on parsing XML to JSON design" New topic
Author

Opinions on parsing XML to JSON design

David McWilliams
Ranch Hand

Joined: Mar 14, 2009
Posts: 73
Can any give their opinions on my design?

Write Java web service to:

Call external web service that returns an XML document as a String of 'Car' information.
Search this xml/string object for particular Car (bring me back all Fords).
Return all Fords as a JSON object.
Solution:

Call external web service to get an XML file (as string) of all Car information.
Using JAXB, parse this String object into multiple Car objects.
Loop through the car objects and when I find a Ford, add it to a Car list.
Loop through Ford list and convert the list into a JSON object.
Return JSON object from web service.

I need my solution to demonstrate good coding practices, not just get a working solution.

What do you think?
H Paul
Ranch Hand

Joined: Jul 26, 2011
Posts: 381
Can you clarify? Is that soapful or restful web service?
Call external web service to get an XML file (as string)
of all Car information.
David McWilliams
Ranch Hand

Joined: Mar 14, 2009
Posts: 73
H Paul wrote:Can you clarify? Is that soapful or restful web service?
Call external web service to get an XML file (as string)
of all Car information.


It's not either really. All I have is an interface with one method: getAllCars();

My program has 2 interfaces:
1. Gets all cars in XML string.
2. Call interface 1. method getAllCars() and parse the data.

I have to implement interface number 2. Interface number 1 is mocked.

So it's just a trivial little program. I suppose I could wrap the 2 Interfaces with a web service later.
H Paul
Ranch Hand

Joined: Jul 26, 2011
Posts: 381
So mechanically:
1. Use JAXB to unmarshall xm string into a list of xml annotated Car class (ford)
2. Then use Jackson Json to jsonize the list of xml annotated Car class.
David McWilliams
Ranch Hand

Joined: Mar 14, 2009
Posts: 73
H Paul wrote:So mechanically:
1. Use JAXB to unmarshall xm string into a list of xml annotated Car class (ford)
2. Then use Jackson Json to jsonize the list of xml annotated Car class.


Thanks for your reply.

I have made progress on the application and here it is:

I have witten the following methods:

String getAllCars();
Calls Interface 1 above returning all cars as a sample XML file in a string format (I must adhere to this exact format).

List<Cars> parseCars()
Generates a list of Car objects based on the XML file. The Car object has other sub objects, e.g. owner.

List<Cars> searchCars(String make)
Loops through the list of cars from above and finds any cards specified by make.
Return a new list of those cars.

String convertToJSON()
Converts the list above into a JSON object.

This JSON object is then returned to the user.

The only issue I have is that the XML file and the JSON formats differ. E.g. instead of the Person object in the XML, the JSON just wants the person name, no other data. I must adhere to the JSON format given.

My question is, what is the best design for this? Would it be best to have to groups of Car objects (and sub objects), e.g. a group that are used to store the XML data, and a new group that are used to convert to JSON. I could write a convertor methods to do the conversion.

Would that be a good design or can you hink of a more effecient method? Is 2 groups of objects excessive?
H Paul
Ranch Hand

Joined: Jul 26, 2011
Posts: 381
Have you look into Jackson Json Annotation?
David McWilliams
Ranch Hand

Joined: Mar 14, 2009
Posts: 73
H Paul wrote:Have you look into Jackson Json Annotation?


No but I will. Can you tell me what part of the problem it will solve? I'm already using Gson..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Opinions on parsing XML to JSON design
 
Similar Threads
issue while getting the value from JSON string
JSON Artifacts for Web Service
ArrayList is Picking Up Last Item During Iteration.
javascript library which can convert html form to an xml ?
Object type casting