views:

127

answers:

2

I'm using Hpricot for traversing an XML packet. For each node I'm on, I want to get a list of the immediate children . However when using

(current_node/:section)

I'm getting ALL descendant sections, not just the immediate children.

How can I get around this?

A: 

From the documentation:

If you’re looking for a single element, the at method will return the first element matched by the expression. In this case, you’ll get back the element itself rather than the Hpricot::Elements array.

Does the following works for you?

current_node.at(:section)

If you prefer, you can also use the xpath child operator.

Simone Carletti
I'm looking for all the direct children, and those children could be anything
Neil Middleton
Yeah this isn't what the question asked - he is asking for ALL children, not the first.
Brian Armstrong
+1  A: 

You can just use current_node.children.

Chuck
This works, but since it's node based it also gives line endings/return characters: "\n". So you would have to iterate through the result array and pick out just the elements, etc.Also, it returns the whole element. So if that element has children then `doc.at("element1").children[1]` could return `{elem <element2> "\n" {emptyelem <element3>} "\n" </element2>}` for example. I can't figure out from the original question whether this is a problem or not.
i5m