开发者

How to save unescaped & in nokogiri xml?

How can I save & in the final xml file using nokogiri?

My code is like:

require 'rubygems' 
require 'nokogiri'

  file_na开发者_开发知识库me = "amp.xml"
    @doc = Nokogiri::XML('<project/>')

    arg = Nokogiri::XML::Node.new "arg", @doc
    arg['line'] = "how to save only &???"
    @doc.root.add_child(arg)

    File.open(file_name, 'w') {|f| f.write(@doc.to_xml) }

and the output is like

<?xml version="1.0"?>
<project>
  <arg line="how to save only &amp;???"/>
</project>

UPDATE

Looks like I can use CDATA but not sure how to use it with nokogiri. I read the xml file using @doc = Nokogiri::XML(File.open(file_name))


You can't put an unescaped & in XML as you wish. Here is from the W3 spec for XML:

The ampersand character (&) and the left angle bracket (<) MUST NOT appear in their literal form, except when used as markup delimiters, or within a comment, a processing instruction, or a CDATA section. If they are needed elsewhere, they MUST be escaped using either numeric character references or the strings " & " and " < " respectively.

As for using CDATA in Nokogiri, here is info from Nokogiri's site, if you use Nokogiri::XML::Builder to build your XML.

UPDATE: Here is the code from my example mentioned in comments.

module Questions
  @source = File.dirname(__FILE__) + '/questions.xml'
  def parse
    if File.exists?(@source)
      File.open(@source, 'r+') do |document|
        q = {}
        text = Nokogiri::XML::Document.parse(document)
        text.xpath('.//question').each do |c|
          parent = c.attribute_nodes[2].to_s
          q[:type] = c.attribute_nodes[1].to_s.to_sym   # => question type
          q[:q_id] = c.attribute_nodes[0].to_s   # => question type
          q[:question] = c.xpath('.//q').first.content   # => question
          q[:answers] = []
          c.xpath('.//a').each { |ans|
            p = ans.attribute_nodes.first.value   # => point value
            a = ans.content   # => answer
            q[:answers] << [a, p]
          }
          if parent == "NA"
            Question.create!(q)
          else
            Question.first(conditions: {q_id: parent}).children << Question.create!(q)
          end
        end
      end
    end
  end

  def write
    builder = Nokogiri::XML::Builder.new do |xml|
      xml.root {
        Question.each do |t|
          xml.question(id: t.id, type: t.type, parent: t.parent) {
            xml.q_ t.q
            t.answers.each { |c|
              xml.a(point: c.p) { xml.text c.a }
            }
          }
        end
      }
    end
    document = builder.to_xml
    File.open(@source, 'w+') do |f|
      f.puts document
    end
  end   # end write

  module_function :parse
  module_function :write
end

--- And an example of what I was working with. ---

  <question id="q0000" type="root" parent="NA">
    <q>How do you feel about sports?</q>
    <a point="0">I don't have any interest in sports.</a>
    <a point="q0001">I like to play sports.</a>
    <a point="q0002">I follow college or professional sports.</a>
  </question>


I recently had a similar problem. You need to use xml.<< instead of xml.text https://www.rubydoc.info/github/sparklemotion/nokogiri/Nokogiri/XML/Builder#%3C%3C-instance_method

Change:

            t.answers.each { |c|
              xml.a(point: c.p) { xml.text c.a }
            }

to

            t.answers.each { |c|
              xml.a(point: c.p) { xml << c.a }
            }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