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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Formatting repeating block of strings

Status
Not open for further replies.

viadisky

Technical User
Jun 19, 2003
110
GB
Hi,

I have this data to work with:

<ConfigPolicyVO>
<ConfigPolicyID>61</ConfigPolicyID>
<ConfigPolicyName>access-list 99</ConfigPolicyName>
<Description>access-list 99</Description>
<Comments></Comments>
<Status>1</Status>
<CreateDate>2006-02-23 17:10:47:000</CreateDate>
<LastModifiedDate>2006-02-27 16:12:50:000</LastModifiedDate>
<LastModifiedUserID>1</LastModifiedUserID>
<InUse>1</InUse>
</ConfigPolicyVO>

<ConfigPolicyVO>
<ConfigPolicyID>35</ConfigPolicyID>
<ConfigPolicyName>Cisco Interfaces Up Down</ConfigPolicyName>
<Description>Cisco Interface Up Down</Description>
<Comments>Cisco Interface Up Down</Comments>
<Status>1</Status>
<CreateDate>2005-12-14 12:03:18:000</CreateDate>
<LastModifiedDate>2006-02-27 16:14:41:000</LastModifiedDate>
<LastModifiedUserID>1</LastModifiedUserID>
<InUse>1</InUse>
</ConfigPolicyVO>

How easy it is to format it this way?

ConfigPolicyName,Description,Status
access-list 99,access-list 99,1
Cisco Interfaces Up Down,Cisco Interface Up Down,1

Please help :)

Cheers!

 
Try XML::Simple. It should read your XML into a hash - note I've had to put an enclosing <root>...</root> around the whole thing to make it well-formed xml.
Code:
use strict;
use warnings;
use Data::Dumper;
use XML::Simple;

my @xml = <DATA>;
chomp @xml;
my $stuff = XMLin(join("", @xml)) or die ;
print Dumper($stuff);

__DATA__
<root>
<ConfigPolicyVO>
<ConfigPolicyID>61</ConfigPolicyID>
<ConfigPolicyName>access-list 99</ConfigPolicyName>
<Description>access-list 99</Description>
<Comments></Comments>
<Status>1</Status>
<CreateDate>2006-02-23 17:10:47:000</CreateDate>
<LastModifiedDate>2006-02-27 16:12:50:000</LastModifiedDate>
<LastModifiedUserID>1</LastModifiedUserID>
<InUse>1</InUse>
</ConfigPolicyVO>

<ConfigPolicyVO>
<ConfigPolicyID>35</ConfigPolicyID>
<ConfigPolicyName>Cisco Interfaces Up Down</ConfigPolicyName>
<Description>Cisco Interface Up Down</Description>
<Comments>Cisco Interface Up Down</Comments>
<Status>1</Status>
<CreateDate>2005-12-14 12:03:18:000</CreateDate>
<LastModifiedDate>2006-02-27 16:14:41:000</LastModifiedDate>
<LastModifiedUserID>1</LastModifiedUserID>
<InUse>1</InUse>
</ConfigPolicyVO>
</root>
XMLin() also accepts a file name. Run the above to show you the contents of the hash. Then all you need to do is print out the bits you want...
 
Hi Stevexff,

Thanks for your suggestion. I'm still learning Perl as of the moment so my question is really basic, sorry.

These lines:
use Data::Dumper;
use XML::Simple;

Do I need to download these?

Thanks again,
Maria
 
If you are running on Windows, no, as the current version of ActivePerl ships with them as standard. Open a command window and
Code:
ppm query *
which will tell you what packages you have installed. If you are on *nix, you can get them easily from CPAN.

XML::Simple handles parsing simple XML like your example really easily. Data::Dumper is great when you have a complex data structure and you want to know what's inside it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top