[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

rexml child nodes

Charles L. Snyder

6/6/2005 2:37:00 PM

Hi,

I having been learning to use ruby for about 1 week (v. new). I am trying to
use ruby w rexml to read in xml documents of the form:

<QUIZ>

<QUESTION TEXT="What is 1 + 1" ANSWER="2" EXPLAIN="The answer is 2">
<CHOICE>3</CHOICE>
<CHOICE>1</CHOICE>
<CHOICE>2</CHOICE>
<CHOICE>6</CHOICE>
</QUESTION>

</QUIZ>

I am able to get the Questions with:

require "rexml/document"
include REXML
doc = Document.new File.new("CVPhys.xml")
quiz=[]
doc.elements.each("QUIZ/QUESTION") { |element| quiz.push
element.attributes["TEXT"] }
puts quiz

But, I can't figure out A) how to get at the vaules in the <CHOICE> nodes -
the code below just gives me results of the form "<CHOICE>6</CHOICE>"

require "rexml/document"
include REXML
doc = Document.new File.new("myQuiz.xml")
quiz=[]
doc.elements.each("QUIZ/QUESTION/CHOICE") { |element| quiz.push element}
puts quiz

And B) how to read the xml doc line by line to output as a file with the
ultimate form:


What is 1 +1
3
False! The answer is 2
1
False! The answer is 2
2
True! The answer is 2
6
False! The answer is 2


Thanks !


5 Answers

Premshree Pillai

6/6/2005 2:53:00 PM

0

On 6/6/05, Charles L. Snyder <csnyder1@kc.rr.com> wrote:
> But, I can't figure out A) how to get at the vaules in the <CHOICE> nodes -
> the code below just gives me results of the form "<CHOICE>6</CHOICE>"
>
> require "rexml/document"
> include REXML
> doc = Document.new File.new("myQuiz.xml")
> quiz=[]
> doc.elements.each("QUIZ/QUESTION/CHOICE") { |element| quiz.push element}
> puts quiz

doc.elements.each("QUIZ/QUESTION/CHOICE") { |element|
p element.text
}

--
Premshree Pillai
http://www.livejournal.com/users/...


james_b

6/6/2005 3:01:00 PM

0

Charles L. Snyder wrote:
> But, I can't figure out A) how to get at the vaules in the <CHOICE> nodes -
> the code below just gives me results of the form "<CHOICE>6</CHOICE>"
>
> require "rexml/document"
> include REXML
> doc = Document.new File.new("myQuiz.xml")
> quiz=[]
> doc.elements.each("QUIZ/QUESTION/CHOICE") { |element| quiz.push element}

quiz.push element.text

An element has tags and content, so you have to ask for that content.

James

--

http://www.ru... - The Ruby Documentation Site
http://www.r... - News, Articles, and Listings for Ruby & XML
http://www.rub... - The Ruby Store for Ruby Stuff
http://www.jame... - Playing with Better Toys


Robert Klemme

6/6/2005 5:00:00 PM

0

James Britt wrote:
> Charles L. Snyder wrote:
>> But, I can't figure out A) how to get at the vaules in the <CHOICE>
>> nodes - the code below just gives me results of the form
>> "<CHOICE>6</CHOICE>"
>>
>> require "rexml/document"
>> include REXML
>> doc = Document.new File.new("myQuiz.xml")
>> quiz=[]
>> doc.elements.each("QUIZ/QUESTION/CHOICE") { |element| quiz.push
>> element}
>
> quiz.push element.text
>
> An element has tags and content, so you have to ask for that content.

Yeah, and most likely a nested version is needed because Charles will
likely want questions *and* choices. Something like this:

# untested
require 'ostruct'
require "rexml/document"
include REXML
doc = Document.new File.new("myQuiz.xml")
quiz=[]

doc.elements.each("QUIZ/QUESTION") do |el_q|
question = OpenStruct.new
question.text = el_q.attributes["TEXT"]
question.answer = el_q.attributes["ANSWER"]
# ...
el_q.elements( "CHOICE" ) do |el_ch|
(question.choices ||= []) << el_ch.text
end
end

p quiz

Kind regards

robert

