如何验证大 XML
How to validate big XML
我正在尝试使用 Nokogiri 根据 XSD 验证 XML 文件。
当文件很小时我使用文档方法验证:
xsd = Nokogiri::XML::Schema(File.read(Rails.root.join('files/xsd', self::XSD)))
xml = Nokogiri::XML(File.read(Rails.root.join('public/uploads', file_path)))
xsd.validate(xml).each do |error|
end
当文件很大时,之前的方法不好,因为需要很多资源,所以我需要文件方法验证:
xsd = Nokogiri::XML::Schema(File.read(Rails.root.join('files/xsd', self::XSD)))
xml = Rails.root.join('public/uploads', file_path).to_s
xsd.validate(xml).each do |error|
end
但是第二种方法没有显示简单的错误,例如属性中未闭合的双引号:
<?xml version="1.0"?>
<catalog version="123 xmlns="http://google.com">
<book id="bk101">
第一个是。
Nokogiri 是适用于中小型 XML 的出色工具,但是当您处理大型 extremely-large/huge 文件时,您需要切换到其他工具,例如 SAX 解析或验证, 类似于 xmllint
.
The xmllint program parses one or more XML files, specified on the command line as xmlfile
. It prints various types of output, depending upon the options selected. It is useful for detecting errors both in XML code and in the XML parser itself.
It is included in libxml2.
我正在尝试使用 Nokogiri 根据 XSD 验证 XML 文件。 当文件很小时我使用文档方法验证:
xsd = Nokogiri::XML::Schema(File.read(Rails.root.join('files/xsd', self::XSD)))
xml = Nokogiri::XML(File.read(Rails.root.join('public/uploads', file_path)))
xsd.validate(xml).each do |error|
end
当文件很大时,之前的方法不好,因为需要很多资源,所以我需要文件方法验证:
xsd = Nokogiri::XML::Schema(File.read(Rails.root.join('files/xsd', self::XSD)))
xml = Rails.root.join('public/uploads', file_path).to_s
xsd.validate(xml).each do |error|
end
但是第二种方法没有显示简单的错误,例如属性中未闭合的双引号:
<?xml version="1.0"?>
<catalog version="123 xmlns="http://google.com">
<book id="bk101">
第一个是。
Nokogiri 是适用于中小型 XML 的出色工具,但是当您处理大型 extremely-large/huge 文件时,您需要切换到其他工具,例如 SAX 解析或验证, 类似于 xmllint
.
The xmllint program parses one or more XML files, specified on the command line as
xmlfile
. It prints various types of output, depending upon the options selected. It is useful for detecting errors both in XML code and in the XML parser itself.It is included in libxml2.