forked from JuliaIO/EzXML.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
julia2xml.jl
executable file
·36 lines (32 loc) · 918 Bytes
/
julia2xml.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env julia
using EzXML
# Convert a Julia expression to an XML document.
function expr2xml(expr)
doc = XMLDocument()
setroot!(doc, expr2elem(expr))
return doc
end
function expr2elem(expr)
if isa(expr, Expr)
# <expr/>
expr_node = ElementNode("expr")
# <head/>
head_node = ElementNode("head")
link!(head_node, TextNode(repr(expr.head)))
link!(expr_node, head_node)
# <args/>
args_node = ElementNode("args")
for arg in expr.args
link!(args_node, expr2elem(arg))
end
link!(expr_node, args_node)
return expr_node
else
# <literal/>
literal_node = ElementNode("literal")
literal_node["type"] = string(typeof(expr))
link!(literal_node, TextNode(repr(expr)))
return literal_node
end
end
prettyprint(expr2xml(Meta.parse(String(read(stdin)))))