Skip to content

Commit

Permalink
keep back refernce to top level XMLDocument object to avoid
Browse files Browse the repository at this point in the history
premature finalisation
  • Loading branch information
samoconnor committed Mar 23, 2016
1 parent 146af78 commit d7b63d0
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/XMLDict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@ using DataStructures

type XMLDictElement <:Associative{Union{AbstractString,Symbol},AbstractString}
x
doc
end

wrap(x) = XMLDictElement(x)
wrap(l::Vector) = [wrap(i) for i in l]

Base.get(x::XMLDictElement, args...) = XMLDict.get(x.x, args...)

wrap(x, doc) = XMLDictElement(x, doc)
wrap(l::Vector, doc) = [wrap(i, doc) for i in l]

Base.get(x::XMLDictElement, args...) = XMLDict.get(x.x, x.doc, args...)

xml_dict(x, args...; options...) = xml_dict(x.x, args...; options...)

Expand All @@ -54,7 +53,7 @@ Base.show(io::IO, x::XMLDictElement) = show(io, x.x)
function parse_xml(xml::AbstractString)
doc = LightXML.parse_string(xml)
finalizer(doc, LightXML.free)
return wrap(doc)
return wrap(doc, doc)
end


Expand All @@ -67,7 +66,7 @@ end
# Get sub-elements that match tag.
# For leaf-nodes return element content (text).

function XMLDict.get(x::XMLElement, tag::AbstractString, default)
function XMLDict.get(x::XMLElement, doc::XMLDocument, tag::AbstractString, default)

if tag == ""
return strip(content(x))
Expand All @@ -81,23 +80,25 @@ function XMLDict.get(x::XMLElement, tag::AbstractString, default)
isempty(attributes(l[1]))
l = [strip(content(i)) for i in l]
else
l = wrap(l)
l = wrap(l, doc)
end
return length(l) == 1 ? l[1] : l
end


# Get element attribute by "name".

function XMLDict.get(x::XMLElement, name::Symbol, default)
function XMLDict.get(x::XMLElement, doc::XMLDocument, name::Symbol, default)
r = attribute(x, string(name))
r != nothing ? r : default
end


# Wrappers for XMLDocument.
# Wrapper for XMLDocument.

XMLDict.get(x::XMLDocument, tag, default) = get(root(x), tag, default)
function XMLDict.get(x::XMLDocument, doc::XMLDocument, tag, default)
return XMLDict.get(root(x), doc, tag, default)
end



Expand Down

0 comments on commit d7b63d0

Please sign in to comment.