[VPM] converting objects to/from xml

Doug Snead semaphore_2000 at yahoo.com
Mon Jul 14 16:52:23 CDT 2003


I like to use XML::Simple where I can. 

Here's a little utility I use to "dump" an xml file, as perl
data. 

=========[ xmldump.pl ]========
#!/usr/bin/perl
# Usage:
#   perl xmldump.pl file:data.xml
#   perl xmldump.pl http//somesite.com/data.xml
# etc.
use XML::Simple;
use LWP::Simple;
use Data::Dumper;

my $data = XMLin( &get_page_content( $ARGV[0] ) );
print Dumper($data);

sub get_page_content {
        my ($url) = @_;
        my ($content) = get($url);
        defined $content
           or print STDERR "Can't fetch content from $url\n";
        return $content;
}

1;
==========================

- Doug

--- Darren Duncan <darren at DarrenDuncan.net> wrote:
> I have another question, mainly for those of you who have
> worked with XML in Perl.  
> 
> Given that my SQL::ObjectModel objects are conceptually
> organized like XML, I would like to make a module which will
> convert them to XML, and conversely parse an XML document
> into one of those objects.  
> 
> I would like to use some suitable modules from CPAN which
> handle most of the work, but I am unsure of which ones to
> use.  I had tried looking at some SAX modules (which seem
> common), because a callback interface seems to make sense for
> parsing XML, but I got the impression that this was perhaps
> too complicated.  Most of the SAX examples I saw didn't have
> much to do with parsing or generating XML.  I want
> object-oriented modules, and pure perl also, for the moment.
> 
> I am wondering whether I would be better off coding this
> conversion myself, which I would expect is less than 40-100
> lines of code, or whether the robustness or wider
> compatability brought by a CPAN module would be better.
> 
> I prefer a simple solution, so I could for example do this:
> 
> my $firstxml = "...";
> 
> my $objectmodel = SQL::ObjectModel::XML->from_xml( $firstxml
> );
> 
> my $secxml = SQL::ObjectModel::XML->to_xml( $objectmodel );
> 
> Then "$firstxml eq $secxml" probably returns true.
> 
> The hypothetical SQL::ObjectModel::XML module would have all
> the references to the CPAN module or alternately home grown
> implementation internally.
> 
> Thanks in advance for any suggestions. -- Darren Duncan


__________________________________
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com



More information about the Victoria-pm mailing list