Skip to content

Commit 0d987a8

Browse files
committed
add type & function list to readme
1 parent 64956ee commit 0d987a8

File tree

2 files changed

+84
-7
lines changed

2 files changed

+84
-7
lines changed

README.md

Lines changed: 82 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Like other Julia packages, you may checkout *LightXML* from METADATA repo, as
1515
Pkg.add("LightXML")
1616
```
1717

18-
**Node:** This package relies on the library *libxml2* to work, which is shipped with Mac OS X and many Linux systems. So this package may work out of the box. If not, you may check whether *libxml2* has been in your system and whether *libxml2.so* (for Linux) or *libxml2.dylib* (for Mac) is on your library search path.
18+
**Note:** This package relies on the library *libxml2* to work, which is shipped with Mac OS X and many Linux systems. So this package may work out of the box. If not, you may check whether *libxml2* has been in your system and whether *libxml2.so* (for Linux) or *libxml2.dylib* (for Mac) is on your library search path.
1919

2020

2121
### Examples
@@ -106,7 +106,7 @@ for a in attributes(e) # a is an instance of
106106
end
107107
```
108108

109-
**Node:** 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.
109+
**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.
110110

111111

112112
#### Create an XML Document
@@ -117,7 +117,7 @@ This package allows you to construct an XML document programmatically. For examp
117117
<?xml version="1.0" encoding="utf-8"?>
118118
<States>
119119
<State tag="MA">Massachusetts</State>
120-
<State tag="MA">Illinois</State>
120+
<State tag="IL">Illinois</State>
121121
</States>
122122
```
123123

@@ -142,7 +142,7 @@ set_attribute(xs1, "tag", "MA")
142142
# likewise for the second child
143143
xs2 = new_child(xroot, "State")
144144
add_text(xs2, "Illinois")
145-
set_attribute(xs2, "tag", "MA")
145+
set_attribute(xs2, "tag", "IL")
146146
```
147147

148148
#### Export an XML file
@@ -160,19 +160,96 @@ s = string(xdoc)
160160
print(xdoc)
161161
```
162162

163-
**Node:** the ``string`` and ``show`` functions are specialized for both ``XMLDocument`` and ``XMLElement``.
163+
**Note:** the ``string`` and ``show`` functions are specialized for both ``XMLDocument`` and ``XMLElement``.
164164

165165

166+
### Types
166167

168+
Main types of this package
167169

170+
* ``XMLDocument``: represent an XML document (in a tree)
171+
* ``XMLElement``: represent an XML element (``child_elements`` give you this)
172+
* ``XMLNode``: represent a generic XML node (``child_nodes`` give you this)
173+
* ``XMLAttr``: represent an XML attribute
168174

175+
Note that one if an ``XMLNode`` instance ``x`` is actually an element node, one may construct an ``XMLElement`` instance by ``XMLElement(x)``.
169176

170177

178+
### API Functions
171179

180+
A list of API functions:
172181

173182

183+
##### Functions to access an XML tree
174184

185+
```julia
186+
# Let xdoc be a document, x be a node/element, e be an element
187+
188+
root(xdoc) # get the root element of a document
189+
190+
nodetype(x) # get an integer indicating the node type
191+
name(x) # get the name of a node/element
192+
content(x) # get text content of a node/element
193+
# if x is an element, this returns all text (concatenated) within x
194+
195+
is_elementnode(x) # whether x is an element node
196+
is_textnode(x) # whether x is a text node
197+
is_cdatanode(x) # whether x is a CDATA node
198+
is_commentnode(x) # whether x is a comment node
199+
200+
has_children(e) # whether e has child nodes
201+
has_attributes(e) # whether e has attributes
202+
203+
child_nodes(x) # iterator of all child nodes of a node/element x
204+
child_elements(e) # iterator of all child elements of e
205+
attributes(e) # iterator of all attributes of e
206+
attribute(e, name) # get the value of a named attribute
207+
208+
find_element(e, name) # the first element of specified name under e
209+
# return nothing is no such an element is found
210+
211+
get_elements_by_tagname(e, name) # a list of all child elements of e with
212+
# the specified name
213+
214+
string(e) # Format an XML element into a string
215+
show(io, e) # output formatted XML element
216+
```
217+
218+
##### Functions to create an XML document
175219

220+
```julia
221+
xdoc = XMLDocument() # create an empty XML document
222+
223+
e = new_element(name) # create a new XML element
224+
# this does not attach e to a tree
225+
226+
t = new_textnode(content) # create a new text node
227+
# this does not attach t to a tree
176228

229+
set_root(xdoc, e) # set element e as the root of xdoc
230+
add_child(parent, x) # add x as a child of a parent element
177231

232+
e = create_root(xdoc, name) # create a root element and set it as root
233+
# equiv. to new_element + set_root
178234

235+
e = new_child(parent, name) # create a new element and add it as a child
236+
# equiv. to new_element + add_child
237+
238+
add_text(e, text) # add text content to an element
239+
# equiv. to new_textnode + add_child
240+
241+
set_attribute(e, name, value) # set an attribute of an element
242+
# this returns the added attribute
243+
# as an instance of XMLAttr
244+
```
245+
246+
##### Functions to work with a document
247+
248+
```julia
249+
xdoc = parse_file(filename) # parse an XML file
250+
xdoc = parse_string(str) # parse an XML doc from a string
251+
save_file(xdoc, filename) # save xdoc to an XML file
252+
253+
string(xdoc) # Formatted XML doc to a string
254+
show(io, xdoc) # output formatted XML document
255+
```

test/create.jl

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

1111
xs2 = new_child(xroot, "State")
1212
add_text(xs2, "Illinois")
13-
set_attribute(xs2, "tag", "MA")
13+
set_attribute(xs2, "tag", "IL")
1414

1515
rtxt = """
1616
<?xml version="1.0" encoding="utf-8"?>
1717
<States>
1818
<State tag="MA">Massachusetts</State>
19-
<State tag="MA">Illinois</State>
19+
<State tag="IL">Illinois</State>
2020
</States>
2121
"""
2222

0 commit comments

Comments
 (0)