Ruby convert single quotes to double quotes in XML

前端 未结 2 1082
失恋的感觉
失恋的感觉 2021-01-11 17:03

Despite the fact that XML attributs can be defined using single or double quotes, my user is trying to integrate my software with another one that will not accept single quo

2条回答
  •  一生所求
    2021-01-11 17:29

    As of Feb 2007 there's a supported way of determining the quoting character. The changes were merged into Ruby sources on Jul 2007 and should be available on all versions since 1.8.6-p110:

    require 'rexml/document'
    
    doc = REXML::Document.new
    doc.context[:attribute_quote] = :quote  # <-- Set double-quote as the attribute value delimiter
    
    root = doc.add_element('root')
    root.add_attribute('val', '123')
    
    doc.write(STDOUT)
    

    Running that yields:

    $ ruby test.rb
    
    $
    

提交回复
热议问题