Skip to content

Commit f858ddb

Browse files
committed
add attributes_dict
1 parent 417c5da commit f858ddb

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,20 @@ r = content(t) # r <- "Everyday Italian"
9999
One can also traverse all attributes of an element ``e`` as
100100

101101
```julia
102-
for a in attributes(e) # a is an instance of
102+
for a in attributes(e) # a is an instance of XMLAttr
103103
n = name(a)
104104
v = value(a)
105105
println("$n = $v")
106106
end
107107
```
108108

109+
Another way to access attributes is to turn them into a dictionary using ``attributes_dict``, as
110+
111+
```julia
112+
ad = attributes_dict(e1)
113+
v = (ad["lang"]) # v <-- "en"
114+
```
115+
109116
**Note:** The functions ``child_nodes``, ``child_elements``, and ``attributes`` return light weight iterators -- so that one can use them with for-loop. To get an array of all items, one may use the ``collect`` function provided by Julia.
110117

111118

@@ -204,6 +211,9 @@ child_nodes(x) # iterator of all child nodes of a node/element x
204211
child_elements(e) # iterator of all child elements of e
205212
attributes(e) # iterator of all attributes of e
206213

214+
attributes_dict(e) # a dictionary of all attributes of e,
215+
# which maps names to corresponding values
216+
207217
has_attribute(e, name) # whether a named attribute exists for e
208218

209219
# get the value of a named attribute

src/LightXML.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ module LightXML
1010
XMLAttr, XMLAttrIter, XMLNode, XMLNodeIter, XMLElement, XMLElementIter,
1111
nodetype, value, content, attribute, has_attribute,
1212
is_elementnode, is_textnode, is_commentnode, is_cdatanode, is_blanknode,
13-
child_nodes, has_children, attributes, has_attributes, child_elements,
14-
find_element, get_elements_by_tagname,
13+
child_nodes, has_children, attributes, has_attributes, attributes_dict,
14+
child_elements, find_element, get_elements_by_tagname,
1515

1616
new_node, add_child, new_child, add_text, set_attribute,
1717

src/nodes.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,19 @@ end
232232
has_attributes(x::XMLElement) = (x.node._struct.attrs != nullptr)
233233
attributes(x::XMLElement) = XMLAttrIter(x.node._struct.attrs)
234234

235+
function attributes_dict(x::XMLElement)
236+
# make an dictionary based on attributes
237+
238+
dct = (ASCIIString=>ASCIIString)[]
239+
if has_attributes(x)
240+
for a in attributes(x)
241+
dct[name(a)] = value(a)
242+
end
243+
end
244+
return dct
245+
end
246+
247+
235248
# element access
236249

237250
immutable XMLElementIter

test/parse.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ b1a2 = b1as[2]
8484
@test name(b1a2) == "tag"
8585
@test value(b1a2) == "first"
8686

87+
adct = attributes_dict(xb1)
88+
@test length(adct) == 2
89+
@test adct["category"] == "COOKING"
90+
@test adct["tag"] == "first"
91+
8792
xb2 = XMLElement(rcs[4])
8893

8994
@test name(xb2) == "book"

0 commit comments

Comments
 (0)