我可以将文件大小添加到 Builder XML 输出吗

发布于 2024-09-25 07:01:14 字数 581 浏览 6 评论 0原文

我正在开发一个 Ruby on Rails 应用程序,该应用程序使用构建器模板生成大型 XML 文档,但我遇到了一些障碍。

XML 输出必须有一个包含文件大小(以字节为单位)的字段。我认为我基本上需要使用填充 http 响应中“Content-Length”标头的值,但更新标记的值显然会改变文件大小。

输出应如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<dataset>
  <metadata>
    <filesize>FILESIZE</filesize>
    <filename>FILENAME.xml</filename>
  </metadata>
    <data>
    .
    .
    .
    </data>
</dataset>

IsAdding the file size in an XML tag possible using a builder template?如果没有,我可以使用某种方法来达到所需的结果吗?

I'm working on a ruby on rails app that generates a large XML document using a builder template, but I've hit a bit of a stumbling point.

The XML output must have a field that contains the file size in bytes. I think that I'll basically need to use the value that would populate the "Content-Length" header in the http response, but updating the value of the tag will obviously change the file size.

The output should looks something like this:

<?xml version="1.0" encoding="UTF-8"?>
<dataset>
  <metadata>
    <filesize>FILESIZE</filesize>
    <filename>FILENAME.xml</filename>
  </metadata>
    <data>
    .
    .
    .
    </data>
</dataset>

Is adding the file size in an XML tag possible using a builder template? If not, is there some method that I could use to achieve the required result?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伪心 2024-10-02 07:01:14

感谢 Garrett,我能够想出以下(丑陋的)解决方案,它确实需要改进,但它确实有效:

class XmlMetaInjector
  require 'nokogiri'

  def initialize(app)  
    @app = app  
  end  

  def call(env)  
    status, headers, response = @app.call(env)  
    if headers['Content-Type'].include? 'application/xml'
      content_length = headers['Content-Length'].to_i # find the original content length

      doc = Nokogiri::XML(response.body)
      doc.xpath('/xmlns:path/xmlns:to/xmlns:node', 'xmlns' => 'http://namespace.com/').each do |node|
         # ugly method to determine content_length; if this happens more than once we're in trouble
        content_length = content_length + (content_length.to_s.length - node.content.length)
        node.content = content_length
      end

      # update the header to reflect the new content length
      headers['Content-Length'] = content_length.to_s

      [status, headers, doc.to_xml]  
    else  
      [status, headers, response]  
    end 
  end # call(env)
end

Thanks to Garrett I was able to come up with the following (ugly) solution, it definitely needs improvement, but it does work:

class XmlMetaInjector
  require 'nokogiri'

  def initialize(app)  
    @app = app  
  end  

  def call(env)  
    status, headers, response = @app.call(env)  
    if headers['Content-Type'].include? 'application/xml'
      content_length = headers['Content-Length'].to_i # find the original content length

      doc = Nokogiri::XML(response.body)
      doc.xpath('/xmlns:path/xmlns:to/xmlns:node', 'xmlns' => 'http://namespace.com/').each do |node|
         # ugly method to determine content_length; if this happens more than once we're in trouble
        content_length = content_length + (content_length.to_s.length - node.content.length)
        node.content = content_length
      end

      # update the header to reflect the new content length
      headers['Content-Length'] = content_length.to_s

      [status, headers, doc.to_xml]  
    else  
      [status, headers, response]  
    end 
  end # call(env)
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文