Solved

Any one explain how to read data from xml file and store that data in to an excel sheet

Posted on 2008-06-25
7
218 Views
Last Modified: 2010-03-05
Hi experts,


How to read a xml file and read the data in xml in to an excel sheet??
0
Comment
Question by:rakeshreddy42
7 Comments
 
LVL 13

Expert Comment

by:kawas
ID: 21865051
You could read the file using XML::LibXML and store the contents in a CSV file (comma delimited)

http://search.cpan.org/dist/XML-LibXML/ explains the packages in XML::LibXML (pay attention to the Documentation links for Document and Element)

GO through and read the nodes in your XML file, and construct a CSV file. You can then read (import) the file in excel
0
 
LVL 39

Expert Comment

by:Adam314
ID: 21866504
You might be able to use the XML::Simple module.  This makes it very easy to read the XML file, but you don't get quite as much control over how it is read.  Whether it works will depend on your specific requirements.

Writing to a csv file would be easiest, and excel will be able to read it.  If you need more control over the output, you can use Spreadsheet::WriteExcel.
0
 

Author Comment

by:rakeshreddy42
ID: 21871883
Hi Adam,

could you give a smple script for reading an xml file into an csv file or an excel sheet.

say the sml file is as below.

<architectures>
      <architecture>
      <name>Pace5</name>
      <testcases>
<testcase>tc_arm_icache<testcase>
<cache>
      <icache_size>32K</icache_size>      
      <dcache_size>4K</dcache_size>
</cache>
<testcase>tc_arm_idcache<testcase>
<cache>
      <icache_size>32K</icache_size>      
      <dcache_size>32K</dcache_size>
</cache>
<testcase>tc_arm_idcache<testcase>
<cache>
      <icache_size>32K</icache_size>      
      <dcache_size>4K</dcache_size>
</cache>
            </testcases>            
      </architecture>
<architecture>
      <name>Faraday</name>
      <testcases>
<testcase>tc_arm_dcache<testcase>
<cache>
      <icache_size>16K</icache_size>      
      <dcache_size>4K</dcache_size>
</cache>
<testcase>tc_arm_icache<testcase>
<cache>
      <icache_size>4K</icache_size>      
      <dcache_size>16K</dcache_size>
</cache>
            </testcases>            
      </architecture>
</architectures>

Thanks & Regards,
Rakesh.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 39

Expert Comment

by:Adam314
ID: 21873946
How do you want that to become a CSV file.  Post an example of the desired output.
0
 

Author Comment

by:rakeshreddy42
ID: 21880966
hi adam,

the output should be as below.Along with the above xml file we have to read data from another xml file:
File2.xml
<testcase>
<number>tc_arm_icache</number>
      <use>Yes</use>
</testcase>
<testcase>
<number>tc_arm_dcache</number>
      <use>Yes</use>
</testcase>
<testcase>
<number>tc_arm_idcache</number>
      <use>Yes</use>
</testcase>
<testcase>
<number>tc_arm_idcache</number>
      <use>Yes</use>
</testcase>
<testcase>
<number>tc_arm_icache</number>
      <use>Yes</use>
</testcase>


Read above xml files and arrange the read information (with appropriate conversion, such as converting 32K to 32768) in an excel sheet named, TestInformaiton.xls, in following format.

Test Case No.      Architecture            Cache                                        Usable
                                    ICache Size DCache Size      
tc_arm9_icache      Pace5       32768        4096                      Yes
tc_arm9_dcache      Faraday      16384      4096                       No
tc_arm9_idcache      Pace5      32768      32768                     Yes
tc_arm9_idcache      Pace5      32768      4096                     Yes
tc_arm9_icache      Faraday      4096      16384                    Yes

Thanjks,
Rakesh.
0
 
LVL 39

Expert Comment

by:Adam314
ID: 21883555
How do you match data between two files?
0
 
LVL 8

Accepted Solution

by:
nedfine earned 500 total points
ID: 21884306
Try this.
This will give you a CSV file.

Thanks
Ned
open  IN, "File1.xml";  #input xml file

open OUT, ">ttt" or die "cannot open output file";

while(<IN>)

{

 if($_ =~/\<(.*)\>(.*)\<(\/*)\1\>/)

 {

  push (@names,$1);

  push(@$1,$2);

  print OUT "$1,$2\n";

   #print "\n$1";

 }

}

close OUT;

close IN;

 

open OUT, ">csvq.csv" or die "cannoo $!";

open IN,"ttt";

print OUT "Name,Architecture,Icachesize,Dcachesize\n";

while(<IN>)

{

chomp();

@a = split/\,/;

$a[1] = 32768 if($a[1] eq '32K');

$a[1] = 16384 if($a[1] eq '16K');

$a[1] = 4096 if($a[1] eq '4K');

if($a[0] eq 'name')

{

$name = $a[1];

$string = $name;

$i=0;

}

else

{

 $string = $string.",".$a[1];

 $i++;

 if($i == 3)

 {

  print OUT "$string\n";

  $i=0;

  $string = $name;

 }

}

 $hash{$a[0]} = 1 if(!$hash{$a[0]});  

}

 

 

foreach (keys % hash)

{

 print "\n headings -> $_" if ($hash{$_}); 

}

 

 

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

895 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

16 Experts available now in Live!

Get 1:1 Help Now