Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 153
  • Last Modified:

Parsing XML file

Hello All,

I need a program to parse the xml file. The program should create the insert statements to the database.

Example the below code should create the MySQL insert statements to the book table where the attributes are author, title publisher, address etc etc and same for the article table.

The sample file looks something like this
-----------------------------------------------
<?xml version="1.0" encoding="utf-8" ?>

<bibtex:file xmlns:bibtex="http://bibtexml.sf.net/">
    <bibtex:entry id="aatre1998-862">
        <bibtex:book>
            <bibtex:author>Aatre, Vasudev K. and Varadan, V. K. and Varadan, V. V. and Engineers., Society of Photo-optical Instrumentation</bibtex:author>
            <bibtex:title>Smart materials, structures, and MEMS : 11-14 December 1996, Bangalore, India</bibtex:title>
            <bibtex:publisher>Spie</bibtex:publisher>
            <bibtex:address>Bellingham, Wash., USA</bibtex:address>
            <bibtex:note>98213336 Vasudev K. Aatre, Vijay K. Varadan, Vasundara V. Varadan, chairs/editors ; sponsored by SPIE--the International Society for Optical Engineering ... [et al.]. SPIE proceedings series ; v. 3321. Includes bibliographical references and index. Proceedings of SPIE--the International Society for Optical Engineering ; v. 3321.</bibtex:note>
            <bibtex:keywords>Smart materials Congresses. Smart structures Congresses. Microelectromechanical systems Materials Congresses. Microactuators Materials Congresses.</bibtex:keywords>
            <bibtex:year>1998</bibtex:year>
        </bibtex:book>

<bibtex:article>
            <bibtex:author>L. N. Zhang</bibtex:author>
            <bibtex:title>An explicit Moyal product realization of quantum
 deformation</bibtex:title>
            <bibtex:journal>Comm. Theoret. Phys.</bibtex:journal>
            <bibtex:volume>26</bibtex:volume>
            <bibtex:year>1996</bibtex:year>
            <bibtex:pages>207--212</bibtex:pages>
        </bibtex:article>
    </bibtex:entry>
</bibtex:file>



0
cutie_smily
Asked:
cutie_smily
  • 5
  • 4
  • 3
2 Solutions
 
ps15Commented:
use XML::Simple
use Data::Dumper

my $data = XMLin($file);
print Dumper $data;

# This will show you the internal perl structure, depending on how exactly you want to insert it into a Database, you can go on from here ;p
0
 
ozoCommented:
use XML::Simple qw(:strict);
my $ref = XMLin(join('',<DATA>),ForceArray=>[qw(bibtex:book bibtex:article)],KeyAttr=>[]);
for$table( qw(book article) ){
  for( map{@$_}@{$ref->{'bibtex:entry'}}{"bibtex:$table"} ){
    $dbh->do("INSERT INTO $table (author,title,publisher,address) VALUES (?,?,?,?)",undef,@{$_}{qw(bibtex:author bibtex:title bibtex:publisher bibtex:address)});
  }
}
__DATA__
<?xml version="1.0" encoding="utf-8" ?>

<bibtex:file xmlns:bibtex="http://bibtexml.sf.net/">
    <bibtex:entry id="aatre1998-862">
        <bibtex:book>
            <bibtex:author>Aatre, Vasudev K. and Varadan, V. K. and Varadan, V. V. and Engineers., Society of Photo-optical Instrumentation</bibtex:author>
            <bibtex:title>Smart materials, structures, and MEMS : 11-14 December 1996, Bangalore, India</bibtex:title>
            <bibtex:publisher>Spie</bibtex:publisher>
            <bibtex:address>Bellingham, Wash., USA</bibtex:address>
    <bibtex:note>98213336 Vasudev K. Aatre, Vijay K. Varadan, Vasundara V. Varadan, chairs/editors ; sponsored by SPIE--the International Society for Optical Engineering ... [et al.]. SPIE proceedings series ; v. 3321. Includes bibliographical references and index. Proceedings of SPIE--the International Society for Optical Engineering ; v. 3321.</bibtex:note>
            <bibtex:keywords>Smart materials Congresses. Smart structures Congresses. Microelectromechanical systems Materials Congresses. Microactuators Materials Congresses.</bibtex:keywords>
            <bibtex:year>1998</bibtex:year>
        </bibtex:book>

