[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Wrapping XML document in Class

Aaron Becker

3/13/2006 7:22:00 PM

I would like to use Ruby and REXML to wrap an XML document and allow
access to the element nodes, attributes, and text. Essentially I need
an XML document builder loosely coupled from the document.

It seems like Ruby is powerful enough to code generate a class given an
XML document or build an object at run-time with the appropriate
accessors.

For instance:

order = XmlBuilder.new

order.add("root").add("header")
order.root.add("body")

order.body.add("component").attributes["type"] = "metric"

order.write($stdout)

I'd also like to be able to plug-in input validation when assigning
values to elements and attributes.

Has anyone done this before?

Thanks.

--
Posted via http://www.ruby-....


6 Answers

Aaron Becker

3/13/2006 8:36:00 PM

0

Miles Monroe wrote:
> I would like to use Ruby and REXML to wrap an XML document and allow
> access to the element nodes, attributes, and text. Essentially I need
> an XML document builder loosely coupled from the document.
>
> It seems like Ruby is powerful enough to code generate a class given an
> XML document or build an object at run-time with the appropriate
> accessors.
>
> For instance:
>
> order = XmlBuilder.new
>
> order.add("root").add("header")
> order.root.add("body")
>
> order.body.add("component").attributes["type"] = "metric"
>
> order.write($stdout)
>
> I'd also like to be able to plug-in input validation when assigning
> values to elements and attributes.
>
> Has anyone done this before?
>
> Thanks.

I found this blog entry on a XmlStringBuffer class in Ruby. I
understand using method_missing to create the element, but I'm not sure
how to closures would be implemented. Thanks.

http://www.beust.com/weblog/archives/0...

xml = XML.new

xml.html {
xml.head {
}
xml.body {
xml.table {
xml.tr {
xml.td({ "valign" => "top"}, "Content1"){
}
xml.td {
xml.append("Content2")
}
}
}
}
}


--
Posted via http://www.ruby-....


Jim Weirich

3/13/2006 8:53:00 PM

0

Miles Monroe wrote:
> I would like to use Ruby and REXML to wrap an XML document and allow
> access to the element nodes, attributes, and text. Essentially I need
> an XML document builder loosely coupled from the document.
>
> It seems like Ruby is powerful enough to code generate a class given an
> XML document or build an object at run-time with the appropriate
> accessors.
>
> For instance:
>
> order = XmlBuilder.new
[... example elided ...]

Are you wanting to build a DOM object, or build a string containing XML
markup? If the later, Builder::XmlMarkup will do this. If the former,
it would be interesting to create a Builder with a similar API to
Buidler::XmlMarkup, but generated DOM trees instead of strings.

Or perhaps you are looking for something completely different.

XML Builder Link: http://builder.ruby...

--
-- Jim Weirich

--
Posted via http://www.ruby-....


Simon Kröger

3/13/2006 9:16:00 PM

0

Hi Miles,

interesting idea, i did a quick (proof of concept) hack:
-------------------------------------------------------------
class Xmltree
attr_accessor :childs
attr_accessor :content
attr_accessor :attributes

def initialize
self.childs = []
self.attributes = {};
end

def method_missing meth, *args, &block
return attributes[args[0]] = args[1] if meth == :[]=
return attributes[args[0]] if meth == :[]
return self.content = args if meth == '='.intern

unless child = childs.assoc(meth.to_s.chomp('='))
childs << child = [meth.to_s.chomp('='), Xmltree.new]
end

return child[1] if meth.to_s[-1] != ?=
child[1].content = args
end

def to_s
childs.map do |t, c|
"""<#{t} #{c.attributes.map do |a, v|
"#{a}=\"#{v}\""
end.join(' ')}>\n#{c}</#{t}>\n"""
end.join + content.to_s + (content ? "\n" : '')
end
end
-------------------------------------------------------------

usage:
-------------------------------------------------------------
xml = Xmltree.new

xml.header.title = 'this is a demo'
xml.body.component['type'] = 'the answer'
xml.body.component = 42

puts xml
-------------------------------------------------------------

output:
-------------------------------------------------------------
<header >
<title >
this is a demo
</title>
</header>
<body >
<component type="the answer">
42
</component>
</body>
-------------------------------------------------------------

cheers

Simon

Miles Monroe:
> I would like to use Ruby and REXML to wrap an XML document and allow
> access to the element nodes, attributes, and text. Essentially I need
> an XML document builder loosely coupled from the document.
>
> It seems like Ruby is powerful enough to code generate a class given an
> XML document or build an object at run-time with the appropriate
> accessors.
>
> For instance:
>
> order = XmlBuilder.new
>
> order.add("root").add("header")
> order.root.add("body")
>
> order.body.add("component").attributes["type"] = "metric"
>
> order.write($stdout)
>
> I'd also like to be able to plug-in input validation when assigning
> values to elements and attributes.
>
> Has anyone done this before?
>
> Thanks.
>



Aaron Becker

3/13/2006 9:44:00 PM

0

Simon Kröger wrote:
> Hi Miles,
>
> interesting idea, i did a quick (proof of concept) hack:
> -------------------------------------------------------------
> class Xmltree
> attr_accessor :childs
> attr_accessor :content
> attr_accessor :attributes
>
> def initialize
> self.childs = []
> self.attributes = {};
> end
>
> def method_missing meth, *args, &block
> return attributes[args[0]] = args[1] if meth == :[]=
> return attributes[args[0]] if meth == :[]
> return self.content = args if meth == '='.intern
>
> unless child = childs.assoc(meth.to_s.chomp('='))
> childs << child = [meth.to_s.chomp('='), Xmltree.new]
> end
>
> return child[1] if meth.to_s[-1] != ?=
> child[1].content = args
> end
>
> def to_s
> childs.map do |t, c|
> """<#{t} #{c.attributes.map do |a, v|
> "#{a}=\"#{v}\""
> end.join(' ')}>\n#{c}</#{t}>\n"""
> end.join + content.to_s + (content ? "\n" : '')
> end
> end
> -------------------------------------------------------------
>
> usage:
> -------------------------------------------------------------
> xml = Xmltree.new
>
> xml.header.title = 'this is a demo'
> xml.body.component['type'] = 'the answer'
> xml.body.component = 42
>
> puts xml
> -------------------------------------------------------------
>
> output:
> -------------------------------------------------------------
> <header >
> <title >
> this is a demo
> </title>
> </header>
> <body >
> <component type="the answer">
> 42
> </component>
> </body>
> -------------------------------------------------------------
>
> cheers
>
> Simon
>
> Miles Monroe:


Awesome! That is better than I ever imagined.

Back in my Perl days, I started creating a source filter to allow the
insertion of non-Perl XPath statements that worked on an implicit XML
document.

I used the source filter to first scan the code for naked XPath
statements and then built the XML document with the slightly-modified
XPath syntax. I converted the invalid non-Perl strings into valid Perl
in the source filter.

Likewise, is it possible to insert non-Ruby text in the main namespace
and then turn it into Ruby code before the rest of the script is parsed?
I guess I'm asking if it's possible to implement a source filter in
Ruby.

Thanks.

--
Posted via http://www.ruby-....


Aaron Becker

3/13/2006 10:04:00 PM

0


http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-t...

http://www.rubyquiz.com/q...

I found a couple links on source filters for Ruby.

I guess what I was proposing was just something like this:


/html/head/title << "Implicit XML Document"

/html/body/h1 << "Ruby source filters"

/html/body/h1["class"] = "golden"

/html/body/table["id"] = "QueryResults"

puts $_implicit_XML


--
Posted via http://www.ruby-....


benjohn

3/13/2006 11:10:00 PM

0

Last time I was doing playing with XML, I wanted to be able to,
basically, add methods on to the nodes matching xpaths. It didn't
seem to be very easy. I wondered at the time if some kind of
transform idea would work better.