Solved

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

Posted on 2011-02-15
7
293 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now