0

I've used the utility xsd2ruby utility provided with soap4r to generate the required classes from an XSD schema definition file. This works well, however when I try to generate an xml file using XSD::Mapping.obj2xml the attributes do not get created as I expect (or would like). I would like the following:

  <obj attr1=value1 attr2=value2>
    <element1>value</element1>
  </obj>

but this is what gets generated:

 <obj>
    <__xmlattr>
      <item>
        <key>
          <name>attr1</name>
          <namespace></namespace>
          <source></source>
        </key>
        <value>value1</value>
      </item>
      <item>
        <key>
          <name>attr2</name>
          <namespace></namespace>
          <source></source>
        </key>
        <value>value2</value>
      </item>
    </__xmlattr>
    <element1>value</element1>
  </obj>

How can I generate the XML output without the xmlattr?

1 Answers1

0

From what I remember XSD::Mapping.obj2xml XML will use an instance's instance variables as element names and their values as child elements. That's all. With the instance variable @__xmlattr getting special processing, as you can see.

To get around these limitations I wrote jaxb2ruby. It generates classes based on an ERB template. You can use one of the builtin templates (ROXML, HappyMapper, or plain ruby class) or write your own.

It's not perfect but has worked out well for me in several instances.

You may also want to checkout ROAR.

sshaw
  • 994
  • 6
  • 10