Perlbeginner1 has asked for the wisdom of the Perl Monks concerning the following question:

good day perl-monks today 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 your XML:

see the Code:

#!/usr/bin/perl use strict; use warnings; use XML::Simple; use Data::Dumper; my $xmlfile = shift || die "Usage: $0 <XML_FILE>\n"; my $ref; eval { $ref = XMLin($xmlfile, ForceArray => 0, KeyAttr => [ ], SuppressEmpty => '', ) or die "Can't read XML from $xmlfile: $!\n"; }; die $@ if($@); print Dumper $ref;



Explantion: iterating trough the array / hash - this does create a file and helps carving up the data into comma separated lines of data than can be redirected to file.
<?xml version="1.0" encoding="UTF-8"?> <osm version="0.6" generator="Overpass API"> <note>The data included in this document is from www.openstreetmap.org +. The data is made available under ODbL.</note> <meta osm_base=":49:02Z"/> <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="Sofienstraße"/> <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-Württemberg"/> <tag k="addr:street" v="Rohrbacher Straße"/> <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>
i runned this - and it looks nice.. Do you think that this is an appropiate way to do this?

Replies are listed 'Best First'.
Re: XML::Simple to parse a xml-file and transform it to csv
by choroba (Cardinal) on Apr 29, 2014 at 17:14 UTC
    Have your read the warning in the module documentation?
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      hello - many many thanks for the quick reply - nope i did not read any warings.....which one do you think off
        Just put your mouse pointer over the word "warning" in my previous post and click the left mouse button.
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: XML::Simple to parse a xml-file and transform it to csv
by Discipulus (Canon) on Apr 30, 2014 at 07:56 UTC
    Hello, as always i suggest XML::Twig

    I'm not an XML expert so i confess i found some probelm in your data example, german char included..
    I put the modified data in a file. You can see the content after the __END__ token in the script:
    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]->childre +n ){ 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'); __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> __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-WUrttem +berg, Rohrbacher StraSSe, Heidel-bike, Tu-Fr 10:00 -18:30; Sa 10:00-14:00, bicycle, http://www.heidelbike.de/, yes,
    ..and an handful of links from my homenode:
    XML "If you need to deal with XML, first, we’re very sorry." b d foy http://www.effectiveperlprogramming.com/2011/07/rewrite-xml-with-xmltwig/ and http://www.effectiveperlprogramming.com/2010/03/process-xml-data-with-xmltwig/ and http://it-is-etc.blogspot.it/2012/07/perl-how-to-manipulate-xml-files-using.html and http://perlmeme.org/tutorials/parsing_xml.html speed comparison http://www.robinclarke.net/archives/xml-parsing-with-perl mirod schratchpad and Re: Another simple XML Twig question and http://www.xml.com/pub/a/2001/03/21/xmltwig.html ambrus's Do not reinvent the wheel: real-world example using XML::Twig and also http://perl-xml.sourceforge.net/faq/ and choroba about XML

    HtH
    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      hello many many thanks for all!!