Skip to content

Commit 1a15d68

Browse files
committed
add xml parsing part to README
1 parent 3c974f1 commit 1a15d68

File tree

1 file changed

+101
-2
lines changed

1 file changed

+101
-2
lines changed

README.md

Lines changed: 101 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
LightXML.jl
2-
============
1+
## LightXML.jl
32

43
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:
54

@@ -8,3 +7,103 @@ This package is a light-weight Julia wrapper of [Libxml2](http://www.xmlsoft.org
87
* Create an XML tree
98
* Export an XML tree to a string or an XML file
109

10+
### Setup
11+
12+
Like other Julia packages, you may checkout *LightXML* from METADATA repo, as
13+
14+
```julia
15+
Pkg.add("LightXML")
16+
```
17+
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+
20+
### Examples
21+
22+
The following examples show how you may use this package to accomplish common tasks.
23+
24+
##### Read an XML file
25+
26+
Suppose you have an XML file ``ex1.xml`` as below
27+
28+
```xml
29+
<?xml version="1.0" encoding="UTF-8"?>
30+
<bookstore>
31+
<book category="COOKING" tag="first">
32+
<title lang="en">Everyday Italian</title>
33+
<author>Giada De Laurentiis</author>
34+
<year>2005</year>
35+
<price>30.00</price>
36+
</book>
37+
<book category="CHILDREN">
38+
<title lang="en">Harry Potter</title>
39+
<author>J K. Rowling</author>
40+
<year>2005</year>
41+
<price>29.99</price>
42+
</book>
43+
</bookstore>
44+
```
45+
46+
Here is the code to parse this file:
47+
48+
```julia
49+
using MiniXML
50+
51+
# parse ex1.xml:
52+
# xdoc is an instance of XMLDocument, which maintains a tree structure
53+
xdoc = parse_file("ex1.xml")
54+
55+
# get the root element
56+
xroot = root(xdoc) # an instance of XMLElement
57+
# print its name
58+
println(name(xroot)) # this should print: bookstore
59+
60+
# traverse all its child nodes and print element names
61+
for c in child_nodes(xroot) # c is an instance of XMLNode
62+
println(nodetype(c))
63+
if is_elementnode(c)
64+
e = XMLElement(c) # this makes an XMLElement instance
65+
println(name(e))
66+
end
67+
end
68+
```
69+
70+
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+
72+
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.
73+
74+
If you only care about child elements, you may use ``child_elements`` instead of ``child_nodes``.
75+
76+
```julia
77+
ces = collect(child_elements(xroot)) # get a list of all child elements
78+
@assert length(ces) == 2
79+
80+
# if you know the child element tagname, you can instead get a list as
81+
ces = get_elements_by_tagname(xroot, "book")
82+
83+
e1 = ces[1] # the first book element
84+
85+
# print the value of an attribute
86+
println(attribute(e1, "category"))
87+
88+
# find the first title element under e1
89+
t = find_element(e1, "title")
90+
91+
# retrieve the value of lang attribute of t
92+
a = attribute(t, "lang") # a <- "en"
93+
94+
# retrieve the text content of t
95+
r = content(t) # r <- "Everyday Italian"
96+
```
97+
98+
One can also traverse all attributes of an element ``e`` as
99+
100+
```julia
101+
for a in attributes(e) # a is an instance of
102+
n = name(a)
103+
v = value(a)
104+
println("$n = $v")
105+
end
106+
```
107+
108+
**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+

0 commit comments

Comments
 (0)