I would take a look at nokogiri. It's an XML/HTML parser for Ruby. Some helpful hints here: http://stackoverflow.com/questions/11198239/parsing-xml-with-ruby

- thomas

On 2015-02-25 19:43, Henrik Lindberg wrote:
On 2015-25-02 18:17, Martin Alfke wrote:
Hi,

I am stuck at a puppet provider where I need to read data from xml.

Data which needs to get parsed:

     <AR>
       <AR_ID><![CDATA[8]]></AR_ID>
       <IP><![CDATA[172.16.100.208]]></IP>
<MAC><![CDATA[02:00:ac:10:64:d0]]></MAC>
       <SIZE><![CDATA[1]]></SIZE>
       <TYPE><![CDATA[IP4]]></TYPE>
       <USED_LEASES>0</USED_LEASES>
       <LEASES/>
     </AR>
     <AR>
       <AR_ID><![CDATA[9]]></AR_ID>
       <IP><![CDATA[172.16.100.209]]></IP>
<MAC><![CDATA[02:00:ac:10:64:d1]]></MAC>
       <SIZE><![CDATA[1]]></SIZE>
       <TYPE><![CDATA[IP4]]></TYPE>
       <USED_LEASES>0</USED_LEASES>
       <LEASES/>
     </AR>


Expected result:

:addressrange =>
   { ‘1' =>
     { ‘ip' => ‘172.16.100.208’, ‘mac' => ’02:00:ac:10:64:d0’ },
      ‘2' =>
     { ‘ip' =>  ‘172.16.100.209’, ‘mac' => ’02:00:ac:10:64:d1’ }
   }




What would be proper code to build the hash as expected?


That is not easy. Is the structure static and only varying where the actual data is? If so, you could hard code a solution. If you expect there to be variability in the tags/layout you should consider using an XML parser and get the data from it.

To do this with regular expressions you need to match the part:

       <IP><![CDATA[172.16.100.209]]></IP>
       <MAC><![CDATA[02:00:ac:10:64:d1]]></MAC>

with something like this (untested):

/<IP><!\[CDATA|[(.*)\]]></IP>.*<MAC><!\[CDATA\[(.*)\]]></MAC>/

Then apply that to iterate over matches - you need to do matching that
accepts newlines as a match for . etc.

Play with it in Rubular (online ruby regexp tester) until you get it right.

- henrik

--
You received this message because you are subscribed to the Google Groups "Puppet 
Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to puppet-dev+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/puppet-dev/54EE2012.8020206%40puppetlabs.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to