Class: Canon::Formatters::XmlFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/canon/formatters/xml_formatter.rb

Overview

XML formatter using Canonical XML 1.1 or pretty printing

Class Method Summary collapse

Class Method Details

.format(xml, pretty: true, indent: 2) ⇒ String

Format XML with pretty printing by default

Parameters:

  • xml (String)

    XML document to format

  • pretty (Boolean) (defaults to: true)

    Whether to pretty print (default: true)

  • indent (Integer) (defaults to: 2)

    Number of spaces for indentation (default: 2)

Returns:

  • (String)

    Formatted XML



17
18
19
20
21
22
23
# File 'lib/canon/formatters/xml_formatter.rb', line 17

def self.format(xml, pretty: true, indent: 2)
  if pretty
    Canon::PrettyPrinter::Xml.new(indent: indent).format(xml)
  else
    Canon::Xml::C14n.canonicalize(xml, with_comments: false)
  end
end

.parse(xml) ⇒ Nokogiri::XML::Document

Parse XML into a Nokogiri document

Parameters:

  • xml (String)

    XML document to parse

Returns:

  • (Nokogiri::XML::Document)

    Parsed XML document



28
29
30
31
32
# File 'lib/canon/formatters/xml_formatter.rb', line 28

def self.parse(xml)
  # Validate before parsing
  Canon::Validators::XmlValidator.validate!(xml)
  Nokogiri::XML(xml)
end