mosiondz has asked for the wisdom of the Perl Monks concerning the following question:

Hello Perl Monks!

I am attempting to parse XML data from an HTTP request and place it into a local database. I've gotten to the point where I've got the XML data saved to a local temporary file and want to take the information from the XML file and place it into the database. However, the ASP script I'm pulling the data from queries a SEQUEL database and returns MS "RowsetSchema" XML Schema; I'm trying to parse the info from it on a Linux system and put it into a MySQL database.

My main question: is there a way to parse the XML schema on my Linux system with a Perl script? I looked at various CPAN parsers but can't seem to find one that interprets this schema correctly. Do I need a proprietary solution?

Thanks in advance!

Replies are listed 'Best First'.
Re: Parsing Data from XML Schemas
by meetraz (Hermit) on Jun 16, 2004 at 00:20 UTC
    Do you really mean an XML schema (.xsd) document, or just plain XML data? If it's coming from a SQL query, it's probably just XML data and XML::Simple (for small XML files) or XML::Parser (for large ones) would probably work ok.

      As far as I understand it, the script returns an XML Schema (I might be off on the terminology here). It starts off with the following:

      - <xml xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:dt="u +uid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:rs="urn:schemas-micro +soft-com:rowset" xmlns:z="#RowsetSchema"> - <s:Schema id="RowsetSchema">
      Would I be correct in calling this a schema?
        In this case, it's an XML file with the database schema and rowset data embedded within it. If you look at the XML file, it should have <s:Schema> and <rs:data> sections.

        Try something like this.. I've tested it and it works. Note that XML::Simple reads the entire XML file into memory and therefore is not ideal for large datasets.

        use strict; use XML::Simple; my $xs = XML::Simple->new(); my $sqlref = $xs->XMLin('sqldata.xml'); my $rowsref = $sqlref->{'rs:data'}{'z:row'}; foreach my $row (@$rowsref) { print "---------New Row--------\n"; print $row->{'column1'}, "\n"; print $row->{'column2'}, "\n"; print $row->{'column3'}, "\n"; }
Re: Parsing Data from XML Schemas
by Steve_p (Priest) on Jun 16, 2004 at 03:08 UTC
Re: Parsing Data from XML Schemas
by vladdrak (Monk) on Jun 16, 2004 at 06:50 UTC
    XML::Parser is often used to read in an XML blob so you can do something interesting with it. Its a good idea to validate the XML, etc, but if you just want the blob un-XML'd try something like this:
    use XML::Parser; my $filename="justdownloaded.xml"; die "The filename specified in this script ($filename)doesnt exist..?" + if (! -e $filename); my $parser = new XML::Parser(ErrorContext => 2); $parser->setHandlers(Start => \&XMLStartHandler, Char => \&XMLCharHandler); $parser->parsefile($file); sub XMLStartHandler { my ($expat, $element, $attr, $value) = @_; print "Attribute: $attr\n"; print "Value: $value\n"; } sub XMLCharHandler { my ($p, $data) = @_; print "Data: $data\n"; }
    ..or something along those lines.. -Vlad