Many thanks for the feedback.
Structure and Data can vary (depending on functionality - in this case ipv4 vs.
ipv6).
I was only doing one thing wrong in my code (one level of indirection too much)
The following solution works:
:addressrange => Hash[vnet.xpath('./AR_POOL/AR/AR_ID').collect { |ar|
[ar.text, Hash[ar.xpath('../*').collect { |singlear| [singlear.name.downcase,
singlear.text.downcase] } ] ] } ]
I know receive all data as nested hash in the address range attribute.
Best,
Martin
On 25 Feb 2015, at 20:18, Thomas Hallgren <[email protected]>
wrote:
> 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 [email protected].
> 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.
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/puppet-dev/0C155D6E-1F24-4B82-B2D8-BA2A93801A9C%40gmail.com.
For more options, visit https://groups.google.com/d/optout.