I have a csv file and i want to convert the same in Xml tree structure can some one tell me how is that possible

the xml structure will have many child element

as below:

<parents>
<child>
<name1/>
<name2/>
</child>
<child>
<name1/>
<name2/>
</child>
</parents>

Recommended Answers

All 3 Replies

Assuming an input file such as example.csv (contains no accented or non-ascii characters)

fname,lname
bugs,bunny
donald,duck
betty,boop

You could use XML::CSV in a script such as the following:

#!/usr/bin/perl
use strict;
use warnings;
use 5.010;

use XML::CSV;
my $csv_obj = XML::CSV->new();
$csv_obj->parse_doc("example.csv", {headings => 1});#file starts with header row
$csv_obj->print_xml("output.xml",
                    {format => " ",
                     file_tag => "children",
                     parent_tag => "child"});

output.xml now contains:

<children>
 <child>
  <fname>bugs</fname>
  <lname>bunny</lname>
 </child>
 <child>
  <fname>donald</fname>
  <lname>duck</lname>
 </child>
 <child>
  <fname>betty</fname>
  <lname>boop</lname>
 </child>
</children>

But what if this child contains multiple nodes as below:
<child>
<details>
<name/>
<education>
</details>
</child>

But what if this child contains multiple nodes as below:
<child>
<details>
<name/>
<education>
</details>
</child>

For multiple levels of tags I don't think you can use XML::CSV as it says in the description of the docs "XML::CSV ... currently ... only allows a simple XML structure." Maybe someone else can help you with creating a multiple level structure. It would help if you could show us a sample of your csv input data.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.