# Xml Processing in IronRuby - Help me improve the wrapper

**URL:** <https://www.ruby-forum.com/t/xml-processing-in-ironruby-help-me-improve-the-wrapper/156714>\
**Category:** IronRuby\
**Created:** [January 1, 2009, 6:49pm UTC](https://www.ruby-forum.com/t/xml-processing-in-ironruby-help-me-improve-the-wrapper/156714 "2009-01-01T18:49:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![benhiller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@benhiller](https://www.ruby-forum.com/u/benhiller)\
**Post date:** [January 1, 2009, 6:49pm UTC](https://www.ruby-forum.com/t/xml-processing-in-ironruby-help-me-improve-the-wrapper/156714/1 "2009-01-01T18:49:03Z")

</div>

Hello,

I’ve just been playing around with IronRuby and processing some XML  
from a REST service. I’ve created this wrapper:  
require ‘mscorlib’  
require ‘System, Version=2.0.0.0, Culture=neutral,  
PublicKeyToken=b77a5c561934e089’  
include System  
require ‘System.Xml, Version=2.0.0.0, Culture=neutral,  
PublicKeyToken=b77a5c561934e089’  
include System::Xml

#Wrapper around .Net XMLDocument to make it easier to query with -  
more like REXML  
class Document  
def initialize(xml)  
@document = XmlDocument.new  
@document.load\_xml xml  
end

def get(xpath) #.collect - I want to pass the block in, not have  
to use it on the outside.  
@document.SelectNodes(xpath)  
end  
end

class XmlNodeList  
def get(name)  
list.select {|e| e.Name == name}  
end  
end

class XmlElement  
def get(name)  
if has\_attribute name  
get\_attribute name  
else  
get\_elements\_by\_tag\_name name  
end  
end

def text  
inner\_text  
end  
end

My first problem is that I’m getting XmlElement object instead of  
XmlNode from an XmlNodeList. If I had XmlNode, then I could do  
e[‘n’].text - which is what I want. As a result, my code to access  
the N element looks like this:

require ‘xml’  
@document = Document.new(‘testaaa’)  
@document.get(‘x’).collect {|n| puts n.text}

> > testaaa

> > > @document.get(‘x/x1’).collect {|e| e.get(‘n’).collect {|n| puts n.text}}  
> > > test

Why am I getting a XmlElement object? How can I access my xml in a  
cleaner fashion?

My second problem, I wanted to be able to pass in a block

def get(xpath)  
@document.SelectNodes(xpath).collect {yield}  
end

In this case, I get the following error:

> > > @document.get(‘x’) {|n| puts n.text}  
> > > :1: undefined method `text' for nil:NilClass (NoMethodError) from xml.rb:27:in`get’

I changed it to {|n| yield}, but I get the same error. The object is  
always nil. What am I doing wrong?

Hope this all makes sense.

Thanks

Ben

---

<div class="post-metadata">

**Author:** ![benhiller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@benhiller](https://www.ruby-forum.com/u/benhiller)\
**Post date:** [January 1, 2009, 7:02pm UTC](https://www.ruby-forum.com/t/xml-processing-in-ironruby-help-me-improve-the-wrapper/156714/2 "2009-01-01T19:02:34Z")

</div>

I wrote something similar a while ago

[http://github.com/casualjim/ironnails/tree/master/IronNails/vendor/iron\_nails/lib/iron\_xml.rb](http://github.com/casualjim/ironnails/tree/master/IronNails/vendor/iron_nails/lib/iron_xml.rb)

statuses = []  
IronXml.parse(‘friends\_timeline.xml’, :document) do |doc|  
doc.elements(‘statuses’) do |status|  
st = Status.new  
st.id = status.element(‘id’).value  
st.created\_at = status.element(‘created\_at’).value  
st.text = status.element(‘text’).value  
st.source = status.element(‘source’).value  
st.truncated = status.element(‘truncated’).value  
st.in\_reply\_to\_status\_id =  
status.element(‘in\_reply\_to\_status\_id’).value  
st.in\_reply\_to\_user\_id = status.element(‘in\_reply\_to\_user\_id’).value  
st.favorited = status.element(‘favorited’).value  
status.elements(‘user’) do |user|  
# process user here  
end

```
statuses << st

```

end  
end

or

statuses = []  
IronXml.parse(‘friends\_timeline.xml’, :document) do |doc|  
doc.statuses do |status|  
st = Status.new  
st.id = status.id  
st.created\_at = status.created\_at  
st.text = status.text  
st.source = status.source  
st.truncated = status.truncated  
st.in\_reply\_to\_status\_id = status.in\_reply\_to\_status\_id  
st.in\_reply\_to\_user\_id = status.in\_reply\_to\_user\_id  
st.favorited = status.favorited  
status.user do |user|  
# process user here  
end

```
statuses << st

```

end  
end

You can maybe look there for inspiration 🙂

---

<div class="post-metadata">

**Author:** ![benhiller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@benhiller](https://www.ruby-forum.com/u/benhiller)\
**Post date:** [January 1, 2009, 7:42pm UTC](https://www.ruby-forum.com/t/xml-processing-in-ironruby-help-me-improve-the-wrapper/156714/3 "2009-01-01T19:42:02Z")

</div>

Excellent!! Thank you. I was missing select\_single\_node and &b 😉

Wrapper is now sorted, and I can do:  
require ‘xml’  
@document =  
Document.new(‘testabctestertesteraaa’)

@document.elements(‘x/x1’) do |e|  
puts e.get(‘z’)  
e.node(‘y’) {|y| puts y.get(‘q’)}  
end

Thanks  
Ben