Charles L. Snyder

6/7/2005 4:27:00 AM

0

Thanks to everyone for their suggestions!
This code does absolutely everything I need, except the resulting text file
has no line breaks. How do force a line break for each of the elements in
the array being sent to the output text file ?

require 'ostruct'
require "rexml/document"
include REXML
print "What is the name of the xml file?"
p myFile = gets.chomp
doc = Document.new File.new("#{myFile}"+'.xml')
quiz=[]

doc.elements.each("QUIZ/QUESTION") do |el_q|
question = OpenStruct.new
quiz.push question.text = el_q.attributes["TEXT"]
question.answer = el_q.attributes["EXPLAIN"]
i=0
# ...
el_q.elements.each("CHOICE") do |el_ch|
(question.choices||=[]) << el_ch.text
quiz.push "False! "+ ("#{question.answer}")
quiz.push("#{question.choices[i]}")
i+=1
end
end
File::open("#{myFile}" + '.quiz', 'w') do |f|
f << quiz
end

Thanks again!


"Charles L. Snyder" <csnyder1@kc.rr.com> wrote in message
news:IcZoe.6716$li.5419@tornado.rdc-kc.rr.com...
> Hi,
>
> I having been learning to use ruby for about 1 week (v. new). I am trying
to
> use ruby w rexml to read in xml documents of the form:
>
> <QUIZ>
>
> <QUESTION TEXT="What is 1 + 1" ANSWER="2" EXPLAIN="The answer is 2">
> <CHOICE>3</CHOICE>
> <CHOICE>1</CHOICE>
> <CHOICE>2</CHOICE>
> <CHOICE>6</CHOICE>
> </QUESTION>
>
> </QUIZ>
>
> I am able to get the Questions with:
>
> require "rexml/document"
> include REXML
> doc = Document.new File.new("CVPhys.xml")
> quiz=[]
> doc.elements.each("QUIZ/QUESTION") { |element| quiz.push
> element.attributes["TEXT"] }
> puts quiz
>
> But, I can't figure out A) how to get at the vaules in the <CHOICE>
nodes -
> the code below just gives me results of the form "<CHOICE>6</CHOICE>"
>
> require "rexml/document"
> include REXML
> doc = Document.new File.new("myQuiz.xml")
> quiz=[]
> doc.elements.each("QUIZ/QUESTION/CHOICE") { |element| quiz.push element}
> puts quiz
>
> And B) how to read the xml doc line by line to output as a file with the
> ultimate form:
>
>
> What is 1 +1
> 3
> False! The answer is 2
> 1
> False! The answer is 2
> 2
> True! The answer is 2
> 6
> False! The answer is 2
>
>
> Thanks !
>
>


Robert Klemme

6/7/2005 7:59:00 AM

0

Charles L. Snyder wrote:
> Thanks to everyone for their suggestions!
> This code does absolutely everything I need, except the resulting
> text file has no line breaks. How do force a line break for each of
> the elements in the array being sent to the output text file ?
>
> require 'ostruct'
> require "rexml/document"
> include REXML
> print "What is the name of the xml file?"
> p myFile = gets.chomp
> doc = Document.new File.new("#{myFile}"+'.xml')

Btw, you are not closing the file here. And you just need a single string
interpolation. You can do

doc = File.open("#{myFile}.xml") {|io| Document.new io}

> quiz=[]
>
> doc.elements.each("QUIZ/QUESTION") do |el_q|
> question = OpenStruct.new
> quiz.push question.text = el_q.attributes["TEXT"]
> question.answer = el_q.attributes["EXPLAIN"]
> i=0
> # ...
> el_q.elements.each("CHOICE") do |el_ch|
> (question.choices||=[]) << el_ch.text
> quiz.push "False! "+ ("#{question.answer}")

Suggestion:

quiz.push "False! #{question.answer}"

> quiz.push("#{question.choices[i]}")

Another one:

quiz.push(question.choices[i].to_s)

> i+=1
> end
> end
> File::open("#{myFile}" + '.quiz', 'w') do |f|
> f << quiz

Insert linebreaks:
f.puts quiz

> end
>

Kind regards

robert