Combining XmlMarkup objects?

A

Andrew Cox

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).=20
The log_results method will be called from another location. What I
want the final result to look like is something like:

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

<result testcase=3D"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.

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

def log_results(name, input, expected, test_status)
r =3D XmlMarkup.new:)target=3D>@results, :indent=3D>2)
r.result:)testcase =3D> 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
=3D=3D

Drew Cox
 
J

Jim Weirich

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
 
A

Andrew Cox

=20
The following should work for you:
=20
class TestResultsLogger
def initialize(fileName, logsToKeep, maxLogSize, xmlFileName )
@logfile =3D File.new(xmlFileName, "w")
@xml =3D Builder::XmlMarkup.new:)target=3D>@logfile, :indent=3D>2)
@xml.instruct! #insert processing instruction
@results =3D
Builder::XmlMarkup.new:)target=3D>@results, :indent=3D>2, :margin=3D>1)
end
=20
def log_results(name, input, expected, test_status)
@results.result:)testcase =3D> name) do
@results.status(test_status)
end
end
=20
def end_log
@xml.results do
@xml << @results.target!
end
@logfile.close
end
end
=20

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

Drew
 

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,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top