简体   繁体   中英

How can I generate attributes with obj2xml for ruby classes generated from xsd2ruby?

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM