help in parsing xml messages

J

Julie

Hi,

I have an xml string like so.....


<q path="bugs/bug/bugs/@bug_id=3" match="endswith" true="true" op="and">
<q path="bugs/bug/reports/@id=57" match="binary" true="true" op="and">
<q path="bugs/bug/reports/@id=21" match="partial" true="true" op="or"/>
</q>
</q>



I want to parse it so that I can then
create a query via
reading the innermost loop and then the outer loop and then the outer loop,
and creating sql queries with each pass.


like.....

SELECT * from reports r,bugs b where r.id=21 and r.id= 57 and b.bug_id = 3;

how do I parse the xml to do this ?

any help will be appreciated.
thanks.
Julie
 
B

Brian McCauley

Julie said:
<q path="bugs/bug/bugs/@bug_id=3" match="endswith" true="true" op="and">
<q path="bugs/bug/reports/@id=57" match="binary" true="true" op="and">
<q path="bugs/bug/reports/@id=21" match="partial" true="true" op="or"/>
</q>
</q>

I want to parse it so that I can then
create a query via
reading the innermost loop and then the outer loop and then the outer loop,
and creating sql queries with each pass.


like.....

SELECT * from reports r,bugs b where r.id=21 and r.id= 57 and b.bug_id = 3;

how do I parse the xml to do this ?

If you want to parse XML I'd suggest using an XML parser module from the
XML::* hierachy on CPAN.

The most popular two (AFAIK) XML::parser and XML::LibXML are based on
external XML parser libraries (expat and the GNOME libxml2).

I've never programmed XML::parser but have used XML::LibXML. This is
because I'm doing stuff with XSLT using XML::LibXSTL. If I wasn't using
XSLT I'd probably have gone for XML::parser.
 
T

Tad McClellan

Julie said:
I have an xml string like so.....


[ snip data, repeated in code below ]

I want to parse it so that I can then
create a query via
reading the innermost loop and then the outer loop and then the outer loop,

how do I parse the xml to do this ?


By using XML::Simple, references and a recursive traversal of
the data structure that it returns:

-------------------------------
#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;
use XML::Simple;

my $xml_ref = XMLin <<'ENDXML';
<q path="bugs/bug/bugs/@bug_id=3" match="endswith" true="true" op="and">
<q path="bugs/bug/reports/@id=57" match="binary" true="true" op="and">
<q path="bugs/bug/reports/@id=21" match="partial" true="true" op="or"/>
</q>
</q>
ENDXML

# print Dumper $xml_ref; # for debugging

my @paths;
params( $xml_ref, \@paths );
print "$_\n" for @paths;

sub params {
my( $xml, $paths_ref ) = @_;

if ( exists $xml->{q} ) {
params( $xml->{q}, $paths_ref );
}

if ( exists $xml->{path} ) {
push @$paths_ref, $xml->{path};
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top