I’ve been looking around for libraries that will allow me to get a multidimensional hash of a given XHTML string.
XHTML:
<div class="class-1 class-2" id="my-id">
<div class="classy">
</div>
</div>
Expected Hash:
hash = {
:div => {
:class => ['class-1', 'class-2'],
:id => ['my-id'],
:children => {
:div => {
:class => ['classy']
}
}
}
}
Your example does not really give a well defined definition of what should be returned. Are text nodes ignored? What happens if an element has multiple
<div>child elements? What happens if the outer<div>element has an attribute namedchildren?In addition to that, you probably shouldn’t build a structure like this if you have a way of using the built-in data structure of the XML/HTML parsing library of your choice, and using XPath queries to arrive at the data nodes you want.
Disregarding all of the above, here is a simple start that may come close to what you have in mind.
Use as follows: