Bobby
Bobby

Reputation: 271

Nokogiri/Ruby array question

I have a quick question. I am currently writing a Nokogiri/Ruby script and have the following code:

fullId = doc.xpath("/success/data/annotatorResultBean/annotations/annotationBean/concept/fullId")
fullId.each do |e|
            e = e.to_s()
            g.write(e + "\n")
    end

This spits out the following text:

<fullId>D001792</fullId>
<fullId>D001792</fullId>
<fullId>D001792</fullId>
<fullId>D008715</fullId>

I wanted the just the numbers text in between the "< fullid>" saved, without the < fullId>,< /fullId> markup. What am I missing?

Bobby

Upvotes: 0

Views: 334

Answers (1)

Greg Campbell
Greg Campbell

Reputation: 15302

I think you want to use the text() accessor (which returns the child text values), rather than to_s() (which serializes the entire node, as you see here).

I'm not sure what the g object you're calling write on is, but the following code should give you an array containing all of the text in the fullId nodes:

doc.xpath(your_xpath).map {|e| e.text}

Upvotes: 10

Related Questions