Link to home
Start Free TrialLog in
Avatar of orazen12

asked on

How do I convert data in a specific format to XML using Java?

I have some data in a file in a specific format. A sample of the same is given below. I need to convert this data to XML

{ "tag1": "46709" ,"tag2" : "Tom" ,"tag3" : "John" ,"tag4" : "501.1" ,"tag5" : "USD 941.70" ,"tag6": "0" ,"tag7":"3:59PM MST" ,"tag8" : "Feb 15, 3:59PM MST" ,"tag9" : "-31.75" ,"tag10" : "3.49" ,"tag11" : "abc" } ,{ "tag1": "20437" ,"tag2" : "dave" ,"tag3" : "matt" ,"tag4" : "5,481.00" ,"tag5" : "USD 5,481.00" ,"tag6": "0" ,"tag7":"4:27PM MST" ,"tag8" : "Feb 15, 4:27PM MST" ,"tag9" : "+171.00" ,"tag10" : "3.22" ,"tag11" : "chg" }

In the above data, I've copied two rows. The two rows are separated by curly brackets and a comma (} ,{). Also, the tag and the value are separated by a colon (:). I could simply insert this data in a database but in my situation there's a possibility that an additional tag could get added in this data. Hence my thought of using XML as a tag.

I'll be using Java for my application. Any help with the same would be appreciated.

Avatar of TommySzalapski
Flag of United States of America image

So you just want to replace anything in the form of "x":"y" to <x>y</x>?
Then maybe {tagsandvalues} to <record>tagsandvalues</record>?
Avatar of Swapnil
Flag of India image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of orazen12


Thanks netswap.
One more question, how do I add the dependency. I did a lot of Google search but could not find that.
Hi orazen12,

      The method depend on which tool you are using for development.

      1. Using eclipse
       2. Websphere

        More info :

        Hope this will help.

Thanks for the links and sorry for the delay in reply as I had been travelling. I'll check the links and get back to you.

I'm having trouble using Maven. I've installed it on my computer and have downloaded a sample project. I'm trying to use Maven with Eclipse but am not able to. I googled and found another way to do it. The url has the class files for the conversion. I downloaded that and wrote a small program to convert it.

Though I've found the solution, I'd still like to know if I have to use Maven if I take the approach suggested by you or can it be done without Maven too?

Hi orazen12,

       I haven't try without maven. So not sure about it.

       this might help you : with maven problem.