Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to convert CVS file to XML file 1

Status
Not open for further replies.

vikings2007

Programmer
Aug 10, 2007
3
US
Hi everyone,

I hope that y'all are doing well.

I have a question on how to convert CSV file to XML file. This is the case. I have an e-commerce site that requires XML file. On the other hand, I have an inventory list which is in CSV file format.

What is the easiest way to convert the CSV file to XML structure format? can I actually use Excel? or is there any software that will do this?

Thank you in advance.
 
What platform are you working on? Also, do you want the columns to be attributes or individual bits of data or a mix of both. Bits of both is the most difficult one

If everything is an attribute then just produce
Code:
<group>
<item attr1="col1" attr2="col2" .../>
...
</group>
Many languages have a split feature so just read the CSV file in one line at a time and then call split and you'll get an array with one item per column, that is assuming the CSV file doesn't contain stuff in quotes with embedded commas. If it does, then it is a bit more difficult.
 
Hi,

Thank you for your response. Well, I did some more research, and Excel actually could does the conversion. However, the structure is exactly like your code is. The structure what I need is as follow:

<Product>
<ID>SY-SZ230PB</ID>
<ProductName>Sony VAIO VGN-SZ230P/B 13.3 inch Core Duo 1.83GHz</ProductName>
<ProductTypeID>168</ProductTypeID>
<ListPrice>0</ListPrice>
<SitePrice>1999.99</SitePrice>
<SiteCost>0</SiteCost>
<MetaKeywords />
<MetaDescription />
<Product/>

Also, there will be some commas, and embedded quotes.

Thank you.
 
Stylus Studio includes a code generating capability that will create Java to do the conversion (assuming from your problem description that this is more than a one-time requirement). You may read about the capability here.

If it is only a one-time requirement, then here.

My guess is that Altova has a similar capability.

Tom Morrison
 
Hi Herebrain,
Thank you for your response. I have never used XSLT. Is there any reference that I could learn from? is it hard to use XSLT?

Hi K5tm,
Thank you for your response. Well, I tried the Stylus studio, and it still does have problem with the description since there is commas and quotes. But I think I just didn't do it correctly as far as the set up.
 
It isn't hard; I had to do it about 3 years ago to convert multiple formats from different vendors, each of whom did their own thing. (I could have killed our manager, who kept telling vendors to just put their data in a CSV file. Then he'd open it in Excel, which would screw it up. "Don't do that!" never worked. Dumb@ss. Wait, I'm ranting.)

Take a look at faq426-6460 for resources.

I'll see if I can dig up some of my old code tonight, as I no longer work where I did that.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top