rexml each_element_with_attribute question

Z

zerohalo

I'm a Ruby newbie and have been working on a script that among other
things, parses a series of XML files, searches for elements that
contain a certain attribute ('question') and then parsing through the
children of all of those elements for certain tags. Note that
'question' elements can be anywhere in the XML file - one level deep or
ten levels deep - so doing an exact path match for 'question' doesn't
work.

I thought I had found an easy way with
doc.root.each_element_with_attribute('question') and that it would
return an array of all of the 'question' elements in the whole file.
But after much frustration I finally realized that it only returns the
elements with that attribute that are at that level - it doesn't
recurse through all the children in the file to find them all. Hard to
believe that there's not an option to have it recurse through all the
children, but maybe I haven't found it. The documentation on rexml
isn't that extensive and I had a hard time understanding some things
(of course that's also due to the the fact that I'm a n00b).

I've tried using XPath.match('question') but Xpath requires the path,
and like I said, these 'question' elements could be at any level,
depending on the XML file, so I'm not sure that that works either
(other than going through each level of the document "manually", but
that sucks).

Does anyone know of a simple way, using something like
each_element_with_attribute, to return an array of all of the elements
that match a certain attribute?

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top