Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

generate XML command front to package

by jorg (Friar)
on Sep 02, 2003 at 09:42 UTC ( [id://288274]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, Are there any packages available that will translate

<myobject> <function name="init"> <parameter name="arg1">value1</parameter> <parameter name="arg2">value2</parameter> </function> </myobject>
to
my $object = new MyObject(); $object->init(value1, value2);
*automatically* ? I don't want to write the parsing code for all the packages i want to access this way. I remember a long time ago seeing something vaguely similar here on perlmonks. Any thoughts?

Jorg

"Do or do not, there is no try" -- Yoda

Replies are listed 'Best First'.
Re: generate XML command front to package
by jeffa (Bishop) on Sep 02, 2003 at 13:40 UTC
    You could use the technique i presented at XML::Simple + Class::MethodMaker. With your XML it would be something like:
    package MyObject; use Class::MethodMaker new_hash_init => 'new', get_set => [qw(arg1 arg2)], ; package main; use strict; use warnings; use Data::Dumper; use XML::Simple qw(2.08); my $xml = XMLin(\*DATA); my %arg = map { $_ => $xml->{function}{parameter}{$_}{content} } keys %{$xml->{function}{parameter}}; my $object = MyObject->new(%arg); print $object->arg1(), "\n"; print $object->arg2(), "\n"; __DATA__ <myobject> <function name="init"> <parameter name="arg1">value1</parameter> <parameter name="arg2">value2</parameter> </function> </myobject>
    If you control the XML, you might want to consider using a simpler approach, one that doesn't require addition munging like i had to do with map.

    Additionally, this is Perl. I am sure there is some way to dynamically create these classes. You could read the XML first, determine what class to create and what parameters to pass along, and then instantiate the object with the desired argument values.

    Then again, maybe SOAP or XML-RPC would serve you better.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: generate XML command front to package
by gjb (Vicar) on Sep 02, 2003 at 11:03 UTC

    You could also have a look at XML-RPC, it's more lightweight than SOAP, but of course a bit less versatile.

    Hope this helps, -gjb-

Re: generate XML command front to package
by edan (Curate) on Sep 02, 2003 at 09:53 UTC

    Sounds like a dirty job! You might want look into SOAP.

    HTH

    --
    3dan
      well soap is one way of doing this ofcourse but consider the following

      ->java XML frontend accessing perl backend (high load).

      Soap wouldn't give me the scalability i need, by using perl in a cgi environment i can piggyback on top of apache for performance.

      Jorg

      "Do or do not, there is no try" -- Yoda
        Soap wouldn't give me the scalability i need, by using perl in a cgi environment i can piggyback on top of apache for performance
        That is wrong. The only way apache/perl can give your high performance is through mod_perl, especially when using XML:: modules to parse xml properly. You're needlessly reinventing a wheel called SOAP.
Re: generate XML command front to package
by derby (Abbot) on Sep 02, 2003 at 12:29 UTC
    I don't know any off the top of my head but how about writing a stylesheet and then using XSLT?

    <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="text" indent="yes" encoding="ISO-8859-1"/> <xsl:template match="myobject"> eval { my $object = new MyObject();<xsl:apply-templates select="function" / +> }; </xsl:template> <xsl:template match="function"> $object-><xsl:value-of select="@name" />(<xsl:apply-templates select +="parameter"/>); </xsl:template> <xsl:template match="parameter"> <xsl:value-of select="." /> <xsl:if test="not(position()=last())">, </xsl:if> </xsl:template> </xsl:stylesheet>

    -derby

Re: generate XML command front to package
by cheshirecat (Sexton) on Sep 03, 2003 at 19:21 UTC
    You could look at XML::Config as the basis for your code

    Given a well formed configuration file like this:

    <Root> <A_variable>A Key</A_variable> </Root> it returns: {A_variable => 'A Key'}

    'Cat

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://288274]
Approved by broquaint
Front-paged by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2024-04-26 07:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found