Skip to content

Commit 11a6a55

Browse files
authored
Merge pull request #23 from JuliaComputing/jd/dev
update benchmarks and add h function
2 parents f7998c5 + 19d3c34 commit 11a6a55

File tree

5 files changed

+59
-20
lines changed

5 files changed

+59
-20
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "XML"
22
uuid = "72c71f33-b9b6-44de-8c94-c961784809e2"
33
authors = ["Josh Day <[email protected]> and contributors"]
4-
version = "0.3.2"
4+
version = "0.3.3"
55

66
[deps]
77
Mmap = "a63ad114-7e13-5084-954f-fe012c677804"

README.md

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ doc[end][2] # Second child of root
8383
## `Node`: Probably What You're Looking For
8484

8585
- `read`-ing a `Node` loads the entire XML DOM in memory.
86-
- **This is what you would use to build an XML document programmatically.**
8786
- See the table above for convenience constructors.
8887
- `Node`s have some additional methods that aid in construction/mutation:
8988

@@ -114,6 +113,24 @@ simplevalue(node2)
114113
# "changed"
115114
```
116115

116+
### Writing `Element` `Node`s with `XML.h`
117+
118+
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:
119+
120+
```julia
121+
using XML: h
122+
123+
julia> node = h.parent(
124+
h.child("content", id="my id")
125+
)
126+
# Node Element <parent> (1 child)
127+
128+
julia> XML.write(node)
129+
# <parent>
130+
# <child id=\"my id\">content</child>
131+
# </parent>
132+
```
133+
117134
<br>
118135

119136
## `XML.LazyNode`: For Fast Iteration through an XML File
@@ -173,45 +190,47 @@ XML.write(node) # String
173190

174191
```
175192
julia> versioninfo()
176-
Julia Version 1.8.5
177-
Commit 17cfb8e65ea (2023-01-08 06:45 UTC)
193+
Julia Version 1.9.4
194+
Commit 8e5136fa297 (2023-11-14 08:46 UTC)
195+
Build Info:
196+
Official https://julialang.org/ release
178197
Platform Info:
179-
OS: macOS (arm64-apple-darwin21.5.0)
198+
OS: macOS (arm64-apple-darwin22.4.0)
180199
CPU: 10 × Apple M1 Pro
181200
WORD_SIZE: 64
182201
LIBM: libopenlibm
183-
LLVM: libLLVM-13.0.1 (ORCJIT, apple-m1)
184-
Threads: 1 on 8 virtual cores
202+
LLVM: libLLVM-14.0.6 (ORCJIT, apple-m1)
203+
Threads: 8 on 8 virtual cores
185204
```
186205

187206

188207
### Reading an XML File
189208

190209
```
191-
XML.LazyNode 0.012084
192-
XML.Node ■■■■■■■■■■■■■■■■■■■■■■■■■■■ 888.367
193-
EzXML.readxml ■■■■■■ 200.009
194-
XMLDict.xml_dict ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1350.63
210+
XML.LazyNode 0.009583
211+
XML.Node ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1071.32
212+
EzXML.readxml ■■■■■■■■■ 284.346
213+
XMLDict.xml_dict ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1231.47
195214
```
196215

197216
### Writing an XML File
198217

199218
```
200-
Write: XML ■■■■■■■■■■■■■■■■■■■■■■ 244.261
201-
Write: EzXML ■■■■■■■■■■ 106.953
219+
Write: XML ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 289.638
220+
Write: EzXML ■■■■■■■■■■■■■ 93.4631
202221
```
203222

204223
### Lazily Iterating over Each Node
205224
```
206-
LazyNode ■■■■■■■■■■■■■■■■ 55.1
207-
EzXML.StreamReader ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 142.515
225+
LazyNode ■■■■■■■■■ 51.752
226+
EzXML.StreamReader ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 226.271
208227
```
209228

210229
### Collecting All Names/Tags in an XML File
211230
```
212-
XML.LazyNode ■■■■■■■■■■■■■■■■■■■■■■■■■■ 152.298
213-
EzXML.StreamReader ■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 165.21
214-
EzXML.readxml ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 239.197
231+
XML.LazyNode ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 210.482
232+
EzXML.StreamReader ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 276.238
233+
EzXML.readxml ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 263.269
215234
```
216235

217236
<br>

benchmarks/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[deps]
22
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
3+
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
34
EzXML = "8f5d6c58-4d21-5cfd-889c-e3ad7ee6a615"
45
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
56
UnicodePlots = "b8865327-cd53-5732-bb35-84acbb429228"

src/XML.jl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,16 @@ struct Node <: AbstractXMLNode
146146
end
147147
end
148148

149-
Node(o::Node; kw...) = isempty(kw) ? o : Node((get(kw, x, getfield(o, x)) for x in fieldnames(Node))...)
149+
function Node(o::Node, x...; kw...)
150+
attrs = !isnothing(kw) ?
151+
merge(
152+
OrderedDict(string(k) => string(v) for (k,v) in pairs(kw)),
153+
isnothing(o.attributes) ? OrderedDict{String, String}() : o.attributes
154+
) :
155+
o.attributes
156+
children = isempty(x) ? o.children : vcat(isnothing(o.children) ? [] : o.children, collect(x))
157+
Node(o.nodetype, o.tag, attrs, o.value, children)
158+
end
150159

151160
function Node(node::LazyNode)
152161
nodetype = node.nodetype
@@ -162,6 +171,9 @@ Node(data::Raw) = Node(LazyNode(data))
162171
# Anything that's not Vector{UInt8} or a (Lazy)Node is converted to a Text Node
163172
Node(x) = Node(Text, nothing, nothing, string(x), nothing)
164173

174+
h(tag::Union{Symbol, String}, children...; kw...) = Node(Element, tag, kw, nothing, children)
175+
Base.getproperty(::typeof(h), tag::Symbol) = h(tag)
176+
(o::Node)(children...; kw...) = Node(o, Node.(children)...; kw...)
165177

166178
# NOT in-place for Text Nodes
167179
function escape!(o::Node, warn::Bool=true)

test/runtests.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using XML
2-
using XML: Document, Element, Declaration, Comment, CData, DTD, ProcessingInstruction, Text, escape, unescape, OrderedDict
2+
using XML: Document, Element, Declaration, Comment, CData, DTD, ProcessingInstruction, Text, escape, unescape, OrderedDict, h
33
using Downloads: download
44
using Test
55
import AbstractTrees
@@ -15,6 +15,13 @@ simple_dtd = joinpath("data", "simple_dtd.xml")
1515

1616
all_files = [xml_xsd, kml_xsd, books_xml, example_kml, simple_dtd]
1717

18+
#-----------------------------------------------------------------------------# h
19+
@testset "h function" begin
20+
@test h.tag == XML.Element("tag")
21+
@test h.tag(id="id") == XML.Element("tag"; id="id")
22+
@test h.tag(1, 2, a="a", b="b") == XML.Element("tag", 1, 2; a="a", b="b")
23+
end
24+
1825
#-----------------------------------------------------------------------------# escaping/unescaping
1926
@testset "escaping/unescaping" begin
2027
s = "This > string < has & some \" special ' characters"

0 commit comments

Comments
 (0)