开发者

How do I use XML Builder to create xml in rails?

I am trying the following, but the output XML is badly formed:

 xml = Builder::XmlMarkup.new({:target => display })

xml.instruct!

xml.mail {  
  xml.documents {
    xml.document {
      xml.template {
        xml.source "gallery"
        xml.name "Postcard: Image fill front"
      }
      xml.sections {
        xml.section {
          xml.name "Text"
          xml.text "Hello, world"
        }

        xml.section {
           xml.name "Image"
           xml.attachment "1136946686-3425"
        }
      } #sections
    } #document         
  } #documents

  xml.addresses {
    xml.addressee {
      xml.name "Me"
      xml.address "Street"
      xml.city "San Francisco"
      xml.state "CA"
    }
  }  
}    
@xml_display = xml

I need it to look more like this:

<?xml version="1.0" encoding="UTF-8"?>
<mail>
  <documents>
    <document>
      <template>
        <source>gallery</source>
  开发者_JS百科      <name>Postcard: Image fill front</name>
      </template>
      <sections>
         <section>
           <name>Text</name>
           <text>Hello, World!</text>
          </section>
       <section>
          <name>Image</name>
          <attachment>...attachment id...</attachment>
          </section>
       </sections>
    </document>
  </documents>
  <addressees>
    <addressee>
      <name>John Doe</name>
      <address>123 Main St</address>
      <city>Anytown</city>
      <state>AZ</state>
      <postal-code>10000</postal-code>
    </addressee>
  </addressees>
</mail>

My code ends up looking like this in the view (page source):

<?xml version="1.0" encoding="UTF-8"?><mail/><documents/><document/><template/><source>gallery</source><name>Postcard:  Image fill front</name>

NOTE: The final XML needs to be POSTED to a URL, and I am using rest-client and so want everything to become an instance variable that is passed as the body.

The instance variable is just @xml_display = xml but it's not working... :(


You can pass a block to each of those to nest the items.

xml.mail do |mail|
  mail.documents do |documents|
    documents.document do |document|
      document.template do |template|
        template.source "gallery"
        template.name "Postcard:  Image fill front"
      end
    end
  end
end

There's more information on the builder rubyforge page.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