0

我正在向 Rails 网络服务提交以下消息:

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

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

4

1 回答 1

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>";
于 2010-04-20T00:54:26.033 回答