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

后端 未结 3 1953
一向
一向 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条回答
  •  孤城傲影
    2021-01-18 14:50

    rexml - build-in library. You can use a error handler to check your xml files

    require 'rexml/document'
    include REXML
    
    errormsg = ''
    doc = nil
    begin
      doc = Document.new(File.new(filename))
    rescue
      errormsg = $!
    end
    
    puts "Fail: #{errormsg}"   if('' != errormsg)
    

提交回复
热议问题