Skip to content

Commit 177afb0

Browse files
committed
add setindex! method for changing attributes
1 parent 53d7ed3 commit 177afb0

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,13 @@ doc[end][2] # Second child of root
6464
# Add a child:
6565
push!(parent::Node, child::Node)
6666

67+
6768
# Replace a child:
6869
parent[2] = child
70+
71+
72+
# Add/change an attribute:
73+
node["key"] = value
6974
```
7075

7176
- **XML** defines `(::NodeType)(args...; kw...)` for more convenient syntax in creating `Node`s, e.g.:

src/XML.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,13 @@ Base.parse(x::AbstractString, ::Type{Node}) = Node(parse(x, Raw))
150150
Base.setindex!(o::Node, val, i::Integer) = o.children[i] = Node(val)
151151
Base.push!(a::Node, b::Node) = push!(a.children, b)
152152

153+
function Base.setindex!(o::Node, val, key::AbstractString)
154+
if isnothing(o.attributes)
155+
o.attributes = Dict{String,String}()
156+
end
157+
o.attributes[key] = string(val)
158+
end
159+
153160
Base.show(io::IO, o::Node) = _show_node(io, o)
154161

155162
#-----------------------------------------------------------------------------# Node Constructors

0 commit comments

Comments
 (0)