CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
text-mangling with xml::twig - some questions regarding a real world example

 



dilbert
User

Mar 26, 2015, 12:14 PM

Post #1 of 2 (5913 views)
text-mangling with xml::twig - some questions regarding a real world example Can't Post

good day perlgurus


well at the moment i have a question regarding textmangling and converting.
well i want to use perl for text-mangling - so we can use the XML::Simple module.
here's an example of a little script to parse some XML that is gained from a request to the openstreetmap-project - eg at the overpass-turbo.eu-webpage:




if we go with XML::Twig

since i am not an XML expert so i confess that i am a real beginner I put the data in a file that needs to be parsed.

You can see the content after the __END__ token in the script



Code
 
use strict;
use warnings;
use XML::Twig;

my $t= XML::Twig->new( pretty_print => 'indented',
twig_handlers => {

'node'=>sub{print $_[1]->att('id'),', ',$_[1]->att('lat'),', ',$_[1]->att('lon'),', ' ;

foreach my $tag ( $_[1]->children ){
print $tag->att('v').", ";
}
print "\n";
},

}
);

print "#ID, LAT, LON, CITY, HOUSNUMBER, POSTCODE, STREET, NAME, PHONE, SHOP, SOURCE, WEBSITE, WHEELCHAIR\n";
$t->parsefile('xml-001.xml');




this is the code that is stored in the file called like so: xml-001.xml



Quote
__END__
<?xml version="1.0" encoding="UTF-8"?>
<root>
<node id="297467" lat="49.5014" lon="8.1465">
<tag k="addr:city" v="Stuttgart"/>
<tag k="addr:housenumber" v="23"/>
<tag k="addr:postcode" v="69115"/>
<tag k="addr:street" v="Sofienstrae"/>
<tag k="name" v="ARLT"/>
<tag k="phone" v="+49 6221 20229"/>
<tag k="shop" v="computer"/>
<tag k="source" v="survey"/>
<tag k="website" v="http://www.arlt.com"/>
<tag k="wheelchair" v="yes"/>
</node>
<node id="305144906" lat="49.40012" lon="8.6929652">
<tag k="addr:city" v="Mainz"/>
<tag k="addr:country" v="DE"/>
<tag k="addr:housenumber" v="13-15"/>
<tag k="addr:postcode" v="69115"/>
<tag k="addr:state" v="Baden-WUrttemberg"/>
<tag k="addr:street" v="Rohrbacher StraSSe"/>
<tag k="name" v="Heidel-bike"/>
<tag k="opening_hours" v="Tu-Fr 10:00-18:30; Sa 10:00-14:00"/>
<tag k="shop" v="bicycle"/>
<tag k="website" v="http://www.heidelbike.de/"/>
<tag k="wheelchair" v="yes"/>
</node>
</root>



......this is the aimed outcome


Quote

__OUTPUT__
#ID, LAT, LON, CITY, HOUSNUMBER, POSTCODE, STREET, NAME, PHONE, SHOP, SOURCE, WEBSITE, WHEELCHAIR
297467, 49.5014, 8.1465, Stuttgart, 23, 69115, Sofienstrae, ARLT, +49 6221 20229, computer, survey, http://www.arlt.com,
yes,
305144906, 49.40012, 8.6929652, Mainz, DE, 13-15, 69115, Baden-WUrttemberg, Rohrbacher StraSSe, Heidel-bike, Tu-Fr 10:00
-18:30; Sa 10:00-14:00, bicycle, http://www.heidelbike.de/, yes,




well - believe it or not: well but if i run this code here



i get back this here



Quote

#ID, LAT, LON, CITY, HOUSNUMBER, POSTCODE, STREET, NAME, PHONE, SHOP, SOURCE, WEBSITE, WHEELCHAIR

XML or text declaration not at start of entity at line 2, column 0, byte 1 at /usr/lib/perl5/vendor_perl/5.18.1/i586-linux-thread-multi/XML/Parser.pm line 187.
at xmltwig.pl line 20.
at xmltwig.pl line 20.
martin@linux-70ce:~/perl/osm> perl xmltwig.pl
#ID, LAT, LON, CITY, HOUSNUMBER, POSTCODE, STREET, NAME, PHONE, SHOP, SOURCE, WEBSITE, WHEELCHAIR

XML or text declaration not at start of entity at line 2, column 0, byte 1 at /usr/lib/perl5/vendor_perl/5.18.1/i586-linux-thread-multi/XML/Parser.pm line 187.
at xmltwig.pl line 19.
at xmltwig.pl line 19.
martin@linux-70ce:~/perl/osm>



what can i do - what goes wrong here?


(This post was edited by dilbert on Mar 26, 2015, 12:16 PM)


dilbert
User

Mar 29, 2015, 6:09 AM

Post #2 of 2 (5776 views)
Re: [dilbert] text-mangling with xml::twig - some questions regarding a real world example [In reply to] Can't Post

hello dear gurus
any ideas

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives