Skip to content

Commit 11b3852

Browse files
committed
Merge pull request #13 from garborg/track04
Track Julia 0.4 syntax changes, minor bug and doc fixes
2 parents 154fefc + 177d561 commit 11b3852

File tree

16 files changed

+347
-358
lines changed

16 files changed

+347
-358
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
*.jl.cov
2+
*.jl.mem
13
ex1

README.md

Lines changed: 67 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
## LightXML.jl
22

3-
This package is a light-weight Julia wrapper of [Libxml2](http://www.xmlsoft.org), which provides a minimal interface that covers functionalities that are commonly needed:
3+
This package is a light-weight Julia wrapper of [libxml2](http://www.xmlsoft.org), which provides a minimal interface that covers functionalities that are commonly needed:
44

55
* Parse a XML file or string into a tree
66
* Access XML tree structure
7-
* Create an XML tree
7+
* Create an XML tree
88
* Export an XML tree to a string or an XML file
99

1010
### Setup
@@ -50,11 +50,11 @@ Here is the code to parse this file:
5050
using LightXML
5151

5252
# parse ex1.xml:
53-
# xdoc is an instance of XMLDocument, which maintains a tree structure
53+
# xdoc is an instance of XMLDocument, which maintains a tree structure
5454
xdoc = parse_file("ex1.xml")
5555

5656
# get the root element
57-
xroot = root(xdoc) # an instance of XMLElement
57+
xroot = root(xdoc) # an instance of XMLElement
5858
# print its name
5959
println(name(xroot)) # this should print: bookstore
6060

@@ -68,15 +68,15 @@ for c in child_nodes(xroot) # c is an instance of XMLNode
6868
end
6969
```
7070

71-
There are actually five child nodes under ``<bookstore>``: the 1st, 3rd, 5th children are text nodes (any space between node elements are captured by text nodes), while the 2nd and 4th nodes are element nodes corresponding to the ``<book>`` elements.
71+
There are actually five child nodes under ``<bookstore>``: the 1st, 3rd, 5th children are text nodes (any space between node elements are captured by text nodes), while the 2nd and 4th nodes are element nodes corresponding to the ``<book>`` elements.
7272

7373
One may use the function ``nodetype`` to determine the type of a node, which returns an integer following the table [here](http://www.w3schools.com/dom/dom_nodetype.asp). In particular, 1 indicates element node and 3 indicates text node.
7474

75-
If you only care about child elements, you may use ``child_elements`` instead of ``child_nodes``.
75+
If you only care about child elements, you may use ``child_elements`` instead of ``child_nodes``.
7676

7777
```julia
7878
ces = collect(child_elements(xroot)) # get a list of all child elements
79-
@assert length(ces) == 2
79+
@assert length(ces) == 2
8080

8181
# if you know the child element tagname, you can instead get a list as
8282
ces = get_elements_by_tagname(xroot, "book")
@@ -90,33 +90,33 @@ println(attribute(e1, "category"))
9090
t = find_element(e1, "title")
9191

9292
# retrieve the value of lang attribute of t
93-
a = attribute(t, "lang") # a <- "en"
93+
a = attribute(t, "lang") # a <- "en"
9494

9595
# retrieve the text content of t
9696
r = content(t) # r <- "Everyday Italian"
9797
```
9898

99-
One can also traverse all attributes of an element ``e`` as
99+
One can also traverse all attributes of an element (``e1``) as
100100

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

109109
Another way to access attributes is to turn them into a dictionary using ``attributes_dict``, as
110110

111111
```julia
112-
ad = attributes_dict(e1)
113-
v = ad["lang"] # v <-- "en"
112+
ad = attributes_dict(e1)
113+
v = ad["category"] # v <-- "COOKING"
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.
117117

118118

119-
#### Create an XML Document
119+
#### Create an XML Document
120120

121121
This package allows you to construct an XML document programmatically. For example, to create an XML document as
122122

@@ -153,7 +153,7 @@ add_text(xs2, "Illinois")
153153
# set multiple attributes using a dict
154154
set_attributes(xs2, {"tag"=>"IL", "cap"=>"Springfield"})
155155

156-
# now, the thrid child
156+
# now, the third child
157157
xs3 = new_child(xroot, "State")
158158
add_text(xs3, "California")
159159
# set attributes using keyword arguments
@@ -166,16 +166,16 @@ With this package, you can easily export an XML file to a string or a file, or s
166166

167167
```julia
168168
# save to an XML file
169-
save_file(xdoc, "f1.xml")
169+
save_file(xdoc, "f1.xml")
170170

171171
# output to a string
172172
s = string(xdoc)
173173

174174
# print to the console (in a pretty format as in an XML file)
175-
print(xdoc)
175+
print(xdoc)
176176
```
177177

178-
**Note:** the ``string`` and ``show`` functions are specialized for both ``XMLDocument`` and ``XMLElement``.
178+
**Note:** the ``string`` and ``show`` functions are specialized for both ``XMLDocument`` and ``XMLElement``.
179179

180180

181181
### Types
@@ -187,7 +187,7 @@ Main types of this package
187187
* ``XMLNode``: represent a generic XML node (``child_nodes`` give you this)
188188
* ``XMLAttr``: represent an XML attribute
189189

190-
Note that one if an ``XMLNode`` instance ``x`` is actually an element node, one may construct an ``XMLElement`` instance by ``XMLElement(x)``.
190+
**Note:** If an ``XMLNode`` instance ``x`` is actually an element node, one may construct an ``XMLElement`` instance by ``XMLElement(x)``.
191191

192192

193193
### API Functions
@@ -200,91 +200,91 @@ A list of API functions:
200200
```julia
201201
# Let xdoc be a document, x be a node/element, e be an element
202202

203-
root(xdoc) # get the root element of a document
203+
root(xdoc) # get the root element of a document
204204

205-
nodetype(x) # get an integer indicating the node type
206-
name(x) # get the name of a node/element
207-
content(x) # get text content of a node/element
208-
# if x is an element, this returns all text (concatenated) within x
205+
nodetype(x) # get an integer indicating the node type
206+
name(x) # get the name of a node/element
207+
content(x) # get text content of a node/element
208+
# if x is an element, this returns all text (concatenated) within x
209209

210-
is_elementnode(x) # whether x is an element node
211-
is_textnode(x) # whether x is a text node
212-
is_cdatanode(x) # whether x is a CDATA node
213-
is_commentnode(x) # whether x is a comment node
210+
is_elementnode(x) # whether x is an element node
211+
is_textnode(x) # whether x is a text node
212+
is_cdatanode(x) # whether x is a CDATA node
213+
is_commentnode(x) # whether x is a comment node
214214

215-
has_children(e) # whether e has child nodes
216-
has_attributes(e) # whether e has attributes
215+
has_children(e) # whether e has child nodes
216+
has_attributes(e) # whether e has attributes
217217

218-
child_nodes(x) # iterator of all child nodes of a node/element x
219-
child_elements(e) # iterator of all child elements of e
220-
attributes(e) # iterator of all attributes of e
218+
child_nodes(x) # iterator of all child nodes of a node/element x
219+
child_elements(e) # iterator of all child elements of e
220+
attributes(e) # iterator of all attributes of e
221221

222-
attributes_dict(e) # a dictionary of all attributes of e,
223-
# which maps names to corresponding values
222+
attributes_dict(e) # a dictionary of all attributes of e,
223+
# which maps names to corresponding values
224224

225225
has_attribute(e, name) # whether a named attribute exists for e
226226

227227
# get the value of a named attribute
228-
# when the attribute does not exist, it either
228+
# when the attribute does not exist, it either
229229
# throws an exception (when required is true)
230230
# or returns nothing (when required is false)
231-
attribute(e, name; required=false)
231+
attribute(e, name; required=false)
232232

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

236236
get_elements_by_tagname(e, name) # a list of all child elements of e with
237-
# the specified name
237+
# the specified name
238238

239-
string(e) # Format an XML element into a string
240-
show(io, e) # output formatted XML element
239+
string(e) # format an XML element into a string
240+
show(io, e) # output formatted XML element
241241
```
242242

243243
##### Functions to create an XML document
244244

245245
```julia
246-
xdoc = XMLDocument() # create an empty XML document
246+
xdoc = XMLDocument() # create an empty XML document
247247

248-
e = new_element(name) # create a new XML element
249-
# this does not attach e to a tree
248+
e = new_element(name) # create a new XML element
249+
# this does not attach e to a tree
250250

251-
t = new_textnode(content) # create a new text node
252-
# this does not attach t to a tree
251+
t = new_textnode(content) # create a new text node
252+
# this does not attach t to a tree
253253

254-
set_root(xdoc, e) # set element e as the root of xdoc
255-
add_child(parent, x) # add x as a child of a parent element
254+
set_root(xdoc, e) # set element e as the root of xdoc
255+
add_child(parent, x) # add x as a child of a parent element
256256

257-
e = create_root(xdoc, name) # create a root element and set it as root
258-
# equiv. to new_element + set_root
257+
e = create_root(xdoc, name) # create a root element and set it as root
258+
# equiv. to new_element + set_root
259259

260-
e = new_child(parent, name) # create a new element and add it as a child
261-
# equiv. to new_element + add_child
260+
e = new_child(parent, name) # create a new element and add it as a child
261+
# equiv. to new_element + add_child
262262

263-
add_text(e, text) # add text content to an element
264-
# equiv. to new_textnode + add_child
263+
add_text(e, text) # add text content to an element
264+
# equiv. to new_textnode + add_child
265265

266-
add_cdata(xdoc, e, text) # add cdata content to an element
267-
# equiv. to new_cdatanode + add_child
266+
add_cdata(xdoc, e, text) # add cdata content to an element
267+
# equiv. to new_cdatanode + add_child
268268

269269
set_attribute(e, name, value) # set an attribute of an element
270-
# this returns the added attribute
270+
# this returns the added attribute
271271
# as an instance of XMLAttr
272272

273-
set_attributes(e, attrs) # set multiple attributes in one call
274-
# attrs can be a dictionary or
275-
# a list of pairs as (name, value)
273+
set_attributes(e, attrs) # set multiple attributes in one call
274+
# attrs can be a dictionary or
275+
# a list of pairs as (name, value)
276276

277277
# one can also use keyword arguments to set attributes to an element
278-
set_attributes(e, key1="val1", key2="val2", ...)
278+
set_attributes(e, key1="val1", key2="val2", ...)
279279
```
280280

281281
##### Functions to work with a document
282282

283283
```julia
284-
xdoc = parse_file(filename) # parse an XML file
285-
xdoc = parse_string(str) # parse an XML doc from a string
286-
save_file(xdoc, filename) # save xdoc to an XML file
284+
xdoc = parse_file(filename) # parse an XML file
285+
xdoc = parse_string(str) # parse an XML doc from a string
286+
save_file(xdoc, filename) # save xdoc to an XML file
287287

288-
string(xdoc) # Formatted XML doc to a string
289-
show(io, xdoc) # output formatted XML document
288+
string(xdoc) # formatted XML doc to a string
289+
show(io, xdoc) # output formatted XML document
290290
```

REQUIRE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
julia 0.2-
1+
julia 0.3-
22
@windows BinDeps
33
@windows WinRPM

src/LightXML.jl

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
module LightXML
22

3-
export
4-
5-
# common
6-
name, free,
7-
8-
# nodes
9-
AbstractXMLNode,
10-
XMLAttr, XMLAttrIter, XMLNode, XMLNodeIter, XMLElement, XMLElementIter,
11-
nodetype, value, content, attribute, has_attribute,
12-
is_elementnode, is_textnode, is_commentnode, is_cdatanode, is_blanknode,
13-
child_nodes, has_children, attributes, has_attributes, attributes_dict,
14-
child_elements, find_element, get_elements_by_tagname,
15-
16-
new_element, add_child, new_child, new_textnode, add_text, add_cdata,
17-
set_attribute, set_attributes,
18-
19-
# document
20-
XMLDocument, version, encoding, compression, standalone, root,
21-
parse_file, parse_string, save_file, set_root, create_root
22-
23-
24-
include("clib.jl")
25-
include("errors.jl")
26-
27-
include("utils.jl")
28-
include("nodes.jl")
29-
include("document.jl")
30-
include("cdata.jl")
3+
export
4+
5+
# common
6+
name, free,
7+
8+
# nodes
9+
AbstractXMLNode,
10+
XMLAttr, XMLAttrIter, XMLNode, XMLNodeIter, XMLElement, XMLElementIter,
11+
nodetype, value, content, attribute, has_attribute,
12+
is_elementnode, is_textnode, is_commentnode, is_cdatanode, is_blanknode,
13+
child_nodes, has_children, attributes, has_attributes, attributes_dict,
14+
child_elements, find_element, get_elements_by_tagname,
15+
16+
new_element, add_child, new_child, new_textnode, add_text, add_cdata,
17+
set_attribute, set_attributes,
18+
19+
# document
20+
XMLDocument, version, encoding, compression, standalone, root,
21+
parse_file, parse_string, save_file, set_root, create_root
22+
23+
24+
include("clib.jl")
25+
include("errors.jl")
26+
27+
include("utils.jl")
28+
include("nodes.jl")
29+
include("document.jl")
30+
include("cdata.jl")
3131
end

src/clib.jl

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
@windows_only const libxml2 = dlopen(Pkg.dir("WinRPM","deps","usr","$(Sys.ARCH)-w64-mingw32","sys-root","mingw","bin","libxml2-2"))
55

66
macro lx2func(fname) # the macro to get functions from libxml2
7-
quote
8-
$(esc(fname)) = dlsym( libxml2, ($(string(fname))) )
9-
end
7+
quote
8+
$(esc(fname)) = dlsym( libxml2, ($(string(fname))) )
9+
end
1010
end
1111

1212
const nullptr = convert(Ptr{Void}, 0)
@@ -21,11 +21,11 @@ typealias Xptr Ptr{Void}
2121
# supporting functions
2222

2323
#
24-
# After tests, it seems that free in libc instead of xmlFree
24+
# After tests, it seems that free in libc instead of xmlFree
2525
# should be used here
2626
#
2727
@lx2func xmlFree
28-
_xmlfree{T}(p::Ptr{T}) = ccall(:free, Void, (Ptr{T},), p)
28+
_xmlfree{T}(p::Ptr{T}) = ccall(:free, Void, (Ptr{T},), p)
2929

3030
# pre-condition: p is not null
3131
_xcopystr(p::Xstr) = (r = bytestring(p); _xmlfree(p); r)
@@ -65,4 +65,3 @@ _xcopystr(p::Xstr) = (r = bytestring(p); _xmlfree(p); r)
6565
@lx2func xmlDocDumpFormatMemoryEnc
6666
@lx2func xmlSaveFileEnc
6767
@lx2func xmlSaveFormatFileEnc
68-

0 commit comments

Comments
 (0)