Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 39 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,20 +99,52 @@ node["key"] = value
node["key"]
```

- `Node` is an immutable type. However, you can easily create a copy with one or more field values changed by using the `Node(::Node; kw...)` constructor where `kw` are the fields you want to change. For example:
- `Node` is an immutable type. However, you can easily create a simple copy using `copy(::Node)` or
copy with one or more changes by using the `Node(::Node, atts...; kw...)` constructor where `atts`
are new children to add and `kw` are the node attributes you want to add or change. For example:

```julia
node = XML.Element("tag", XML.Text("child"))
julia> node = XML.Element("tag", XML.Text("child"))
Node Element <tag> (1 child)

simple_value(node)
# "child"
julia> simple_value(node)
"child"

node2 = Node(node, children=XML.Text("changed"))
julia> node2 = Node(node, new_att="I'm new")
Node Element <tag new_att="I'm new"> (1 child)

simple_value(node2)
# "changed"
julia> node3 = Node(node2, XML.Element("new_child", child_att="I'm new too"))
Node Element <tag new_att="I'm new"> (2 children)

julia> println(XML.write(node))
<tag>child</tag>

julia> println(XML.write(node2))
<tag new_att="I'm new">child</tag>

julia> println(XML.write(node3))
<tag new_att="I'm new">
child
<new_child child_att="I'm new too"/>
</tag>

julia> node4=copy(node3)
Node Element <tag new_att="I'm new"> (2 children)

julia> println(XML.write(node4))
<tag new_att="I'm new">
child
<new_child child_att="I'm new too"/>
</tag>

julia> node4==node3
true

julia> node4===node3
false
```


### Writing `Element` `Node`s with `XML.h`

Similar to [Cobweb.jl](https://github.com/JuliaComputing/Cobweb.jl#-creating-nodes-with-cobwebh), `XML.h` enables you to write elements with a simpler syntax:
Expand Down
4 changes: 4 additions & 0 deletions src/XML.jl
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ simple_value(o) = is_simple(o) ? value(only(o)) : error("`XML.simple_value` is o

Base.@deprecate_binding simplevalue simple_value

#-----------------------------------------------------------------------------# copy node

Base.copy(o::AbstractXMLNode) = Node(o.nodetype, o.tag, o.attributes, o.value, isnothing(o.children) ? nothing : [Base.copy(x) for x in o.children])

#-----------------------------------------------------------------------------# nodes_equal
function nodes_equal(a, b)
out = XML.tag(a) == XML.tag(b)
Expand Down
10 changes: 10 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,16 @@ end
end
end

#-----------------------------------------------------------------------------# copy
@testset "Copy node" begin
for path in all_files
node = read(path, Node)
node2 = copy(node)
@test node == node2
@test node2==deepcopy(node)
end
end

#-----------------------------------------------------------------------------# roundtrip
@testset "read/write/read roundtrip" begin
for path in all_files
Expand Down
Loading