module XML

Overview

The XML module allows parsing and generating XML documents.

XML#parse will parse xml from String or io and return xml document as an XML:Node which represents all kinds of xml nodes.

Example:

require "xml"

xml = <<-XML
 <person id="1">
  <firstname>Jane</firstname>
  <lastname>Doe</lastname>
 </person>
XML

document = XML.parse(xml)             # : XML::Node
person = document.first_element_child # : XML::Node?
if person
  puts person["id"] # "1" : String?

  puts typeof(person.children)                       # XML::NodeSet
  person.children.select(&.element?).each do |child| # Select only element children
    puts typeof(child)                               # XML::Node
    puts child.name                                  # firstname : String
    puts child.content                               # Jane : String?
  end
end

Defined in:

xml.cr

Constant Summary

SUBSTITUTIONS = {'>' => "&gt;", '<' => "&lt;", '"' => "&quot;", '\'' => "&apos;", '&' => "&amp;"}

Class Method Summary

Class Method Detail

def self.escape(string : String) #

[View source]
def self.parse(io : IO, options : ParserOptions = ParserOptions.default) : Node #

Parses an XML document from io with options into an XML::Node. See ParserOptions.default for default options.


[View source]
def self.parse(string : String, options : ParserOptions = ParserOptions.default) : Node #

Parses an XML document from string with options into an XML::Node. See ParserOptions.default for default options.


[View source]
def self.parse_html(io : IO, options : HTMLParserOptions = HTMLParserOptions.default) : Node #

Parses an HTML document from io with options into an XML::Node. See HTMLParserOptions.default for default options.


[View source]
def self.parse_html(string : String, options : HTMLParserOptions = HTMLParserOptions.default) : Node #

Parses an HTML document from string with options into an XML::Node. See HTMLParserOptions.default for default options.


[View source]