Solved

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

Posted on 2011-02-15
7
296 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

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
stackato and cloud 4 85
eclipse shortcuts 9 54
MySqlDump not dumping triggers 1 31
XML response optional elements 12 40
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
The greatest common divisor (gcd) of two positive integers is their largest common divisor. Let's consider two numbers 12 and 20. The divisors of 12 are 1, 2, 3, 4, 6, 12 The divisors of 20 are 1, 2, 4, 5, 10 20 The highest number among the c…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

770 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