From b3c71361a8c974df31392ddd7211a0f8dafbbd62 Mon Sep 17 00:00:00 2001 From: TimG1964 <157401228+TimG1964@users.noreply.github.com> Date: Sat, 31 May 2025 15:29:29 +0100 Subject: [PATCH 1/3] Add Base,copy(::Node) --- src/XML.jl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/XML.jl b/src/XML.jl index 9027ade..9baaaa5 100644 --- a/src/XML.jl +++ b/src/XML.jl @@ -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) From 4763d2f5e9f49d7c5e926b16c60e6edc1381cdc3 Mon Sep 17 00:00:00 2001 From: TimG1964 <157401228+TimG1964@users.noreply.github.com> Date: Sat, 31 May 2025 15:31:22 +0100 Subject: [PATCH 2/3] Update information on copying nodes --- README.md | 46 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 9968fdc..8c7e5d6 100644 --- a/README.md +++ b/README.md @@ -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 (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 (1 child) -simple_value(node2) -# "changed" +julia> node3 = Node(node2, XML.Element("new_child", child_att="I'm new too")) +Node Element (2 children) + +julia> println(XML.write(node)) +child + +julia> println(XML.write(node2)) +child + +julia> println(XML.write(node3)) + + child + + + +julia> node4=copy(node3) +Node Element (2 children) + +julia> println(XML.write(node4)) + + child + + + +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: From e08e523504458fe6561cd5301059fe700f9ac176 Mon Sep 17 00:00:00 2001 From: TimG1964 <157401228+TimG1964@users.noreply.github.com> Date: Sat, 31 May 2025 15:32:44 +0100 Subject: [PATCH 3/3] Add tests for `copy()` --- test/runtests.jl | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/runtests.jl b/test/runtests.jl index d41924b..e5f5f45 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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