Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
No such thing as a small change
 
PerlMonks  

generate XML command front to package

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

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

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 edan (Curate) on Sep 02, 2003 at 05: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 gjb (Vicar) on Sep 02, 2003 at 07: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 derby (Abbot) on Sep 02, 2003 at 08: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 jeffa (Bishop) on Sep 02, 2003 at 09: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 cheshirecat (Sexton) on Sep 03, 2003 at 15: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
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.