469,266 Members | 1,679 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,266 developers. It's quick & easy.

How do I read xml file in perl?

Hello All,

I am very new to perl.I am a java person.
I have to read xml file in perl script and use those values in that file.
So can anybody please help me to solve this xml parsing in perl file.

Thankyou in advance.
Aug 28 '07 #1
5 17783
KevinADC
4,059 Expert 2GB
search cpan for xml modules:

cpan: xml modules

XML::Twig and XML::Simple are often sited as being good xml handling modules.
Aug 28 '07 #2
I have searched in google....
I got the following code to read xml file...

Expand|Select|Wrap|Line Numbers
  1. use XML::DOM;
  2. use XML::DOM;
  3.  
  4. my $file = 'letters.xml';
  5. my $parser = XML::DOM::Parser->new();
  6.  
  7. my $doc = $parser->parsefile($file);
  8.  
  9. foreach my $species ($doc->getElementsByTagName('Letter')){
  10.     print $species->getElementsByTagName('Letter')->item(0)->getFirstChild->getNodeValue;
  11. }
  12.  
when I run this file then it is giving some errors.

Can't locate XML/DOM.pm in @INC (@INC contains: /usr/local/lib/perl5/5.6.1/aix /usr/local/lib/perl5/5.6.1 /usr/local/lib/perl5/site_perl/5.6.1/aix /usr/local/lib/perl5/site_perl/5.6.1 /usr/local/lib/perl5/site_perl .) at xml-dom_write.pl line 4.
BEGIN failed--compilation aborted at xml-dom_write.pl line 4.
is it means i need to put the drivers in classpath.But Where do i need to put and what do i need to put.

please help me out
Aug 28 '07 #3
KevinADC
4,059 Expert 2GB
You need to install the module.

How do I install perl modules?


Note: perl 5.6 is very old, you should upgrade to 5.8 if at all possible.
Aug 28 '07 #4
Dear Kevin,

Thanks for your reply.
I am using Unix machine.I am trying to install required modules by following command.

perl -MCPAN -e shell 'install XML::DOM'

thenit is asking so many questions.How do i know hich one need to give.Is there any specific procedure need to follow.

It is giving the following when i run the command.

fmc /opt/fmc> perl -MCPAN -e shell 'install XML::DOM'


/opt/fmc/.cpan/CPAN/MyConfig.pm initialized.


CPAN is the world-wide archive of perl resources. It consists of about
100 sites that all replicate the same contents all around the globe.
Many countries have at least one CPAN site already. The resources
found on CPAN are easily accessible with the CPAN.pm module. If you
want to use CPAN.pm, you have to configure it properly.

If you do not want to enter a dialog now, you can answer 'no' to this
question and I'll try to autoconfigure. (Note: you can revisit this
dialog anytime later by typing 'o conf init' at the cpan prompt.)

Are you ready for manual configuration? [yes] yes


The following questions are intended to help you with the
configuration. The CPAN module needs a directory of its own to cache
important index files and maybe keep a temporary mirror of CPAN files.
This may be a site-wide directory or a personal directory.



I see you already have a directory
/opt/fmc/.cpan
Shall we use it as the general CPAN build and cache directory?

CPAN build and cache directory? [/opt/fmc/.cpan] /opt/fmc/.cpan


If you want, I can keep the source files after a build in the cpan
home directory. If you choose so then future builds will take the
files from there. If you don't want to keep them, answer 0 to the
next question.



How big should the disk cache be for keeping the build directories
with all the intermediate files?

Cache size for build directory (in MB)? [10] 10


By default, each time the CPAN module is started, cache scanning
is performed to keep the cache size in sync. To prevent from this,
disable the cache scanning with 'never'.

Perform cache scanning (atstart or never)? [atstart] atstart


To considerably speed up the initial CPAN shell startup, it is
possible to use Storable to create a cache of metadata. If Storable
is not available, the normal index mechanism will be used.

Cache metadata (yes/no)? [yes] yes


The next option deals with the charset your terminal supports. In
general CPAN is English speaking territory, thus the charset does not
matter much, but some of the aliens out there who upload their
software to CPAN bear names that are outside the ASCII range. If your
terminal supports UTF-8, you say no to the next question, if it
supports ISO-8859-1 (also known as LATIN1) then you say yes, and if it
supports neither nor, your answer does not matter, you will not be
able to read the names of some authors anyway. If you answer no, names
will be output in UTF-8.

Your terminal expects ISO-8859-1 (yes/no)? [yes] yes


The CPAN module can detect when a module that which you are trying to
build depends on prerequisites. If this happens, it can build the
prerequisites for you automatically ('follow'), ask you for
confirmation ('ask'), or just ignore them ('ignore'). Please set your
policy to one of the three values.

Policy on building prerequisites (follow, ask or ignore)? [ask] ask


The CPAN module will need a few external programs to work properly.
Please correct me, if I guess the wrong path for a program. Don't
panic if you do not have some of them, just press ENTER for those. To
disable the use of a download program, you can type a space followed
by ENTER.

