Last active
December 17, 2015 19:39
-
-
Save talex5/5661802 to your computer and use it in GitHub Desktop.
Testing XML parsing in OCaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<foo:one xmlns:foo='http://example.com/'><foo:two xmlns:foo='http://foo/'><foo:three/></foo:two></foo:one> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* 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