如何在没有“”的情况下生成XML ?xml version =…”?(复制)

时间:2021-04-07 19:19:07

Possible Duplicate:
Print an XML document without the XML header line at the top

可能的重复:打印一个XML文档,顶部没有XML标题行

I have a problem with Nokogiri::XML::Builder. I am generating XML wth this code:

我对Nokogiri::XML: Builder有一个问题。我正在生成XML wth这段代码:

builder = Nokogiri::XML::Builder.new do 
    request {
        data '1'
    }
end

And the result is:

结果是:

<?xml version="1.0" encoding="UTF-8"?><request><data>1</data></request>

How can I remove:

我如何删除:

<?xml version="1.0" encoding="UTF-8"?>

from my XML?

从我的XML ?

2 个解决方案

#1


25  

Maybe take just the root node of the current Document object being built – .doc – instead of the whole document?

也许只取正在构建的当前文档对象的根节点—.doc—而不是整个文档?

builder.doc.root.to_s

#2


5  

A quick and dirty answer is to tell Nokogiri to reparse the resulting output, then look at the root:

一个快速而肮脏的答案是告诉Nokogiri重新解析结果输出,然后查看根:

require 'nokogiri'

builder = Nokogiri::XML::Builder.new do 
  request {
    data '1'
  }
end

puts Nokogiri::XML(builder.to_xml).root.to_xml

Which outputs:

输出:

<request>
  <data>1</data>
</request>

#1


25  

Maybe take just the root node of the current Document object being built – .doc – instead of the whole document?

也许只取正在构建的当前文档对象的根节点—.doc—而不是整个文档?

builder.doc.root.to_s

#2


5  

A quick and dirty answer is to tell Nokogiri to reparse the resulting output, then look at the root:

一个快速而肮脏的答案是告诉Nokogiri重新解析结果输出,然后查看根:

require 'nokogiri'

builder = Nokogiri::XML::Builder.new do 
  request {
    data '1'
  }
end

puts Nokogiri::XML(builder.to_xml).root.to_xml

Which outputs:

输出:

<request>
  <data>1</data>
</request>