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
+67-67Lines changed: 67 additions & 67 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,10 @@
1
1
## LightXML.jl
2
2
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:
4
4
5
5
* Parse a XML file or string into a tree
6
6
* Access XML tree structure
7
-
* Create an XML tree
7
+
* Create an XML tree
8
8
* Export an XML tree to a string or an XML file
9
9
10
10
### Setup
@@ -50,11 +50,11 @@ Here is the code to parse this file:
50
50
using LightXML
51
51
52
52
# 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
54
54
xdoc =parse_file("ex1.xml")
55
55
56
56
# get the root element
57
-
xroot =root(xdoc) # an instance of XMLElement
57
+
xroot =root(xdoc) # an instance of XMLElement
58
58
# print its name
59
59
println(name(xroot)) # this should print: bookstore
60
60
@@ -68,15 +68,15 @@ for c in child_nodes(xroot) # c is an instance of XMLNode
68
68
end
69
69
```
70
70
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.
72
72
73
73
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.
74
74
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``.
76
76
77
77
```julia
78
78
ces =collect(child_elements(xroot)) # get a list of all child elements
79
-
@assertlength(ces) ==2
79
+
@assertlength(ces) ==2
80
80
81
81
# if you know the child element tagname, you can instead get a list as
One can also traverse all attributes of an element ``e`` as
99
+
One can also traverse all attributes of an element (``e1``) as
100
100
101
101
```julia
102
-
for a inattributes(e) # a is an instance of XMLAttr
103
-
n =name(a)
104
-
v =value(a)
105
-
println("$n = $v")
102
+
for a inattributes(e1) # a is an instance of XMLAttr
103
+
n =name(a)
104
+
v =value(a)
105
+
println("$n = $v")
106
106
end
107
107
```
108
108
109
109
Another way to access attributes is to turn them into a dictionary using ``attributes_dict``, as
110
110
111
111
```julia
112
-
ad =attributes_dict(e1)
113
-
v = ad["lang"] # v <-- "en"
112
+
ad =attributes_dict(e1)
113
+
v = ad["category"] # v <-- "COOKING"
114
114
```
115
115
116
116
**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.
117
117
118
118
119
-
#### Create an XML Document
119
+
#### Create an XML Document
120
120
121
121
This package allows you to construct an XML document programmatically. For example, to create an XML document as
0 commit comments