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:
Parsing DOM of XML file -problem with CDATA-

 



ticketE
Novice

Apr 21, 2013, 7:32 AM

Post #1 of 1 (298 views)
Parsing DOM of XML file -problem with CDATA- Can't Post

Hi,
I have to save the different words contained in a file that is a rss file.
Particularly I need to save words contained in the node category,
but in category there is a CData Section.
Example:

Code
<category><![CDATA[Hervé Falciani]]></category>

I need to save Hervé and Falciani

How could I access to it?
This is my perl code:

Code
#!/usr/bin/perl -w 

use strict;
use warnings;
use utf8;
use XML::Parser;
use LWP::Simple;
use XML::LibXML;
use XML::LibXML::XPathContext;

my $parser = new XML::LibXML;
my $struct = $parser -> parse_file("file.xml");
my $rootel = $struct -> getDocumentElement(); #rss
my $elname;

my @channels = $rootel -> childNodes();
my $channel;
my @channel_childs;
my @atts;
my @item_childs;
my $cdata;

foreach $channel(@channels) {
@channel_childs = $channel -> childNodes();
foreach my $el_channel_childs (@channel_childs){
$elname = $el_channel_childs -> getName();
if ($elname eq "item"){
@item_childs = $el_channel_childs -> childNodes();
foreach my $el_item_childs (@item_childs){
$elname = $el_item_childs -> getName();
if ($elname eq "category"){
$cdata = XML::LibXML::CDATASection->new($el_item_childs);
my $va = $cdata -> getValue();

print $va;
}
}
}
}
}

I tried in this way but it prints:

Quote
<category><![CDATA[Hervé Falciani]]></category>

but I only want the content of CData Section and I can't use regular expression.


(This post was edited by ticketE on Apr 21, 2013, 7:33 AM)

 
 


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

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