Pulling text from elements with REXML

P

Paul Willis

Hi

I am using REXML to pull text from a NewsML document.

require 'rexml/document'
include REXML
file = File.new("Main_News.xml")
doc = Document.new(file)
root = doc.root
puts
root.elements["NewsItem/NewsComponent/NewsComponent[1]/NewsComponent/ContentItem/DataContent/nitf/body/body.head/hedline/hl1"]

Gives me...

<hl1>Blueprint to cut emissions unveiled</hl1>

Is there an easy way (ie something in REXML) to pull just the text
without the containers <hl1> and </hl1>.

Paul
 
P

Peter Szinek

Paul said:
Hi

I am using REXML to pull text from a NewsML document.

require 'rexml/document'
include REXML
file = File.new("Main_News.xml")
doc = Document.new(file)
root = doc.root
puts
root.elements["NewsItem/NewsComponent/NewsComponent[1]/NewsComponent/ContentItem/DataContent/nitf/body/body.head/hedline/hl1"]

Gives me...

<hl1>Blueprint to cut emissions unveiled</hl1>

Is there an easy way (ie something in REXML) to pull just the text
without the containers <hl1> and </hl1>.

If I understood correctly, you need the text content of the node rather
than the whole node. This can be accomplished with:

some_element.text

so you could do something like

root.elements[...your stuff_here...].to_a.each {|e| puts e.text}

HTH,
Peter
__
http://www.rubyrailways.com :: Ruby and Web2.0 blog
http://scrubyt.org :: Ruby web scraping framework
http://rubykitchensink.ca/ :: The indexed archive of all things Ruby
 
P

Paul Willis

Peter said:
If I understood correctly, you need the text content of the node rather
than the whole node. This can be accomplished with:

some_element.text

You did understand correctly, .text on the end was all I needed.

Cheers

Paul
 
P

Phrogz

root.elements["NewsItem/NewsComponent/NewsComponent[1]/NewsComponent/Conten tItem/DataContent/nitf/body/body.head/hedline/hl1"]

Gives me...

<hl1>Blueprint to cut emissions unveiled</hl1>

Is there an easy way (ie something in REXML) to pull just the text
without the containers <hl1> and </hl1>.

require 'rexml/document'
doc = REXML::Document.new("<root><kid>hello world</kid></root>")
p REXML::XPath.first( doc, '/root/kid/text()' )
#=> "hello world"
 
P

Phrogz

root.elements["NewsItem/NewsComponent/NewsComponent[1]/NewsComponent/Conten tItem/DataContent/nitf/body/body.head/hedline/hl1"]
Gives me...
<hl1>Blueprint to cut emissions unveiled</hl1>
Is there an easy way (ie something in REXML) to pull just the text
without the containers <hl1> and </hl1>.

require 'rexml/document'
doc = REXML::Document.new("<root><kid>hello world</kid></root>")
p REXML::XPath.first( doc, '/root/kid/text()' )
#=> "hello world"

Also, depending on your needs:

include REXML
doc = Document.new("<root><kid>hello</kid><kid>world</kid></root>")
p XPath.match( doc, '/root/kid/text()' )
#=> ["hello", "world"]
 
K

Keith Fahlgren

Hey,

Two notes:
1. I always suggest the REXML::XPath methods over the others for
people who grok XPath.
2. A REXML::XPath.* ... text() match will return a REXML::Text node,
which may _not_ be what you want:

$ irb --simple-prompt foo.rb=> REXML::Text

Just something to be aware of (use .to_s if you want a string, as usual).


HTH,
Keith
 
P

Paul Willis

require 'rexml/document'
doc = REXML::Document.new("<root><kid>hello world</kid></root>")
p REXML::XPath.first( doc, '/root/kid/text()' )
#=> "hello world"

Thanks for that, I'm now using REXML::XPath with a combination of .first
and .match to pull the element text out.

One more thing, given an XML document...

<root><kid stuff="some-other-text">hello world</kid></root>

What would be the path to the attribute 'stuff' and return
'some-other-text'?

Paul
 
P

Phrogz

One more thing, given an XML document...

<root><kid stuff="some-other-text">hello world</kid></root>

What would be the path to the attribute 'stuff' and return
'some-other-text'?

require 'rexml/document'
include REXML
doc = Document.new( <<ENDDOC )
<root>
<kid stuff="some-other-text">hello world</kid>
<kid class="best" stuff="gibbles">hello world</kid>
</root>
ENDDOC

att = XPath.first( doc, '//kid/@stuff' )
p att, att.class, att.value
#=> stuff='some-other-text'
#=> REXML::Attribute
#=> "some-other-text"

p XPath.first( doc, '//kid[@class="best"]/@stuff' ).value
#=> "gibbles"

I don't know what the XPath syntax is to select the value of an
attribute directly. I'd be interested to know if someone else knows it.
 
P

Paul Willis

Gavin said:
att = XPath.first( doc, '//kid/@stuff' )
I don't know what the XPath syntax is to select the value of an
attribute directly. I'd be interested to know if someone else knows it.

Cheers, it was the kid/@stuff I needed...

puts XPath.first( doc, '/root/kid/@stuff' )

#=> some-other-text

Paul
 
P

Phrogz

Cheers, it was the kid/@stuff I needed...

puts XPath.first( doc, '/root/kid/@stuff' )

#=> some-other-text

Nice, I didn't realize that REXML::Attribute had such different output
for #inspect versus #to_s. It's nice, then, that you don't need to
call .value in this particular case. Just be aware that without
the .value call you still have an Attribute instance that can just be
treated as a string in some areas:

att = XPath.first( doc, '//kid/@stuff' )

puts att
#=> some-other-text

puts att.value + '-more'
#=> some-other-text-more

puts att + "-more"
#=> tmp.rb:17: undefined method `+' for
stuff='some-other-text':REXML::Attribute (NoMethodError)
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top