Skip to content

Commit 417c5da

Browse files
committed
add has_attribute and the option required for attribute
1 parent 8bdd8d2 commit 417c5da

File tree

5 files changed

+43
-6
lines changed

5 files changed

+43
-6
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,14 @@ has_attributes(e) # whether e has attributes
203203
child_nodes(x) # iterator of all child nodes of a node/element x
204204
child_elements(e) # iterator of all child elements of e
205205
attributes(e) # iterator of all attributes of e
206-
attribute(e, name) # get the value of a named attribute
206+
207+
has_attribute(e, name) # whether a named attribute exists for e
208+
209+
# get the value of a named attribute
210+
# when the attribute does not exist, it either
211+
# throws an exception (when required is true)
212+
# or returns nothing (when required is false)
213+
attribute(e, name; required=false)
207214

208215
find_element(e, name) # the first element of specified name under e
209216
# return nothing is no such an element is found

src/LightXML.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ module LightXML
88
# nodes
99
AbstractXMLNode,
1010
XMLAttr, XMLAttrIter, XMLNode, XMLNodeIter, XMLElement, XMLElementIter,
11-
nodetype, value, content, attribute,
12-
is_elementnode, is_textnode, is_commentnode, is_cdatanode,
11+
nodetype, value, content, attribute, has_attribute,
12+
is_elementnode, is_textnode, is_commentnode, is_cdatanode, is_blanknode,
1313
child_nodes, has_children, attributes, has_attributes, child_elements,
1414
find_element, get_elements_by_tagname,
1515

src/clib.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ _xcopystr(p::Xstr) = (r = bytestring(p); _xmlfree(p); r)
3939
# functions for nodes
4040

4141
@lx2func xmlNodeGetContent
42+
@lx2func xmlHasProp
4243
@lx2func xmlGetProp
4344
@lx2func xmlFirstElementChild
4445
@lx2func xmlNextElementSibling
4546
@lx2func xmlNodeDump
47+
@lx2func xmlIsBlankNode
4648

4749
@lx2func xmlNewNode
4850
@lx2func xmlAddChild

src/nodes.jl

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ name(nd::XMLNode) = bytestring(nd._struct.name)
142142
nodetype(nd::XMLNode) = nd._struct.nodetype
143143
has_children(nd::XMLNode) = (nd._struct.children != nullptr)
144144

145+
# whether it is a white-space only text node
146+
is_blanknode(nd::XMLNode) = bool(ccall(xmlIsBlankNode, Cint, (Xptr,), nd.ptr))
147+
148+
145149
# iteration over children
146150

147151
immutable XMLNodeIter
@@ -207,10 +211,22 @@ Base.show(io::IO, x::XMLElement) = Base.show(x.node)
207211

208212
# attribute access
209213

210-
function attribute(x::XMLElement, name::ASCIIString)
214+
function attribute(x::XMLElement, name::ASCIIString; required::Bool=false)
211215
pv = ccall(xmlGetProp, Xstr, (Xptr, Xstr), x.node.ptr, name)
212-
pv != nullptr || throw(XMLAttributeNotFound())
213-
_xcopystr(pv)
216+
if pv != nullptr
217+
return _xcopystr(pv)
218+
else
219+
if required
220+
throw(XMLAttributeNotFound())
221+
else
222+
return nothing
223+
end
224+
end
225+
end
226+
227+
function has_attribute(x::XMLElement, name::ASCIIString)
228+
p = ccall(xmlHasProp, Xptr, (Xptr, Xstr), x.node.ptr, name)
229+
return p != nullptr
214230
end
215231

216232
has_attributes(x::XMLElement) = (x.node._struct.attrs != nullptr)

test/parse.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,16 @@ rcs = collect(child_nodes(xroot))
5252
@test is_textnode(rcs[3])
5353
@test is_textnode(rcs[5])
5454

55+
@test is_blanknode(rcs[1])
56+
@test is_blanknode(rcs[3])
57+
@test is_blanknode(rcs[5])
58+
5559
@test is_elementnode(rcs[2])
5660
@test is_elementnode(rcs[4])
5761

62+
@test !is_blanknode(rcs[2])
63+
@test !is_blanknode(rcs[4])
64+
5865
xb1 = XMLElement(rcs[2])
5966

6067
@test name(xb1) == "book"
@@ -83,8 +90,13 @@ xb2 = XMLElement(rcs[4])
8390
@test nodetype(xb2) == 1
8491
@test has_attributes(xb2)
8592
@test has_children(xb2)
93+
@test has_attribute(xb2, "category")
8694
@test attribute(xb2, "category") == "CHILDREN"
8795

96+
@test !has_attribute(xb2, "wrongattr")
97+
@test is(attribute(xb2, "wrongattr"), nothing)
98+
@test_throws attribute(xb2, "wrongattr"; required=true)
99+
88100
rces = get_elements_by_tagname(xroot, "book")
89101
@test length(rces) == 2
90102
@test isa(rces, Vector{XMLElement})

0 commit comments

Comments
 (0)