开发者

Nokogiri/Ruby array question

开发者 https://www.devze.com 2023-01-08 18:58 出处:网络
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\"

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 f开发者_StackOverflow社区ollowing 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


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}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号