Ruby Unit Test : Is this a Valid (well-formed) XML Doc?

后端 未结 3 1952
一向
一向 2021-01-18 14:37

I\'m creating an XML document: I want to unit test at least to make sure it\'s well-formed. So far, I have only been able to approximate this , by using the \'hasElements\'

3条回答
  •  猫巷女王i
    2021-01-18 14:52

    You can use Nokogiri. It's not a standard Ruby library, but you can easily install it as a Gem.

    begin
      bad_doc = Nokogiri::XML(badly_formed) { |config| config.options = Nokogiri::XML::ParseOptions::STRICT }
    rescue Nokogiri::XML::SyntaxError => e
      puts "caught exception: #{e}"
    end
    # => caught exception: Premature end of data in tag root line 1
    

提交回复
热议问题