package csvfields

  1. Overview
  2. Docs

Module Xml_light.XmlSource

Xml Light

Xml Light is a minimal Xml parser & printer for OCaml. It provide few functions to parse a basic Xml document into an OCaml data structure and to print back the data structures to an Xml document.

Xml Light has also support for DTD (Document Type Definition).

(c)Copyright 2002-2003 Nicolas Cannasse

Xml Data Structure
Sourcetype xml = Types.xml =
  1. | Element of string * (string * string) list * xml list
  2. | PCData of string

An Xml node is either Element (tag-name, attributes, children) or PCData text

Xml Parsing

For easily parsing an Xml data source into an xml data structure, you can use theses functions. But if you want advanced parsing usage, please look at the XmlParser module. All the parsing functions can raise some exceptions, see the Exceptions section for more informations.

Sourceval parse_file : string -> xml

Parse the named file into an Xml data structure.

Sourceval parse_in : in_channel -> xml

Read the content of the in_channel and parse it into an Xml data structure.

Sourceval parse_string : string -> xml

Parse the string containing an Xml document into an Xml data structure.

Sourceval parse_string_with : XmlParser.t -> string -> xml
Xml Exceptions

Several exceptions can be raised when parsing an Xml document :

  • Xml.Error is raised when an xml parsing error occurs. the Xml.error_msg tells you which error occured during parsing and the Xml.error_pos can be used to retreive the document location where the error occured at.
  • Xml.File_not_found is raised when and error occured while opening a file with the Xml.parse_file function or when a DTD file declared by the Xml document is not found (see the XmlParser module for more informations on how to handle the DTD file loading).

If the Xml document is containing a DTD, then some other exceptions can be raised, see the module Dtd for more informations.

Sourcetype error_pos
Sourcetype error_msg = Types.error_msg =
  1. | UnterminatedComment
  2. | UnterminatedString
  3. | UnterminatedEntity
  4. | IdentExpected
  5. | CloseExpected
  6. | NodeExpected
  7. | AttributeNameExpected
  8. | AttributeValueExpected
  9. | EndOfTagExpected of string
  10. | EOFExpected
Sourcetype error = error_msg * error_pos
Sourceexception Error of error
Sourceexception File_not_found of string
Sourceval error : error -> string

Get a full error message from an Xml error.

Sourceval error_msg : error_msg -> string

Get the Xml error message as a string.

Sourceval line : error_pos -> int

Get the line the error occured at.

Sourceval range : error_pos -> int * int

Get the relative character range (in current line) the error occured at.

Sourceval abs_range : error_pos -> int * int

Get the absolute character range the error occured at.

Xml Functions
Sourceexception Not_element of xml
Sourceexception Not_pcdata of xml
Sourceexception No_attribute of string
Sourceval tag : xml -> string

tag xdata returns the tag value of the xml node. Raise Xml.Not_element if the xml is not an element

Sourceval pcdata : xml -> string

pcdata xdata returns the PCData value of the xml node. Raise Xml.Not_pcdata if the xml is not a PCData

Sourceval attribs : xml -> (string * string) list

attribs xdata returns the attribute list of the xml node. First string if the attribute name, second string is attribute value. Raise Xml.Not_element if the xml is not an element

Sourceval attrib : xml -> string -> string

attrib xdata "href" returns the value of the "href" attribute of the xml node (attribute matching is case-insensitive). Raise Xml.No_attribute if the attribute does not exists in the node's attribute list Raise Xml.Not_element if the xml is not an element

Sourceval children : xml -> xml list

children xdata returns the children list of the xml node Raise Xml.Not_element if the xml is not an element

Sourceval iter : (xml -> unit) -> xml -> unit

iter f xdata calls f on all children of the xml node. Raise Xml.Not_element if the xml is not an element

Sourceval map : (xml -> 'a) -> xml -> 'a list

map f xdata is equivalent to List.map f (Xml.children xdata) Raise Xml.Not_element if the xml is not an element

Sourceval fold : ('a -> xml -> 'a) -> 'a -> xml -> 'a

fold f init xdata is equivalent to List.fold_left f init (Xml.children xdata) Raise Xml.Not_element if the xml is not an element

Xml Printing
Sourceval to_string : xml -> string

Print the xml data structure into a compact xml string (without any user-readable formating ).

Sourceval to_string_fmt : xml -> string

Print the xml data structure into an user-readable string with tabs and lines break between different nodes.

Sourceval to_human_string : xml -> string
Sourcemodule type X = sig ... end
Sourcemodule Make (X : X) : sig ... end
OCaml

Innovation. Community. Security.