I’m using xml-mapping within my ruby on rails app.
I need to load xml files and parse those to objects using xml-mapping
xml example is here
<?xml version="1.0" encoding="UTF-8"?>
<elements type="array">
<element></element>
<element></element>
...
</elements>
and here is the ruby code
require 'xml/mapping'
class Macro; end
class Elements
include XML::Mapping
array_node :elements, "elements","element" :class => Element
end
class Element
include XML::Mapping
text_node :name, "name"
text_node :description, "description"
end
The problem is when I use Elements.load_from_file("my.xml") it doesn’t load array, but if I add root node to xml it works.
this xml works
<?xml version="1.0" encoding="UTF-8"?>
<mynode>
<elements type="array">
<element></element>
<element></element>
...
</elements>
<mynode>
Does anybody know how to fix this?
(I’m the xml-mapping author)
The XPath expressions are always relative to the mapped class’s base node in the XML tree, which is the “elements” element for the “Elements” class. So you mustn’t include that name in XPath expressions in Elements’s nodes. Just leave it out and the code should work: