Junos Automation (Scripting)
Reply
Super Contributor
jschulman
Posts: 145
Registered: ‎11-03-2009
0

Re: Processing XML in PERL

Hi Mattia,

 

Thank you for your post.  I did indeed experiment with using XML::smileyfrustrated:imple (it was my first one!).  This is very good for doing simple things (as the name implies). 

 

The significant benefit of using something like XML::LibXML is when you get into situations that require XPath.  It is possible to do the same things in 'native perl' that you could do in XPath, but it's an aweful amount of hand coding for things that XPath provides natively.  There are a few other benefits to XML::LibXML that relate to more complex coding examples as well.

 

Here's an example re:XPath.  Let's say you have an EX4200 and you want to find interfaces that are not in the default VLAN; i.e. they've been assigned to a specific VLAN.  The XML::LibXML findnode() method takes an XPath expression:

 

   @vlan_members = $ifd->findnodes('interface-vlan-member-list/interface-   vlan-member[not(interface-vlan-name = "default")]');

 

And @vlan_members would contain the list.

 

So XML::smileyfrustrated:imple is good for some things and XML::LibXML is a much more complex super-set that does just about everything.  They can be used together to get the best of both worlds too!

 

Hope this helps.


Cheers,

-- Jeremy

 

Cheers,
-- Jeremy

@nwkautomaniac
Recognized Expert
Mattia
Posts: 198
Registered: ‎03-17-2010
0

Re: Processing XML in PERL

[ Edited ]

Hi Jeremy!

 

Thanks for your reply,

 

XML::LibXML seems very interesting indeed; in fact, I was using SML::smileyfrustrated:imple for some easy tasks, so far...

But I understand that browsing through xml nodes can be quite hard with that module...  the example you posted show how easier is doing it through XML::LibXML...

 

I think that tomorrow I will give it a try, I'll let you know!

 

.................................................................................
JNCIP-ENT, JNCIP-SEC, JNCIS-SP
(If this post helped you, please mark it as an "Accepted Solution"; kudos are also appreciated!)


Super Contributor
B2
Posts: 188
Registered: ‎05-06-2010
0

Re: Processing XML in PERL

I too started off with XML::smileyfrustrated:imple, when you deal with some large/complex XML it can be very challenging to dig through the PERL data structures though, and your code gets hard to read.

 

XML::smileyvery-happy:OM::XPath makes it very easy to pick out the data you want with Xpath expressions, as stated earlier in the thread I had much more success with that package than the LibXML.  I can provide some example code if you ever want to give it a try.

Super Contributor
jschulman
Posts: 145
Registered: ‎11-03-2009
0

Re: Processing XML in PERL

Hi B2,

 

I would be interested in seeing some sample code. 

 

I also use XML::LibXML to create XML documents for the purposes of configuration changes.  I found that LibXML has a very feature rich set of capabilities in this area.  I have not experimented with XML::smileyvery-happy:OM::* so I'd be interested in your experiences with it.

 

Thank you!

 

Cheers,

-- Jeremy

Cheers,
-- Jeremy

@nwkautomaniac
Super Contributor
B2
Posts: 188
Registered: ‎05-06-2010
0

Re: Processing XML in PERL

  I haven't messed much with configuration changes, but plan to shortly, so I may want to take a look into LibXML more deeply in the near future.  As far as XPath, it is very similar to the snipper you showed of LibXML, but you don't have to much about with stripping off attributes.  For example, here is a snippet of getting and printing out all BGP peers:

 

my $dom = XML::smileyvery-happy:OM::smileytongue:arser->new();

 

#create Netconf Manager object, call it $jnx

 

my $query = "get_bgp_summary_information";
my $rpc_reply = $jnx->$query;

my $reply = $dom->parse($rpc_reply);

 

my @peers = $reply->findnodes('//bgp-peer');

foreach my $peer (@peers) {

    print $peer->findvalue('peer-address') . "\n";

}

 

Copyright© 1999-2013 Juniper Networks, Inc. All rights reserved.