Where is your gzip program? [/usr/bin/gzip] /usr/bin/gzip
Where is your tar program? [/usr/bin/tar] /usr/bin/tar
Warning: unzip not found in PATH
Where is your unzip program? []
Where is your make program? [/usr/bin/make] /usr/bin/make
Warning: lynx not found in PATH
Where is your lynx program? []
Warning: wget not found in PATH
Where is your wget program? []
Warning: ncftpget not found in PATH
Where is your ncftpget program? []
Warning: ncftp not found in PATH
Where is your ncftp program? []
Where is your ftp program? [/usr/bin/ftp] /usr/bin/ftp
What is your favorite pager program? [/usr/bin/more] /usr/bin/more
What is your favorite shell? [/usr/bin/ksh]


Every Makefile.PL is run by perl in a separate process. Likewise we
run 'make' and 'make install' in processes. If you have any
parameters (e.g. PREFIX, LIB, UNINST or the like) you want to pass
to the calls, please specify them here.

If you don't understand this question, just press ENTER.

Parameters for the 'perl Makefile.PL' command?
Typical frequently used settings:

POLLUTE=1 increasing backwards compatibility
LIB=~/perl non-root users (please see manual for more hints)

Your choice: []
Parameters for the 'make' command?
Typical frequently used setting:

-j3 dual processor system

Your choice: []
Parameters for the 'make install' command?
Typical frequently used setting:

UNINST=1 to always uninstall potentially conflicting files

Your choice: []


Sometimes you may wish to leave the processes run by CPAN alone
without caring about them. As sometimes the Makefile.PL contains
question you're expected to answer, you can set a timer that will
kill a 'perl Makefile.PL' process after the specified time in seconds.

If you set this value to 0, these processes will wait forever. This is
the default and recommended setting.

Timeout for inactivity during Makefile.PL? [0]


If you're accessing the net via proxies, you can specify them in the
CPAN configuration or via environment variables. The variable in
the $CPAN::Config takes precedence.

Your ftp_proxy?
Your http_proxy?
Your no_proxy?
You have no /opt/fmc/.cpan/sources/MIRRORED.BY
I'm trying to fetch one
LWP not available

Please, install Net::FTP as soon as possible. CPAN.pm installs it for you
if you just type
install Bundle::libnet

Issuing "/usr/bin/ftp -n"

ftp: connect to address ::ffff:153.106.5.70: A remote host did not respond within the timeout period.
Trying ::ffff:199.239.233.95...
ftp: connect to address ::ffff:199.239.233.95: A remote host did not respond within the timeout period.
Trying ::ffff:153.106.5.70...
ftp: connect: A remote host did not respond within the timeout period.
Not connected.
Local directory now /opt/fmc/.cpan/sources
Not connected.
Not connected.
Not connected.
Not connected.
Not connected.
Bad luck... Still failed!
Can't access URL ftp://ftp.perl.org/pub/CPAN/MIRRORED.BY.

Please check, if the URLs I found in your configuration file () are valid.
The urllist can be edited. E.g. with 'o conf urllist push ftp://myurl/'

Could not fetch MIRRORED.BY
CPAN.pm needs at least one URL where it can fetch CPAN files from.

Please enter your CPAN site: [] CPAN.pm needs at least one URL where it can fetch CPAN files from.

Please enter your CPAN site: []
Aug 28 '07 #5
numberwhun
3,503 Expert Mod 2GB
This is your typical CPAN configuration. You only really need to go through this once, unless you decide to change something later. Here are my three suggestions.

1. For this line: "Policy on building prerequisites (follow, ask or ignore)? [ask] "

I always choose "follow". That way, CPAN automatically downloads, builds and installs any prerequisites to the module(s) that I am installing.

2. Make sure you install all of the programs it is looking for. In other words, take this list:

Where is your gzip program? [/usr/bin/gzip] /usr/bin/gzip
Where is your tar program? [/usr/bin/tar] /usr/bin/tar
Warning: unzip not found in PATH
Where is your unzip program? []
Where is your make program? [/usr/bin/make] /usr/bin/make
Warning: lynx not found in PATH
Where is your lynx program? []
Warning: wget not found in PATH
Where is your wget program? []
Warning: ncftpget not found in PATH
Where is your ncftpget program? []
Warning: ncftp not found in PATH
Where is your ncftp program? []
Where is your ftp program? [/usr/bin/ftp] /usr/bin/ftp
What is your favorite pager program? [/usr/bin/more] /usr/bin/more
What is your favorite shell? [/usr/bin/ksh]


and make sure that any program not listed is installed before re-running it.

3. After these questions, you will be prompted to select the sites you want to include in your search list (URL's). you would list them by using the number of the sites, separated by a space. I include 2 through 56 ( I think that's the highest one), only I do them in reverse order: ie: 56 55 54 53 52 51 50 ........
until they are all listed. Sure, this is overkill, but if I am looking for a module listed on cpan and its only on one of the mirrors (for example), then I will find it eventually. The reason I list them in reverse order is because the cpan urls are at the end (in the 50's) and seem to respond all the time.

I hope this helped!

Regards,

Jeff
Aug 28 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Berk Birand | last post: by
3 posts views Thread by NNTP | last post: by
5 posts views Thread by Radium | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.