Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Parsing XML...by hand (ugh)

by Generoso (Vicar)
on Dec 06, 2010 at 03:37 UTC ( #875541=note: print w/ replies, xml ) Need Help??


in reply to Parsing XML...by hand (ugh)

This by no way the finish product but it may help you get started.

#!/usr/bin/perl -w # use strict; my ($xmlvar,$i,$k,$v); my %hash = (); while (<DATA>) { print; chomp($_); $i = 0; while ($i++ < length($_)) { print "1: '"; print $1 if /(<)/gc; print "', pos=", pos, "\n"; $k = ''; print "2: '"; $k = $1 if /\G([A-Za-z0-9]+)/gc; print "$k', pos=", pos, "\n" +; my $f = '</'.$k.'>' if defined $k; print "3: '"; print $1 if /(>)/gc; print "', pos=", pos, "\n"; $v = ''; print "4: '"; $v = $1 if /\G(.*)$f/gc; print "$v', pos=", pos, "\n"; $i = pos; print $k,' << ',$v,"\n"; $hash{$k} .= $v.','if defined $k; } print "Final: '$1', pos=",pos,"\n" if /\G(.)/; } s/,\z// for values %hash; while ( my ($key, $value) = each(%hash) ) { print "$key => $value\n"; } __DATA__ <dataset> <row> <name>dog</name> <category>home pet</category> </row> <row> <name>cat</name><category>home pet</category> </row> <row> <name>penguin</name> <category>fish</category> </row> <row> <name>lax</name> <category>wile</category> </row> <row> <name>whale</name> <category>fish</category> </row> <row> <name>ostrich</name> <category>bird</category> </row> <row> <name>catfish</name> <category>fish</category> </row> </dataset>


Comment on Re: Parsing XML...by hand (ugh)
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://875541]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2014-12-27 20:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls