in reply to Re: PERL/XML
in thread PERL/XML
I hope you are still having formatting problems: your XML is still not valid, <![CDATA and]]> must be written as-is, without spaces.
Anyway, using XML::Simple here is how you get the information:
#!/usr/bin/perl use strict; use XML::Simple; my $xml= XMLin( \*DATA); my $artikle= $xml->{artikle}; $artikle=~ s/^\s*//s; $artikle=~ s/\s*$//s; print "article: $artikle\n"; __DATA__ <doc> <!-- you still need to wrap your XML in a +single root element --> <artikle> <![CDATA[Test tittle ]]> </artikle> <ingres> <![CDATA[Test ingres ]]> </ingres> <url> <![CDATA[Test url ]]> </url> </doc>
You should actually avoid unnecessary line returns within the XML documents, they _are_ significant:
<doc> <artikle><![CDATA[Test tittle ]]></artikle> <ingres><![CDATA[Test ingres ]]></ingres> <url><![CDATA[Test url ]]></url> </doc>
Then you can get rid of the 2 lines that remove leading and trailing spaces.
Note that I still can't figure out whether you real XML document includes only one article or several, in which case the format of the data as loaded by XML::Simple will change. As should your XML data actually (each article should be wrapped in a tag).
Usually I use the following script to figure out what's going on and how XML::Simple digests my document:
#!/usr/bin/perl use strict; use XML::Simple; use Data::Dumper; my $xml= XMLin( \*DATA); print Dumper( $xml); __DATA__ <my> document here, oh </my>
So finally I think what you are looking for night be something like:
#!/usr/bin/perl use strict; use XML::Simple; use Data::Dumper; my @fields= qw( title ingres url); my $xml= XMLin( \*DATA); foreach my $article (@{$xml->{artikle}}) # $xml is an ar +ray of hashes, each hash is an article { foreach my $field (@fields) # the key/value + pairs are element => content { print "$field: ", $article->{$field}, "\n"; } print "\n"; } __DATA__ <doc> <artikle> <title><![CDATA[Test tittle ]]></title> <ingres><![CDATA[Test ingres ]]></ingres> <url><![CDATA[Test url ]]></url> </artikle> <artikle> <title><![CDATA[Test tittle 2]]></title> <ingres><![CDATA[Test ingres 2]]></ingres> <url><![CDATA[Test url 2]]></url> </artikle> </doc>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: PERL/XML
by falco (Initiate) on Feb 20, 2001 at 10:02 UTC |