Hello - Below you'll find a Perl script and XML code. The .pl file reads from the .xml file, then turns the XML info into an HMTL document. The script works, but it works too well. I'm trying to extract the information from between the
<! [ CDATA ] > tags. But instead it retrieves everything (the <![CDATA ]> and the info within it). How can I have the script pull just the info from within the brackets?

XML:

<br> - <Feed> - <artikle> <ItemNum /> - <tittel> <![CDATA[ Test tittel ]]> </tittel> - <ingress> <![CDATA[ test ingres ]]> </ingress> - <url> <![CDATA[ http://www.test.com/test ]]> </url> </artikle> </Feed> </TestData>

PERL:

#!/usr/bin/perl # This script retrives an xml formatted news feed document and # generates a HTML file based on a tamplate file # # Format of the input should be: # <article>Some article # <title>Some Title</title> # <ingress>Some text</ingress> # <url>Some url</url> # </article> # # Modify vars in the config part to match your environment # # asbjorn@linux-directory.com, Oct. 2000 # ###### CONFIG START # Input format $article_del = "artikle"; $title_del = "tittle"; $url_del = "url"; $ing_del = "ingress"; # Input format end $template = "../../xml/test/test.txt"; ### Template HTML file. $html = "../../xml/test/test.html"; ### Name & path of HTML file to b +e generated $weburl = "http://www.test.com/xml/test.xml"; ### URL of document to b +e retrived $maxarticles = 1; ### Max no. of articles to be buildt. Must match tem +plate file $cgi = 1; ### Set if the script is to be run from a browser ###### CONFIG END #### MODULES USED use HTTP::Request; use LWP::UserAgent; use CGI; #### MODULES END $q=new CGI; print $q->header() if ( $cgi ); print $q->h3("$ver") if ( $cgi ); print "Henter $weburl"; print "<br>" if ( $cgi ); $ua = LWP::UserAgent->new; $request = HTTP::Request->new(GET => "$weburl"); $response = $ua->request($request); %hash = %{$response}; $content = $hash{_content}; $msg = $hash{_msg}; if ( $msg ne "OK" ) { print "Could not contact web server!\n"; exit 2; } print "Fetched $weburl\n"; print "<br>" if ( $cgi ); open(IN,"$template") || die "Failed to open $template: $!\n"; open(OUT,">$template.tmp") || die "Failed to write to $template.txt: $ +!\n"; while(<IN>) { print OUT; } close(OUT); close(IN); print "Generating HTML file:\n"; print "<br>" if ( $cgi ); @art = split m#(<$article_del>|</$article_del>)#, $content; shift @art; pop @art; foreach $art ( @art ) { ($title) = $art =~ m#<$title_del>(.*)</$title_del>#; ($ingress) = $art =~ m#<$ing_del>(.*)</$ing_del>#; ($url) = $art =~ m#<$url_del>(.*)</$url_del>#; ($url) = $url =~ m#<a href="(.*)">#; $i++ if $title; last if ( $i > $maxarticles ); &writefile($i,"title",$title) if ( $title ); &writefile($i,"ingress",$ingress) if ( $ingress ); &writefile($i,"url",$url) if ( $url ); } rename("$template.tmp","$html") || die "Failed to write $html: $!\n"; print " ----: DONE :----"; sub writefile { my $num = shift; my $type = shift; my $text = shift; $st = "URL_$num" if ( $type eq "url" ); $st = "TITTEL_$num" if ( $type eq "title" ); $st = "INGRESS_$num" if ( $type eq "ingress" ); open(IN,"$template.tmp") || die "Failed to open $template: $!\n"; @in = <IN>; close (IN); open(OUT,">$template.tmp") || die "Could not write to $html: $!\n"; foreach ( @in ) { s/$st/$text/g; print OUT; } close(OUT); }

Edit: 2001-03-03 by neshura


In reply to 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.