[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

libxml iterate xml tree

Philipp

10/20/2007 5:44:00 PM

Hello Everyone,
i dumped my whole saturday on a small xml problem :-)

i have a xml document

e.g

<car>
<door id=1>
<door_handle id=1 color='black'>
</door_handle>
<door_handle id=2 color='blue'>
</door_handle>
</door>

<door id=2>
<door_handle id=3 color='green'>
</door_handle>
</door>

</car>

now i want to get all door_handle from each door ?
i tried to iterate it with

running_config_xml = XML::Document.file(@running_config_file)
car = running_config_xml.root

car.each{|door|
door.each{|door_handle|
puts door_handle['color']
}
}


fist it looks working but every second door_handle had no color ..
i probably iterate it wrong..

is there a trick to do this ??

thanks ahead


Philipp

1 Answer

Philipp

10/21/2007 10:28:00 AM

0

if i do it like

car.each{|door| if door.child?
door.each{|door_handle| if door_handle.child?
puts "color #{door_handle['color']}"

end }

end}

it shows only the elements with content !!
dont know why but its working ..



On 20 Okt., 19:43, Philipp <philipp.goetzin...@googlemail.com> wrote:
> Hello Everyone,
> i dumped my whole saturday on a small xml problem :-)
>
> i have a xml document
>
> e.g
>
> <car>
> <door id=1>
> <door_handle id=1 color='black'>
> </door_handle>
> <door_handle id=2 color='blue'>
> </door_handle>
> </door>
>
> <door id=2>
> <door_handle id=3 color='green'>
> </door_handle>
> </door>
>
> </car>
>
> now i want to get all door_handle from each door ?
> i tried to iterate it with
>
> running_config_xml = XML::Document.file(@running_config_file)
> car = running_config_xml.root
>
> car.each{|door|
> door.each{|door_handle|
> puts door_handle['color']
> }
>
> }
>
> fist it looks working but every second door_handle had no color ..
> i probably iterate it wrong..
>
> is there a trick to do this ??
>
> thanks ahead
>
> Philipp