<bibtex:article>
            <bibtex:author>L. N. Zhang</bibtex:author>
            <bibtex:title>An explicit Moyal product realization of quantum
 deformation</bibtex:title>
            <bibtex:journal>Comm. Theoret. Phys.</bibtex:journal>
            <bibtex:volume>26</bibtex:volume>
            <bibtex:year>1996</bibtex:year>
            <bibtex:pages>207--212</bibtex:pages>
        </bibtex:article>
    </bibtex:entry>
</bibtex:file>
0
 
cutie_smilyAuthor Commented:
Hi,

Thanks very much for the code. I am a real beginner in the perl. Below is the error I got

Can't locate XML/Simple.pm in @INC (@INC contains: /usr/lib/perl5/5.8.0/i386-linux-thread-multi /usr/lib/perl5/5.8.0 /usr/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.0 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.0/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.0 /usr/lib/perl5/vendor_perl /usr/lib/perl5/5.8.0/i386-linux-thread-multi /usr/lib/perl5/5.8.0 .) at xml_parser_new.pl line 3.

Hope I will be helped
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
ps15Commented:
you haven't got the required module XML::Simple, try looking for a package that hast a name something like that in your distribution,
or run
perl -MCPAN -e 'install XML::Simple'
(as root if you want it installed globally)
0
 
cutie_smilyAuthor Commented:
I think I don't have permissions....is there a different way?

$ cd /
$ perl -MCPAN -e 'install XML::Simple'
CPAN: Storable loaded ok
mkdir /root/.cpan: Permission denied at /usr/lib/perl5/5.8.0/CPAN.pm line 2264

thanks
0
 
ps15Commented:
Do you have root access to the System your working on ?
if you do, execute the command as root, if you don't, tell your administrator to do it ;)
0
 
cutie_smilyAuthor Commented:
Thanks. I don't have the root permissions. I am just curious what does XML::Simple do? If it is used to connect to the database then probably we can try to create a file with the insert statements where I can load that file into the table.

Thanks
0
 
ps15Commented:
XML::Simple is a Simple XML Parser, a Perl Module to Read XML Simply into perl
0
 
ozoCommented:
perldoc CPAN
                 5)  I am not root,  how  can  I  install  a  module  in  a
                     personal directory?
                     You will most probably like something like this:
                       o conf makepl_arg "LIB=~/myperl/lib \
                                         INSTALLMAN1DIR=~/myperl/man/man1 \
                                         INSTALLMAN3DIR=~/myperl/man/man3"
                       install Sybase::Sybperl

                     You  can  make  this setting permanent like all o conf
                     settings with o conf commit.
                     You will have  to  add  ~/myperl/man  to  the  MANPATH
                     environment  variable and also tell your perl programs
                     to look into ~/myperl/lib, e.g. by including
                       use lib "$ENV{HOME}/myperl/lib";

                     or setting the PERL5LIB environment variable.
                     Another thing you should bear  in  mind  is  that  the
                     UNINST  parameter  should  never be set if you are not
                     root.

0
 
cutie_smilyAuthor Commented:
Thanks for the info.

I aplogize to ask this:
-----------------------
I am not restricted to write the parsing program in a specific language. So, is there any other way we can do this task?

thanks
0
 
ozoCommented:
# if you know the file will always be in exactly that format,
# you may not need a full XML parser, and you may be able to do something like
$/="</bibtex:entry>";
while( <> ){
    while( /<bibtex:(book|article)>(.*?)<\/bibtex:\1>/sg ){
        my($table,$attributes)=($1,$2);
        my %attributes=();
        while( $attributes =~ /<bibtex:(author|title|publisher|address)>(.*?)<\/bibtex:\1/sg ){
           $attributes{$1}=$2;
        }
        $dbh->do("INSERT INTO $table (".
                 join(",",keys %attributes).
                 ") VALUES (".
                 join(",",("?")x keys %attributes).
                 ")",undef,values %attributes;
    }
}
0
 
cutie_smilyAuthor Commented:
Thanks very much for the code.

I am getting a syntax error

syntax error at perl_loop.pl line 14, near "%attributes ;"
Execution of perl_loop.pl aborted due to compilation errors.

Please let me know how do I supply my input file.

thanks again
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now