Skip to content

Commit 64956ee

Browse files
committed
more examples to README
1 parent 1a15d68 commit 64956ee

File tree

1 file changed

+70
-1
lines changed

1 file changed

+70
-1
lines changed

README.md

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ Pkg.add("LightXML")
1717

1818
**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.
1919

20+
2021
### Examples
2122

2223
The following examples show how you may use this package to accomplish common tasks.
2324

24-
##### Read an XML file
25+
#### Read an XML file
2526

2627
Suppose you have an XML file ``ex1.xml`` as below
2728

@@ -107,3 +108,71 @@ end
107108

108109
**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.
109110

111+
112+
#### Create an XML Document
113+
114+
This package allows you to construct an XML document programmatically. For example, to create an XML document as
115+
116+
```xml
117+
<?xml version="1.0" encoding="utf-8"?>
118+
<States>
119+
<State tag="MA">Massachusetts</State>
120+
<State tag="MA">Illinois</State>
121+
</States>
122+
```
123+
124+
You may write:
125+
126+
```julia
127+
# create an empty XML document
128+
xdoc = XMLDocument()
129+
130+
# create & attach a root node
131+
xroot = create_root(xdoc, "States")
132+
133+
# create the first child
134+
xs1 = new_child(xroot, "State")
135+
136+
# add the inner content
137+
add_text(xs1, "Massachusetts")
138+
139+
# set attribute
140+
set_attribute(xs1, "tag", "MA")
141+
142+
# likewise for the second child
143+
xs2 = new_child(xroot, "State")
144+
add_text(xs2, "Illinois")
145+
set_attribute(xs2, "tag", "MA")
146+
```
147+
148+
#### Export an XML file
149+
150+
With this package, you can easily export an XML file to a string or a file, or show it on the console, as
151+
152+
```julia
153+
# save to an XML file
154+
save_file(xdoc, "f1.xml")
155+
156+
# output to a string
157+
s = string(xdoc)
158+
159+
# print to the console (in a pretty format as in an XML file)
160+
print(xdoc)
161+
```
162+
163+
**Node:** the ``string`` and ``show`` functions are specialized for both ``XMLDocument`` and ``XMLElement``.
164+
165+
166+
167+
168+
169+
170+
171+
172+
173+
174+
175+
176+
177+
178+

0 commit comments

Comments
 (0)