9
9
# Interface:
10
10
children, nodetype, tag, attributes, value, is_simple, simplevalue, simple_value,
11
11
# Extended Interface for LazyNode:
12
- parent, depth, next, prev
12
+ parent, depth, next, prev,
13
+ # Extension XMLJSONExt:
14
+ xml2dicts, xml2json
13
15
14
16
# -----------------------------------------------------------------------------# escape/unescape
15
17
const escape_chars = (' &' => " &" , ' <' => " <" , ' >' => " >" , " '" => " '" , ' "' => " "" )
@@ -69,9 +71,9 @@ A Lazy representation of an XML node.
69
71
"""
70
72
mutable struct LazyNode <: AbstractXMLNode
71
73
raw:: Raw
72
- tag:: Union{Nothing, String}
73
- attributes:: Union{Nothing, OrderedDict{String, String}}
74
- value:: Union{Nothing, String}
74
+ tag:: Union{Nothing,String}
75
+ attributes:: Union{Nothing,OrderedDict{String,String}}
76
+ value:: Union{Nothing,String}
75
77
end
76
78
LazyNode (raw:: Raw ) = LazyNode (raw, nothing , nothing , nothing )
77
79
@@ -126,33 +128,33 @@ A representation of an XML DOM node. For simpler construction, use `(::NodeType
126
128
"""
127
129
struct Node <: AbstractXMLNode
128
130
nodetype:: NodeType
129
- tag:: Union{Nothing, String}
130
- attributes:: Union{Nothing, OrderedDict{String, String}}
131
- value:: Union{Nothing, String}
132
- children:: Union{Nothing, Vector{Node}}
131
+ tag:: Union{Nothing,String}
132
+ attributes:: Union{Nothing,OrderedDict{String,String}}
133
+ value:: Union{Nothing,String}
134
+ children:: Union{Nothing,Vector{Node}}
133
135
134
136
function Node (nodetype:: NodeType , tag= nothing , attributes= nothing , value= nothing , children= nothing )
135
137
new (nodetype,
136
138
isnothing (tag) ? nothing : string (tag),
137
139
isnothing (attributes) ? nothing : OrderedDict (string (k) => string (v) for (k, v) in pairs (attributes)),
138
140
isnothing (value) ? nothing : string (value),
139
141
isnothing (children) ? nothing :
140
- children isa Node ? [children] :
141
- children isa Vector{Node} ? children :
142
- children isa Vector ? map (Node, children) :
143
- children isa Tuple ? map (Node, collect (children)) :
144
- [Node (children)]
142
+ children isa Node ? [children] :
143
+ children isa Vector{Node} ? children :
144
+ children isa Vector ? map (Node, children) :
145
+ children isa Tuple ? map (Node, collect (children)) :
146
+ [Node (children)]
145
147
)
146
148
end
147
149
end
148
150
149
151
function Node (o:: Node , x... ; kw... )
150
152
attrs = ! isnothing (kw) ?
151
- merge (
152
- OrderedDict (string (k) => string (v) for (k,v) in pairs (kw)),
153
- isnothing (o. attributes) ? OrderedDict {String, String} () : o. attributes
154
- ) :
155
- o. attributes
153
+ merge (
154
+ OrderedDict (string (k) => string (v) for (k, v) in pairs (kw)),
155
+ isnothing (o. attributes) ? OrderedDict {String,String} () : o. attributes
156
+ ) :
157
+ o. attributes
156
158
children = isempty (x) ? o. children : vcat (isnothing (o. children) ? [] : o. children, collect (x))
157
159
Node (o. nodetype, o. tag, attrs, o. value, children)
158
160
end
@@ -171,7 +173,7 @@ Node(data::Raw) = Node(LazyNode(data))
171
173
# Anything that's not Vector{UInt8} or a (Lazy)Node is converted to a Text Node
172
174
Node (x) = Node (Text, nothing , nothing , string (x), nothing )
173
175
174
- h (tag:: Union{Symbol, String} , children... ; kw... ) = Node (Element, tag, kw, nothing , children)
176
+ h (tag:: Union{Symbol,String} , children... ; kw... ) = Node (Element, tag, kw, nothing , children)
175
177
Base. getproperty (:: typeof (h), tag:: Symbol ) = h (tag)
176
178
(o:: Node )(children... ; kw... ) = Node (o, Node .(children)... ; kw... )
177
179
@@ -261,7 +263,7 @@ next(o) = missing
261
263
prev (o) = missing
262
264
263
265
is_simple (o) = nodetype (o) == Element && (isnothing (attributes (o)) || isempty (attributes (o))) &&
264
- length (children (o)) == 1 && nodetype (only (o)) in (Text, CData)
266
+ length (children (o)) == 1 && nodetype (only (o)) in (Text, CData)
265
267
266
268
simple_value (o) = is_simple (o) ? value (only (o)) : error (" `XML.simple_value` is only defined for simple nodes." )
267
269
@@ -274,22 +276,22 @@ function nodes_equal(a, b)
274
276
out &= XML. attributes (a) == XML. attributes (b)
275
277
out &= XML. value (a) == XML. value (b)
276
278
out &= length (XML. children (a)) == length (XML. children (b))
277
- out &= all (nodes_equal (ai, bi) for (ai,bi) in zip (XML. children (a), XML. children (b)))
279
+ out &= all (nodes_equal (ai, bi) for (ai, bi) in zip (XML. children (a), XML. children (b)))
278
280
return out
279
281
end
280
282
281
283
Base.:(== )(a:: AbstractXMLNode , b:: AbstractXMLNode ) = nodes_equal (a, b)
282
284
283
285
# -----------------------------------------------------------------------------# parse
284
- Base. parse (:: Type{T} , str:: AbstractString ) where {T <: AbstractXMLNode } = parse (str, T)
286
+ Base. parse (:: Type{T} , str:: AbstractString ) where {T<: AbstractXMLNode } = parse (str, T)
285
287
286
288
# -----------------------------------------------------------------------------# indexing
287
- Base. getindex (o:: Union{Raw, AbstractXMLNode} ) = o
288
- Base. getindex (o:: Union{Raw, AbstractXMLNode} , i:: Integer ) = children (o)[i]
289
- Base. getindex (o:: Union{Raw, AbstractXMLNode} , :: Colon ) = children (o)
290
- Base. lastindex (o:: Union{Raw, AbstractXMLNode} ) = lastindex (children (o))
289
+ Base. getindex (o:: Union{Raw,AbstractXMLNode} ) = o
290
+ Base. getindex (o:: Union{Raw,AbstractXMLNode} , i:: Integer ) = children (o)[i]
291
+ Base. getindex (o:: Union{Raw,AbstractXMLNode} , :: Colon ) = children (o)
292
+ Base. lastindex (o:: Union{Raw,AbstractXMLNode} ) = lastindex (children (o))
291
293
292
- Base. only (o:: Union{Raw, AbstractXMLNode} ) = only (children (o))
294
+ Base. only (o:: Union{Raw,AbstractXMLNode} ) = only (children (o))
293
295
294
296
Base. length (o:: AbstractXMLNode ) = length (children (o))
295
297
338
340
function _print_attrs (io:: IO , o; color= :normal )
339
341
attr = attributes (o)
340
342
isnothing (attr) && return nothing
341
- for (k,v) in attr
343
+ for (k, v) in attr
342
344
# printstyled(io, ' ', k, '=', '"', v, '"'; color)
343
345
print (io, ' ' , k, ' =' , ' "' , v, ' "' )
344
346
end
@@ -356,13 +358,13 @@ write(x; kw...) = (io = IOBuffer(); write(io, x; kw...); String(take!(io)))
356
358
write (filename:: AbstractString , x; kw... ) = open (io -> write (io, x; kw... ), filename, " w" )
357
359
358
360
function write (io:: IO , x; indentsize:: Int = 2 , depth:: Int = depth (x))
359
- indent = ' ' ^ indentsize
361
+ indent = ' ' ^ indentsize
360
362
nodetype = XML. nodetype (x)
361
363
tag = XML. tag (x)
362
364
value = XML. value (x)
363
365
children = XML. children (x)
364
366
365
- padding = indent ^ max (0 , depth - 1 )
367
+ padding = indent^ max (0 , depth - 1 )
366
368
print (io, padding)
367
369
if nodetype === Text
368
370
print (io, value)
@@ -377,7 +379,7 @@ function write(io::IO, x; indentsize::Int=2, depth::Int=depth(x))
377
379
else
378
380
println (io)
379
381
foreach (children) do child
380
- write (io, child; indentsize, depth = depth + 1 )
382
+ write (io, child; indentsize, depth= depth + 1 )
381
383
println (io)
382
384
end
383
385
print (io, padding, " </" , tag, ' >' )
@@ -407,4 +409,8 @@ function write(io::IO, x; indentsize::Int=2, depth::Int=depth(x))
407
409
end
408
410
end
409
411
412
+ # Extension XMLJSONExt
413
+ function xml2dicts end
414
+ function xml2json end
415
+
410
416
end
0 commit comments