You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+70-1Lines changed: 70 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,11 +17,12 @@ Pkg.add("LightXML")
17
17
18
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.
19
19
20
+
20
21
### Examples
21
22
22
23
The following examples show how you may use this package to accomplish common tasks.
23
24
24
-
#####Read an XML file
25
+
#### Read an XML file
25
26
26
27
Suppose you have an XML file ``ex1.xml`` as below
27
28
@@ -107,3 +108,71 @@ end
107
108
108
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
110
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
+
<Statetag="MA">Massachusetts</State>
120
+
<Statetag="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``.
0 commit comments