Skip to content

Commit 5872c3a

Browse files
committed
add set_attributes to allow setting multiple attributes in various ways
1 parent f858ddb commit 5872c3a

File tree

4 files changed

+39
-6
lines changed

4 files changed

+39
-6
lines changed

README.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ Another way to access attributes is to turn them into a dictionary using ``attri
110110

111111
```julia
112112
ad = attributes_dict(e1)
113-
v = (ad["lang"]) # v <-- "en"
113+
v = ad["lang"] # v <-- "en"
114114
```
115115

116116
**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.
@@ -124,7 +124,8 @@ This package allows you to construct an XML document programmatically. For examp
124124
<?xml version="1.0" encoding="utf-8"?>
125125
<States>
126126
<State tag="MA">Massachusetts</State>
127-
<State tag="IL">Illinois</State>
127+
<State tag="IL" cap="Springfield">Illinois</State>
128+
<State tag="CA" cap="Sacramento">California</State>
128129
</States>
129130
```
130131

@@ -149,7 +150,14 @@ set_attribute(xs1, "tag", "MA")
149150
# likewise for the second child
150151
xs2 = new_child(xroot, "State")
151152
add_text(xs2, "Illinois")
152-
set_attribute(xs2, "tag", "IL")
153+
# set multiple attributes using a dict
154+
set_attributes(xs2, {"tag"=>"IL", "cap"=>"Springfield"})
155+
156+
# now, the thrid child
157+
xs3 = new_child(xroot, "State")
158+
add_text(xs3, "California")
159+
# set attributes using keyword arguments
160+
set_attributes(xs3; tag="CA", cap="Sacramento")
153161
```
154162

155163
#### Export an XML file
@@ -258,6 +266,13 @@ add_text(e, text) # add text content to an element
258266
set_attribute(e, name, value) # set an attribute of an element
259267
# this returns the added attribute
260268
# as an instance of XMLAttr
269+
270+
set_attributes(e, attrs) # set multiple attributes in one call
271+
# attrs can be a dictionary or
272+
# a list of pairs as (name, value)
273+
274+
# one can also use keyword arguments to set attributes to an element
275+
set_attributes(e, key1="val1", key2="val2", ...)
261276
```
262277

263278
##### Functions to work with a document

src/LightXML.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module LightXML
1313
child_nodes, has_children, attributes, has_attributes, attributes_dict,
1414
child_elements, find_element, get_elements_by_tagname,
1515

16-
new_node, add_child, new_child, add_text, set_attribute,
16+
new_node, add_child, new_child, add_text, set_attribute, set_attributes,
1717

1818
# document
1919
XMLDocument, version, encoding, compression, standalone, root,

src/nodes.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,4 +315,17 @@ function set_attribute(x::XMLElement, name::ASCIIString, val::ASCIIString)
315315
return XMLAttr(a)
316316
end
317317

318+
function set_attributes{P<:NTuple{2}}(x::XMLElement, attrs::AbstractArray{P})
319+
for (nam, val) in attrs
320+
set_attribute(x, string(nam), string(val))
321+
end
322+
end
323+
324+
set_attributes(x::XMLElement, attrs::Associative) = set_attributes(x, collect(attrs))
325+
326+
function set_attributes(x::XMLElement; attrs...)
327+
for (nam, val) in attrs
328+
set_attribute(x, string(nam), string(val))
329+
end
330+
end
318331

test/create.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,18 @@ set_attribute(xs1, "tag", "MA")
1010

1111
xs2 = new_child(xroot, "State")
1212
add_text(xs2, "Illinois")
13-
set_attribute(xs2, "tag", "IL")
13+
set_attributes(xs2, {"tag"=>"IL", "cap"=>"Springfield"})
14+
15+
xs3 = new_child(xroot, "State")
16+
add_text(xs3, "California")
17+
set_attributes(xs3; tag="CA", cap="Sacramento")
1418

1519
rtxt = """
1620
<?xml version="1.0" encoding="utf-8"?>
1721
<States>
1822
<State tag="MA">Massachusetts</State>
19-
<State tag="IL">Illinois</State>
23+
<State tag="IL" cap="Springfield">Illinois</State>
24+
<State tag="CA" cap="Sacramento">California</State>
2025
</States>
2126
"""
2227

0 commit comments

Comments
 (0)