Skip to content

Instantly share code, notes, and snippets.

@talex5
Last active December 17, 2015 19:39
Show Gist options
  • Save talex5/5661802 to your computer and use it in GitHub Desktop.
Save talex5/5661802 to your computer and use it in GitHub Desktop.
Testing XML parsing in OCaml
<foo:one xmlns:foo='http://example.com/'><foo:two xmlns:foo='http://foo/'><foo:three/></foo:two></foo:one>
from xml.dom import minidom
doc = minidom.parse("test.xml")
def show_node(node):
print(node.localName, node.namespaceURI)
# Prints (as expected):
#
# one http://example.com/
# two http://foo/
# three http://foo/
root = doc.documentElement
show_node(root)
show_node(root.childNodes[0])
show_node(root.childNodes[0].childNodes[0])
(* ocamlfind ocamlopt -o xml -package pxp -linkpkg xml.ml *)
let m = Pxp_dtd.create_namespace_manager()
let config = { Pxp_types.default_config with
Pxp_types.encoding = `Enc_utf8;
Pxp_types.enable_namespace_processing = Some m
}
let spec = Pxp_tree_parser.default_namespace_spec
let source = Pxp_types.from_file "test.xml"
let doc = Pxp_tree_parser.parse_wfdocument_entity config source spec;;
let show node =
print_endline (node#localname);
print_endline (node#namespace_uri);;
open Pxp_document;;
let root = doc#root;;
show root;;
let child = List.hd (root#sub_nodes);;
show child;;
let gchild = List.hd (child#sub_nodes);;
show gchild;;
(* Prints (wrong):
one
http://example.com/
two
http://example.com/
three
http://example.com/
*)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment