[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Combining XmlMarkup objects?

Andrew Cox

6/9/2005 10:38:00 PM

Is it possible to create an XmlMarkup object and then populate the
nodes within another method? What I'm trying to do is create an
XmlMarkup object, start it off .... and then add nodes to it in
another method. Here's the basic code (that isn't exactly working).
The log_results method will be called from another location. What I
want the final result to look like is something like:

<?xml version="1.0" encoding="UTF-8"?>
<results>
<result testcase="test_a">
<status>PASSED</status>
</result>

<result testcase="test_b">
<status>FAILED</status>
</result>
</results>

What I've been able to do is attach the internal <result> nodes to the
@xml variable, but I can't get them inside a containing <results>
node. In the code below, @results does have all of the XML in it, but
I can't figure out how to stick it into the @xml object. Is this even
possible?

The initialize method is called first (obviously) and the log_results
method is called several times from another class and then finally the
end_log method is called.

==
def initialize(fileName, logsToKeep, maxLogSize, xmlFileName )
@logfile = File.new(xmlFileName, "w")
@xml = XmlMarkup.new(:target=>@logfile, :indent=>2)
@xml.instruct! #insert processing instruction
@results = XmlMarkup.new
end

def log_results(name, input, expected, test_status)
r = XmlMarkup.new(:target=>@results, :indent=>2)
r.result(:testcase => name) do
r.status(test_status)
end #results tag
end #log_results

def end_log
@xml.results do
@results
end
@logfile.close
end #end_log
==

Drew Cox


2 Answers

Jim Weirich

6/10/2005 11:27:00 AM

0

On Thursday 09 June 2005 06:38 pm, Andrew Cox wrote:
> def initialize(fileName, logsToKeep, maxLogSize, xmlFileName )
>   @logfile = File.new(xmlFileName, "w")
>   @xml = XmlMarkup.new(:target=>@logfile, :indent=>2)
>   @xml.instruct! #insert processing instruction
>   @results = XmlMarkup.new
> end
>
> def log_results(name, input, expected, test_status)
>   r = XmlMarkup.new(:target=>@results, :indent=>2)
>   r.result(:testcase => name) do
>     r.status(test_status)
>   end #results tag
> end #log_results
>
> def end_log
>   @xml.results do
>     @results
>   end
>   @logfile.close
> end #end_log

The following should work for you:

class TestResultsLogger
def initialize(fileName, logsToKeep, maxLogSize, xmlFileName )
@logfile = File.new(xmlFileName, "w")
@xml = Builder::XmlMarkup.new(:target=>@logfile, :indent=>2)
@xml.instruct! #insert processing instruction
@results =
Builder::XmlMarkup.new(:target=>@results, :indent=>2, :margin=>1)
end

def log_results(name, input, expected, test_status)
@results.result(:testcase => name) do
@results.status(test_status)
end
end

def end_log
@xml.results do
@xml << @results.target!
end
@logfile.close
end
end

--
-- Jim Weirich jim@weirichhouse.org http://onest...
-----------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)


Andrew Cox

6/10/2005 11:45:00 AM

0

On 6/10/05, Jim Weirich <jim@weirichhouse.org> wrote:
> On Thursday 09 June 2005 06:38 pm, Andrew Cox wrote:
> > def initialize(fileName, logsToKeep, maxLogSize, xmlFileName )
> > @logfile = File.new(xmlFileName, "w")
> > @xml = XmlMarkup.new(:target=>@logfile, :indent=>2)
> > @xml.instruct! #insert processing instruction
> > @results = XmlMarkup.new
> > end
> >
> > def log_results(name, input, expected, test_status)
> > r = XmlMarkup.new(:target=>@results, :indent=>2)
> > r.result(:testcase => name) do
> > r.status(test_status)
> > end #results tag
> > end #log_results
> >
> > def end_log
> > @xml.results do
> > @results
> > end
> > @logfile.close
> > end #end_log
>
> The following should work for you:
>
> class TestResultsLogger
> def initialize(fileName, logsToKeep, maxLogSize, xmlFileName )
> @logfile = File.new(xmlFileName, "w")
> @xml = Builder::XmlMarkup.new(:target=>@logfile, :indent=>2)
> @xml.instruct! #insert processing instruction
> @results =
> Builder::XmlMarkup.new(:target=>@results, :indent=>2, :margin=>1)
> end
>
> def log_results(name, input, expected, test_status)
> @results.result(:testcase => name) do
> @results.status(test_status)
> end
> end
>
> def end_log
> @xml.results do
> @xml << @results.target!
> end
> @logfile.close
> end
> end
>

Perfect! Thank you very much. I was about to e-mail you first, but
figured that others might benefit from the solution ;-)

Drew