Ruby 1.9.3:为URL编码XML

时间:2012-02-17 12:25:49

标签: ruby

我做了

require "builder"
require "uri"

caption_preview_url = "http://api.cheezburger.com/xml/captions/preview"
encoded_xml = URI.escape(caption_xml.to_s)

xml = Builder::XmlMarkup.new( :indent => 2 )
xml.instruct! :xml, :encoding => "ASCII"
xml.CaptionPreview do |element|
      element.CaptionedImageUrl = "http://api.cheezburger.com/xml/caption/" + encoded_xml
end
post_xml(caption_preview_url,xml) #posts to the URL with a header

我从服务器收到了非声明失败:

     意外的错误      已将此错误的详细信息记录到服务器。错误事件Id:be4bf0d8-f230-4301-9d88-eae35bcc9321      be4bf0d8-f230-4301-9d88-eae35bcc9321

我的第一个猜测是它是我的编码,但不确定我会做错什么

修改

caption xml是这个的输出:

  def build_caption_xml(lol_caption_data) #lol_caption_data is an object I pass in where I have set attrs for the xml (e.g. FontType, Text etc).
    xml = Builder::XmlMarkup.new( :indent => 2 )
    xml.instruct! :xml, :encoding => "utf-8"
    xml.Caption do |element|
      element.Text            lol_caption_data.text
      element.FontFamily      lol_caption_data.font_family
      element.FontSize        lol_caption_data.font_size
      element.FontColor       lol_caption_data.font_color
      element.XPosition       lol_caption_data.x_position
      element.YPosition       lol_caption_data.y_position
      element.IsBold          lol_caption_data.is_bold
      element.TextStyle       lol_caption_data.text_style
      element.IsItalic        lol_caption_data.is_italic
      element.IsStrikeThrough lol_caption_data.is_strikethrough
      element.IsUnderline     lol_caption_data.is_underline
      element.Opacity         lol_caption_data.font_opacity
    end
    return xml
  end

进一步修改:

XML输出:

<?xml version="1.0" encoding="utf-8"?>
<Caption>
  <Text>Meow?</Text>
  <FontFamily>Impact</FontFamily>
  <FontSize>25</FontSize>
  <FontColor>White</FontColor>
  <XPosition>10</XPosition>
  <YPosition>10</YPosition>
  <IsBold>false</IsBold>
  <TextStyle>outline</TextStyle>
  <IsItalic>false</IsItalic>
  <IsStrikeThrough>false</IsStrikeThrough>
  <IsUnderline>false</IsUnderline>
  <Opacity>100</Opacity>
</Caption>

编码输出:

%3C?XML%20version =%221.0%22%20encoding =%22utf-8%22?%3E%0A%3CCaption%3E%0A%20%20%3CText%3EMeow?%3C /文字%3E% 0A%20%20%3CFontFamily%3EImpact%3C / FontFamily%3E%0A%20%20%3CFontSize%3E25%3C / FontSize%3E%0A%20%20%3CFontColor%3EWhite%3C / FontColor%3E%0A% 20%20%3CXPosition%3E10%3C / XPosition%3E%0A%20%20%3CYPosition%3E10%3C / YPosition%3E%0A%20%20%3CIsBold%3Efalse%3C / IsBold%3E%0A%20% 20%3CTextStyle%3Eoutline%3C / TextStyle%3E%0A%20%20%3CIsItalic%3Efalse%3C / IsItalic%3E%0A%20%20%3CIsStrikeThrough%3 Efalse%3C / IsStrikeTh%3E%0A%20%20 %3CIsUnderline%3Efalse%3C / IsUnderline%3E%0A%20%20%3COpacity%3E100%3C /不透明度%3E%0A%3C /字幕%3E%0A

0 个答案:

没有答案