如何阻止rails验证xml?

时间:2010-04-20 00:51:29

标签: ruby-on-rails xml xml-validation

我正在向rails webservice提交以下消息:

xmlPostData = "<message>
                   <message-text>" + MESSAGE_WITH_XML  + "</message-text>
                   <name>" + subject + "</name>
                   <f1>" + toPhone + "</f1>
                   <f2>" + fromPhone + "</f2>
               </message>";

问题是包含带有XML数据的文本的字段是一种解决方法,但我需要能够将该xml提交到数据库并从那里获取它。

我可以阻止rails验证并以json格式替换我的xml吗? 这就是它的样子:

    --- !map:HashWithIndifferentAccess 
smil: !map:HashWithIndifferentAccess 
  head: !map:HashWithIndifferentAccess 
    layout: !map:HashWithIndifferentAccess 
      root_layout: !map:HashWithIndifferentAccess 
        height: &quot;600&quot;
        background_color: white
        width: &quot;800&quot;
      type: text/smil-basic-layout
  body: !map:HashWithIndifferentAccess 
    par: !map:HashWithIndifferentAccess 
      text: !map:HashWithIndifferentAccess 
        left: &quot;33&quot;
        begin: &quot;33&quot;
        dur: &quot;33&quot;
        val: 34343434343434343aaaaaaa
        height: &quot;33&quot;
        width: &quot;33&quot;
        top: &quot;33&quot;

这是来自rails webservice的ruby方法:

# POST /messages
  # POST /messages.xml
  def create
    @message = Message.new(params[:message])

    respond_to do |format|
      if @message.save
        flash[:notice] = 'Message was successfully created.'
        format.html { redirect_to(@message) }
        format.xml  { render :xml => @message, :status => :created, :location => @message }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @message.errors, :status => :unprocessable_entity }
      end
    end
  end

是一种解决方法,但目前这必须起作用......

1 个答案:

答案 0 :(得分:1)

如果您只需要嵌入任意文本,则应使用CDATA。只需确保字符串]]>没有出现在MESSAGE_WITH_XML中。

xmlPostData = "<message>
                   <message-text><![CDATA[" + MESSAGE_WITH_XML  + "]]></message-text>
                   <name>" + subject + "</name>
                   <f1>" + toPhone + "</f1>
                   <f2>" + fromPhone + "</f2>
               </message>";