Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PERL XML::XPath 2

Status
Not open for further replies.

defdefdef

Technical User
Jul 4, 2005
19
0
0
FR
Hi,
I'm using a script to extract data from a node of an XML document with (PERL) XML::XPath with a loop :
......
foreach my $row ($xp->findnodes('/Ensemble/item/Attributes')) {
# extract from the XML
my $code = $row->find('code')->string_value;
my $title = $row->find('Title')->string_value;
my $url = $row->find('URL')->string_value;

# insert into the db (using placeholders)
$sth->execute($code, $title, $url) || die $DBI::errstr;}
......

But how could I extract data from SEVERAL nodes in the same loop. (Not only the node '/Ensemble/item/Attributes').

For example:
How could I extract also a data $subtitle in the node
'Ensemble/item/Subinfo' and finally execute the insert in database :
$sth->execute($code, $title, $url, $subtitle) || die $DBI::errstr;}

Thanks if you have the courage to read my post until this line ;-)

 
Code:
my @nodes=('/Ensemble/item/Attributes','/Ensemble/item/Subinfo');
foreach(@nodes){
  foreach my $row ($xp->findnodes($_)) {
  ...
  $sth->execute($code, $title, $url) || die $DBI::errstr;}
  }
}

cigless ...
 
compound location paths:-

use pipes...

/Ensemble/item/Attributes | /Ensemble/item/Subinfo


Kind Regards
Duncan
 
Thank you very much for your help.


When I use pipes :

assuming ('code' and 'title' are in the first node and 'url' in the second one)

......
my $sth = $dbh->prepare(qq{INSERT INTO books VALUES (?, ?, ?)});
foreach my $row ($xp->findnodes('/Ensemble/item/Attributes | Ensemble/item/SubInfo ')) {
# extract from the XML
my $code = $row->find('code')->string_value;
my $title = $row->find('Title')->string_value;
my $url = $row->find('URL')->string_value;

# insert into the db (using placeholders)
$sth->execute($code, $title, $url) || die $DBI::errstr;}
......

I only extract url and the others are blank in the database.??? Any Idea why ????
PS : I have the same problem with your code PAUL, I can't extract all the data only those from the last node, the other database fields are blank.

Best Regards
Defer

 
My XPath is a bit rusty, and I may not have understood your problem correctly, but could you do this?
Code:
foreach my $row ($xp->findnodes('/Ensemble/item/Attributes')) {
    # extract from the XML
    my $code = $row->find('code')->string_value;
    my $title = $row->find('Title')->string_value;
    my $url = $row->find('URL')->string_value;
    my $subtitle = $row->find('../Subinfo')->string_value;
    
# insert into the db (using placeholders)
    $sth->execute($code, $title, $url,$subtitle) || die $DBI::errstr;}
Or maybe this...
Code:
foreach my $row ($xp->findnodes('/Ensemble/item')) {
    # extract from the XML
    my $code = $row->find('Attributes/code')->string_value;
    my $title = $row->find('Attributes/Title')->string_value;
    my $url = $row->find('Attributes/URL')->string_value;
    my $subtitle = $row->find('Subinfo')->string_value;
    
# insert into the db (using placeholders)
    $sth->execute($code, $title, $url,$subtitle) || die $DBI::errstr;}


-- Chris Hunt
Webmaster & Tragedian
Extra Connections Ltd
 
Thanks a lot for your help Chris , the second solution works fine.

Best Regards
Defer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top