module Xmlr:Interface to libxml's XMLReader.sig..end
The Xmlr API provides a reader type, which represents the cursor within
the document. A reader can only be advanced, and most of the functions return
information about the node the reader is currently at.
type t
type nodetype =
| |
NodeTypeNone |
| |
StartElement |
| |
Attribute |
| |
Text |
| |
CData |
| |
EntityRef |
| |
EntityDecl |
| |
PI |
| |
Comment |
| |
Document |
| |
Doctype |
| |
Fragment |
| |
Notation |
| |
Whitespace |
| |
SigWhitespace |
| |
EndElement |
| |
EndEntity |
| |
XMLDecl |
val string_of_nodetype : nodetype -> stringtype parser_option =
| |
Recover |
| |
NoEnt |
| |
DTDLoad |
| |
DTDAttr |
| |
DTDValid |
| |
NoError |
| |
NoWarning |
| |
Pedantic |
| |
NoBlanks |
| |
SAX1 |
| |
XInclude |
| |
NoNet |
| |
NoDict |
| |
NSClean |
| |
NoCDATA |
val from_filename : ?encoding:string -> ?opts:parser_option list -> string -> tval from_string : ?baseurl:string ->
?encoding:string -> ?opts:parser_option list -> string -> tval read : t -> booltrue if there was a node,
false if there wasn't, and raises an exception on error.val next : t -> boolfalse if there isn't a node following the current one, and raises an exception on error.val node_type : t -> nodetypenodetype of the current node.val prefix : t -> stringval local_name : t -> stringval name : t -> stringval namespace_uri : t -> stringval has_value : t -> booltrue if the current node has a text value.val value : t -> stringval base_uri : t -> stringval is_empty_element : t -> booltrue if the current node is an empty element.val depth : t -> intval has_attributes : t -> booltrue if the current element has attributes.val attribute_count : t -> intval get_attribute : t -> string -> stringget_attribute reader attr gets the value of the attribute with the qualified name attr.
Raises Not_found if the attribute doesn't exist.val get_attribute_no : t -> int -> stringget_attribute_no reader n returns the value of the nth attribute.
Raises Not_found if the attribute doesn't exist.val get_attribute_ns : t -> string -> string -> stringget_attribute reader ns attr gets the value of the attribute with the namespace ns and name attr.
Raises Not_found if the attribute doesn't exist.val skip_to_close : t -> string -> unitskip_to_close r tag reads r forward up the next closing tag with name
tag. Raises Not_found if there is no closing tag.val fold_subnodes : t -> string -> 'a -> (string -> 'a -> 'a) -> 'afold_subnodes reader tag initial f iterates reader through the subnodes
of the current tag, passing each node to f along with the current state,
which f transforms. After fold_subnodes runs, reader is positioned at the
closing tag.
Example:
type person = { person_firstname : string;
person_lastname : string }
let empty_person = { person_firstname=""; person_lastname="" }
let sample_xml = "<person><firstname>John</firstname><lastname>Doe</lastname></person>"
let johndoe =
let r = Xmlr.from_string sample_xml in
if not (Xmlr.read r) then failwith "Cannot find start element";
if (Xmlr.node_type r <> Xmlr.StartElement ||
Xmlr.name r <> "person") then failwith "Bad start element";
Xmlr.fold_subnodes r "person" empty_person (fun tag person ->
match tag with
"firstname" -> { person with person_firstname = Xmlr.value r }
| "lastname" -> { person with person_lastname = Xmlr.value r }
| _ -> person)val iter_subnodes : t -> string -> (string -> unit) -> unititer_subnodes r tag f calls f on each subnode of the current node,
ending at tag.