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>

In reply to Re: Re: PERL/XML by mirod
in thread PERL/XML by falco

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.