# XmlStruct-1.0.0 released

**URL:** <https://www.ruby-forum.com/t/xmlstruct-1-0-0-released/55673>\
**Category:** Ruby\
**Created:** [March 22, 2006, 9:54pm UTC](https://www.ruby-forum.com/t/xmlstruct-1-0-0-released/55673 "2006-03-22T21:54:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![neilw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e36b37/32.png) [@neilw](https://www.ruby-forum.com/u/neilw)\
**Post date:** [March 22, 2006, 9:54pm UTC](https://www.ruby-forum.com/t/xmlstruct-1-0-0-released/55673/1 "2006-03-22T21:54:03Z")

</div>

# XmlStruct

[http://rubyforge.org/projects/xmlstruct/](http://rubyforge.org/projects/xmlstruct/)

# Documentation

[http://xmlstruct.rubyforge.org/](http://xmlstruct.rubyforge.org/)

# About

XML Markup the simple way.

I’ve uploaded a small library to Rubyforge that allows you  
to markup data using XML tags using direct Ruby assignment  
following the example of OpenStruct.

The library supports attributes and simple text elements -  
all of which are XML escaped before output. The interface is  
designed to be as simple as possible, with the least amount  
of syntactic sugar. The general idea is that you mark up your  
flat data and output a string containing the markup.

The system works best when you have a small amount of data,  
and a large amount of irritating XML structure to impose upon  
it. The class came about to allow me to quickly draw up XML  
messages to send to the UK Government Gateway. Boy can you  
tell that that schema was designed by committee.

I hope that it finds some use somewhere.

# Usage

I hope the interface is intuitive. Very simply you use assignment  
commands, e.g

```
require 'rubygems'
require 'xml_struct'
@xmldoc = XmlStruct.new
@xmldoc.GovTalkMessage={:xmlns =>

```

“[http://www.govtalk.gov.uk/CM/envelope](http://www.govtalk.gov.uk/CM/envelope)”}  
@xmldoc.GovTalkMessage.EnvelopeVersion=“2.0”  
md=@xmldoc.GovTalkMessage.Header.MessageDetails  
md.Class=“MAFF-IACS-AAPS2001”  
md.Qualifier=“request”  
md.Function=“submit”  
md.CorrelationID  
md.Transformation=“XML”  
md.GatewayTimeStamp  
@xmldoc.GovTalkDetails.Keys.Key[0]={:Type =\> “VendorNumber”}  
@xmldoc.GovTalkDetails.Keys.Key[0]=275687  
@xmldoc.GovTalkDetails.Keys.Key[1]={:Type =\> “MainCPH”}  
@xmldoc.GovTalkDetails.Keys.Key[1]=“14/02/0327”  
@xmldoc.GovTalkDetails.Body  
print @xmldoc.to\_s

outputs

```
<GovTalkMessage xmlns="http://www.govtalk.gov.uk/CM/envelope">
<EnvelopeVersion>2.0</EnvelopeVersion>
<Header>
<MessageDetails>
<Class>MAFF-IACS-AAPS2001</Class>
<Qualifier>request</Qualifier>
<Function>submit</Function>
<CorrelationID>
</CorrelationID>
<Transformation>XML</Transformation>
<GatewayTimeStamp>
</GatewayTimeStamp>
</MessageDetails>
</Header>
</GovTalkMessage>
<GovTalkDetails>
<Keys>
<Key Type="VendorNumber">275687</Key>
<Key Type="MainCPH">14/02/0327</Key>
</Keys>
<Body>
</Body>
</GovTalkDetails>

```

# Enjoy

Neil W., Aldur Systems Ltd

---

<div class="post-metadata">

**Author:** ![neilw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e36b37/32.png) [@neilw](https://www.ruby-forum.com/u/neilw)\
**Post date:** [March 22, 2006, 10:08pm UTC](https://www.ruby-forum.com/t/xmlstruct-1-0-0-released/55673/2 "2006-03-22T22:08:54Z")

</div>

Have you seen the xml builder project? It’s along the same lines but  
gives a  
much cleaner syntax. Your example could be done along these lines:

xml.GovTalkMessage :xmlns =\> “http …” { |xml|  
xml.EnvelopeVersion “2.0”  
xml.Header {  
xml.MessageDetails {  
etc, etc  
}  
}  
}

Check it out [http://rubyforge.org/projects/builder/](http://rubyforge.org/projects/builder/)

---

<div class="post-metadata">

**Author:** ![neilw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e36b37/32.png) [@neilw](https://www.ruby-forum.com/u/neilw)\
**Post date:** [March 23, 2006, 1:45am UTC](https://www.ruby-forum.com/t/xmlstruct-1-0-0-released/55673/3 "2006-03-23T01:45:15Z")

</div>

Justin B. wrote:

> }
> 
> Check it out [http://rubyforge.org/projects/builder/](http://rubyforge.org/projects/builder/)

I don’t like the reuse of xml inside of each of the blocks. IIRC it is  
why I dislike the CGI library. Visually it looks like  
everything gets appended right to xml, although thinking twice I know  
that the block scope is actually where the xml addition  
takes place.

xml.MyTag :xmlns+.“…” { |xml|  
xml.ATag “2.0”  
xml.BTag {  
xml.CTag {  
xml.DTAG { …etc… }  
}  
}  
}

The above makes me think I am getting:

My brain thinks this at first because of the reuse of “xml”. Now  
thinking twice I realize that the scope takes precedence, and my  
brain conforms.

It seems like the following would be more approriate, although it would  
be more typing:

xml.MyTag :xmlns+.“…” { |xml|  
xml.ATag “2.0”  
xml.BTag { |btag|  
btag.CTag { |ctag|  
ctag.DTAG { |dtag| …etc… }  
}  
}  
}

This way I can easily visually tell that what the hiearchy looks like in  
my xml document w/o having to count tabs or curly braces.

Zach

---

<div class="post-metadata">

**Author:** ![neilw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e36b37/32.png) [@neilw](https://www.ruby-forum.com/u/neilw)\
**Post date:** [March 23, 2006, 3:08am UTC](https://www.ruby-forum.com/t/xmlstruct-1-0-0-released/55673/4 "2006-03-23T03:08:56Z")

</div>

I’ll add that there’s a partial E4X implementation on RubyForge that  
could use some TLC too.

T.

---

<div class="post-metadata">

**Author:** ![neilw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e36b37/32.png) [@neilw](https://www.ruby-forum.com/u/neilw)\
**Post date:** [March 23, 2006, 6:13am UTC](https://www.ruby-forum.com/t/xmlstruct-1-0-0-released/55673/5 "2006-03-23T06:13:22Z")

</div>

zdennis wrote:

> It seems like the following would be more approriate, although it would  
> be more typing:
> 
> xml.MyTag :xmlns+."…" { |xml|  
> xml.ATag “2.0”  
> xml.BTag { |btag|  
> btag.CTag { |ctag|  
> ctag.DTAG { |dtag| …etc… }  
> }  
> }  
> }
> 
> This way I can easily visually tell that what the hiearchy looks like in  
> my xml document w/o having to count tabs or curly braces.

If you really want to, that style works fine with XML builder too.

require ‘builder/xmlmarkup’

xml = Builder::XmlMarkup.new(:indent=\>2)

xml.MyTag { |xml|  
xml.ATag “2.0”  
xml.BTag { |btag|  
btag.CTag { |ctag|  
ctag.DTAG { |dtag| }  
}  
}  
}  
puts xml.target!

Produces:

 2.0 

–  
– Jim W.

---

<div class="post-metadata">

**Author:** ![neilw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e36b37/32.png) [@neilw](https://www.ruby-forum.com/u/neilw)\
**Post date:** [March 23, 2006, 9:09am UTC](https://www.ruby-forum.com/t/xmlstruct-1-0-0-released/55673/6 "2006-03-23T09:09:31Z")

</div>

One man’s meat is another man’s poison.

I don’t like the nested braces syntax and my system is not interested  
in the XML layout. The straight assignment just seems simpler to me,  
and I find it useful when I’m doing assignments throughout a program  
rather than in just one place.

Builder is much more powerful. I didn’t need the power, but I needed  
the simplicity. Hence why I built XmlStruct. (That and learning a bit  
about reflection and meta-programming).

NeilW
