Solved

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

Posted on 2011-02-15
7
298 Views
Last Modified: 2012-08-14
Hi,
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.

Thanks.
0
Comment
Question by:orazen12
  • 3
  • 3
7 Comments
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34897638
So you just want to replace anything in the form of "x":"y" to <x>y</x>?
Then maybe {tagsandvalues} to <record>tagsandvalues</record>?
0
 
LVL 16

Accepted Solution

by:
Swapnil Piparia earned 500 total points
ID: 34897664
Hi orazen12,

The format that you specified is JSON. Below article has step by step solution to convert JSON to XML in Java.

http://answers.oreilly.com/topic/279-how-to-convert-json-to-xml-in-java/

Thanks,
netswap
0
 

Author Comment

by:orazen12
ID: 34898295
Thanks netswap.
One more question, how do I add the dependency. I did a lot of Google search but could not find that.
 
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 16

Expert Comment

by:Swapnil Piparia
ID: 34898522
Hi orazen12,

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

      1. Using eclipse
           http://www.avajava.com/tutorials/lessons/how-do-i-add-a-project-as-a-dependency-of-another-project.html
       2. Websphere
           http://publib.boulder.ibm.com/infocenter/wmbhelp/v6r1m0/index.jsp?topic=/com.ibm.etools.mft.doc/ac30280_.htm

        More info : http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

        Hope this will help.

Thanks,
netswap
0
 

Author Comment

by:orazen12
ID: 34940814
netswap,
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.

Thanks.
0
 

Author Comment

by:orazen12
ID: 34942920
netswap,
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 www.json.org/java/ 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?

Thanks.
0
 
LVL 16

Expert Comment

by:Swapnil Piparia
ID: 34943527
Hi orazen12,

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

       this might help you : http://www.mkyong.com/maven/maven-dependency-libraries-not-deploy-in-eclipse-ide/ with maven problem.


Thanks,
Netswap.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
When there is a disconnect between the intentions of their creator and the recipient, when algorithms go awry, they can have disastrous consequences.
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question