[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[no subject]



I'm trying to identify a better way to parse the XML via XML::Parser;
Normally I use the xml subs method but I intend to have everything
readable in one perl program.  I'm also using OOP.  So I want a package
for each noun but I do not want a package for each noun and one for xml
parsing of data for each noun.  Here is what my code looks like

----------------------------------------
  # Retrieve ENS Hostname
  my $ens_network = ENSNetwork->new($ua);
  $ens_network->load();


{
  package ENSNetwork;

  sub new {
    my ($proto, $ua) = @_;
    my $class = ref($proto) || $proto;

    my $self = {
      'server' => $ua,
      'errstr'     => undef,
    };
    bless ($self,$class);
    return $self;

  }

  sub load {
    my ($self) = shift;
    my $server = Frontier::Client->new( debug => 0,
      url =&gt; &quot;<a  rel="nofollow" href="http://$ens_ip/cgi-bin/xml-rpc&quot;,ua";>http://$ens_ip/cgi-bin/xml-rpc&quot;,ua</a> =&gt; $self-&gt;{'server'} );
    my $result = $server-&gt;call('get_network_config', (&quot;0&quot;));
    $result = MIME::Base64::decode_base64($result);
    print &quot;$result\n&quot;;

    # Need to parse the data!

  }
}

----------------------------------------

Here is what XML data looks like when retrieved:
----------------------------------------
&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;config&gt;
  &lt;network id=&quot;1&quot;&gt;
    &lt;active&gt;enabled&lt;/active&gt;
    &lt;fqdn&gt;localhost.localdomain.com&lt;/fqdn&gt;
    &lt;dhcp&gt;disabled&lt;/dhcp&gt;
    &lt;address&gt;192.168.2.137&lt;/address&gt;
    &lt;broadcast&gt;192.168.2.255&lt;/broadcast&gt;
    &lt;netmask&gt;255.255.255.0&lt;/netmask&gt;
    &lt;gateway&gt;192.168.2.254&lt;/gateway&gt;
    &lt;nameserver1&gt;192.168.2.254&lt;/nameserver1&gt;
    &lt;nameserver2&gt;0.0.0.0&lt;/nameserver2&gt;
    &lt;nameserver3&gt;0.0.0.0&lt;/nameserver3&gt;
    &lt;forwarding&gt;disabled&lt;/forwarding&gt;
  &lt;/network&gt;
  &lt;network id=&quot;2&quot;&gt;
    &lt;active&gt;disabled&lt;/active&gt;
    &lt;fqdn&gt;localhost.localdomain.com&lt;/fqdn&gt;
    &lt;dhcp&gt;enabled&lt;/dhcp&gt;
    &lt;address&gt;0.0.0.0&lt;/address&gt;
    &lt;broadcast&gt;0.0.0.0&lt;/broadcast&gt;
    &lt;netmask&gt;0.0.0.0&lt;/netmask&gt;
    &lt;gateway&gt;0.0.0.0&lt;/gateway&gt;
    &lt;nameserver1&gt;0.0.0.0&lt;/nameserver1&gt;
    &lt;nameserver2&gt;0.0.0.0&lt;/nameserver2&gt;
    &lt;nameserver3&gt;0.0.0.0&lt;/nameserver3&gt;
    &lt;forwarding&gt;disabled&lt;/forwarding&gt;
  &lt;/network&gt;
&lt;/config&gt;
--------------------------------------


Can I have a package within a package:

{
  package ENSNetwork;

  { 
    package ens_network_xml_subs;
  }
}

That makes reading a pain.  If that is doable how would the XML::Parser
gain access to the current object's data?  Normally I declare a hash as
readable by the whole program like this:


my %device = ()

{ 
  package xml_subs;

  sub device {
    my(undef, undef, %attrs) = @_;
    $device{'id'} = $attrs{'id'};
  }

}



On Thu, 2005-11-10 at 09:41 -0500, Philip Polstra wrote:
&gt; I would use split instead of what you are doing with the pattern
&gt; match.  Something like this
&gt; ($verb, $noun, $cmdline) = split /\s+/, $_, 3;
&gt; 
&gt; You can then check to see if $noun and $cmdline are defined to know
&gt; how many parameters were passed.
&gt; 
&gt; On 11/10/05, Christopher Fowler &lt;cfowler at outpostsentinel.com&gt; wrote:
&gt;         Perl Guru's:
&gt;         
&gt;         I'm trying to create a config shell that runs in Linux that
&gt;         will use 
&gt;         XML-RPC to configure our embedded device.  The XML-RPC thing
&gt;         is
&gt;         complete.  What I'm working on now is parsing the command
&gt;         line.
&gt;         
&gt;         here is my code:
&gt;         
&gt;         --------------------------------
&gt;         sub prompt {
&gt;           print &quot;# &quot;; 
&gt;           my $cmd = &lt;STDIN&gt;;
&gt;           chomp $cmd;
&gt;           $cmd =~ m/^(.+?)\s(.+?)\s(.+?)$/;
&gt;           my $verb = $1;
&gt;           my $noun = $2;
&gt;           my $cmdline = $3;
&gt;           print &quot;[[$verb]]\n&quot;;
&gt;           return ($verb, $noun, $cmdline); 
&gt;         }
&gt;         -------------------------------
&gt;         
&gt;         $1, $2, and $3 substitutions fail when there is only one verb
&gt;         like
&gt;         'exit'.  Verbs can be 'show, set, add, delete, or
&gt;         exit'.  Nouns can be
&gt;         'port, user, vtun, snmp, etc.'   So to show port 1's config I
&gt;         use 'show 
&gt;         port 1'  To show all ports I use 'show port'.  I guess I could
&gt;         simply
&gt;         use split and split on \s boundaries.  for $cmdline I need to
&gt;         be
&gt;         smarter.  Some items have descriptions that can include a
&gt;         space and I
&gt;         will want to cut $cmdline into arguments. 
&gt;         
&gt;         'set system location &quot;Buford Development&quot;'
&gt;         verb = set
&gt;         noun = system
&gt;         arg[0] = location
&gt;         arg[1] = 'Buford Development'
&gt;         
&gt;         So I'm trying to do shell like parsing.  I bet there is a
&gt;         module that 
&gt;         does all this for me.  If not can someone point me in the best
&gt;         direction.
&gt;         
&gt;         Thanks,
&gt;         Chris
&gt;         
&gt;         
&gt;         _______________________________________________
&gt;         Ale mailing list
&gt;         Ale at ale.org
&gt;         <a  rel="nofollow" href="http://www.ale.org/mailman/listinfo/ale";>http://www.ale.org/mailman/listinfo/ale</a>
&gt; 
&gt; _______________________________________________
&gt; Ale mailing list
&gt; Ale at ale.org
&gt; <a  rel="nofollow" href="http://www.ale.org/mailman/listinfo/ale";>http://www.ale.org/mailman/listinfo/ale</a>



</pre>
<!--X-Body-of-Message-End-->
<!--X-MsgBody-End-->
<!--X-Follow-Ups-->
<hr>
<ul><li><strong>Follow-Ups</strong>:
<ul>
<li><strong><a name="00164" href="msg00164.html">[ale] Perl parsing problem</a></strong>
<ul><li><em>From:</em> cfowler at outpostsentinel.com (Christopher Fowler)</li></ul></li>
</ul></li></ul>
<!--X-Follow-Ups-End-->
<!--X-References-->
<ul><li><strong>References</strong>:
<ul>
<li><strong><a name="00159" href="msg00159.html">[ale] Perl parsing problem</a></strong>
<ul><li><em>From:</em> cfowler at outpostsentinel.com (Christopher Fowler)</li></ul></li>
<li><strong><a name="00160" href="msg00160.html">[ale] Perl parsing problem</a></strong>
<ul><li><em>From:</em> ppolstra at gmail.com (Philip Polstra)</li></ul></li>
</ul></li></ul>
<!--X-References-End-->
<!--X-BotPNI-->
<ul>
<li>Prev by Date:
<strong><a href="msg00160.html">[ale] Perl parsing problem</a></strong>
</li>
<li>Next by Date:
<strong><a href="msg00162.html">[ale] Could not solve SUSE 10.0 slow performance with Asus Intel	cpu/chipset</a></strong>
</li>
<li>Previous by thread:
<strong><a href="msg00160.html">[ale] Perl parsing problem</a></strong>
</li>
<li>Next by thread:
<strong><a href="msg00164.html">[ale] Perl parsing problem</a></strong>
</li>
<li>Index(es):
<ul>
<li><a href="maillist.html#00161"><strong>Date</strong></a></li>
<li><a href="threads.html#00161"><strong>Thread</strong></a></li>
</ul>
</li>
</ul>

<!--X-BotPNI-End-->
<!--X-User-Footer-->
<!--X-User-Footer-End-->
</body>
</html>