开发者

How can I stop rails validating xml?

开发者 https://www.devze.com 2022-12-27 23:47 出处:网络
I\'m submitting to a rails webservice the following message: xmlPostData = \"<message> <message-text>\" + MESSAGE_WITH_XML+ \"</message-text>

I'm submitting to a rails webservice the following message:

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

The problem is the the field with contain a text with XML data, is a workaround but I need to be able to submit that xml to the db and get it from there.

Can I stop rails validating and replacing my xml in json format? this is how it looks:

    --- !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;

And this is the ruby method from the rails webservice:

# POST /messages
  # POST /messages.xml
  def create
    @message = Message.new(p开发者_运维问答arams[: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

Is a workaround but for the moment this has to work ...


If you just need embed arbitrary text, you should use CDATA. Just make sure the string ]]> doesn't appear in the MESSAGE_WITH_XML.

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

精彩评论

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

关注公众号