Skip to content

Commit 4763d2f

Browse files
authored
Update information on copying nodes
1 parent b3c7136 commit 4763d2f

File tree

1 file changed

+39
-7
lines changed

1 file changed

+39
-7
lines changed

README.md

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,20 +99,52 @@ node["key"] = value
9999
node["key"]
100100
```
101101

102-
- `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:
102+
- `Node` is an immutable type. However, you can easily create a simple copy using `copy(::Node)` or
103+
copy with one or more changes by using the `Node(::Node, atts...; kw...)` constructor where `atts`
104+
are new children to add and `kw` are the node attributes you want to add or change. For example:
103105

104106
```julia
105-
node = XML.Element("tag", XML.Text("child"))
107+
julia> node = XML.Element("tag", XML.Text("child"))
108+
Node Element <tag> (1 child)
106109

107-
simple_value(node)
108-
# "child"
110+
julia> simple_value(node)
111+
"child"
109112

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

112-
simple_value(node2)
113-
# "changed"
116+
julia> node3 = Node(node2, XML.Element("new_child", child_att="I'm new too"))
117+
Node Element <tag new_att="I'm new"> (2 children)
118+
119+
julia> println(XML.write(node))
120+
<tag>child</tag>
121+
122+
julia> println(XML.write(node2))
123+
<tag new_att="I'm new">child</tag>
124+
125+
julia> println(XML.write(node3))
126+
<tag new_att="I'm new">
127+
child
128+
<new_child child_att="I'm new too"/>
129+
</tag>
130+
131+
julia> node4=copy(node3)
132+
Node Element <tag new_att="I'm new"> (2 children)
133+
134+
julia> println(XML.write(node4))
135+
<tag new_att="I'm new">
136+
child
137+
<new_child child_att="I'm new too"/>
138+
</tag>
139+
140+
julia> node4==node3
141+
true
142+
143+
julia> node4===node3
144+
false
114145
```
115146

147+
116148
### Writing `Element` `Node`s with `XML.h`
117149

118150
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:

0 commit comments

Comments
 (0)