Skip to content

Commit e56b407

Browse files
committed
readme docs
1 parent 1b01acb commit e56b407

File tree

3 files changed

+265
-98
lines changed

3 files changed

+265
-98
lines changed

README.md

Lines changed: 112 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,66 +4,132 @@
44

55
<br><br>
66

7+
## Introduction
8+
9+
**XML.jl** offers both *lazy* and *eager* data structures for reading and writing XML files with a consistent interface:
10+
11+
- For `XML.RawData`, `XML.RowNode`, and `XML.Node`:
12+
- `nodetype(node) --> XML.NodeType` (See `?XML.NodeType` for details).
13+
- `tag(node) --> String or Nothing`
14+
- `attributes(node) --> OrderedDict{String,String} or Nothing`
15+
- `value(node) --> String or Nothing`
16+
- `children(node) --> Vector{typeof(node)}`
17+
- `depth(node) --> Int`
18+
- For `XML.RawData` and `XML.RowNode` only:
19+
- `next(node) --> typeof(node)`
20+
- `prev(node) --> typeof(node)`
21+
- `parent(node) --> typeof(node)`
22+
723
## Quickstart
824

925
```julia
1026
using XML
1127

12-
doc = XML.Document(joinpath(dirname(pathof(XML)), "..", "test", "books.xml"))
28+
filename = joinpath(dirname(pathof(XML)), "..", "test", "books.xml")
29+
30+
doc = XML.Node(filename)
31+
32+
children(doc)
33+
# 2-element Vector{Node}:
34+
# Node DECLARATION <?xml version="1.0">
35+
# Node ELEMENT <catalog> (12 children)
36+
37+
doc[end] # The root node
38+
# Node ELEMENT <catalog> (12 children)
39+
40+
doc[end][2] # Second child of root
41+
# Node ELEMENT <book id="bk102"> (6 children)
42+
```
43+
44+
## Data Structures
45+
46+
**`XML.Node`**
47+
- An eager data structure that loads the entire XML DOM in memory.
48+
- **This is what you should use to build an XML document programmatically.**
49+
- `Node`s have some additional methods that aid in construction/mutation:
50+
51+
```julia
52+
# Add a child:
53+
push!(parent::Node, child::Node)
54+
55+
# Replace a child:
56+
parent[2] = child
57+
58+
# Create a new node with an edited field. `kw...` can be one or more of:
59+
# - nodetype::NodeType
60+
# - tag (String or Nothing)
61+
# - attributes (OrderedDict{String,String} or Nothing)
62+
# - value (String or Nothing)
63+
# - children (Vector{Node} or nothing),
64+
# - depth::Int (this will be automatically set if not provided)
65+
Node(node; kw...)
66+
```
67+
68+
**`XML.RowNode`**
69+
- A data structure that can used as a *Tables.jl* source. It is only lazy in how it access its children.
70+
1371

14-
doc.prolog
15-
# <?xml version="1.0"?>
72+
**`XML.RawData`**
73+
- A super lazy data structure that holds the reference `Vector{UInt8}` data and the sta
1674

17-
doc.root
18-
# <catalog> (12 children)
75+
## Reading
1976

20-
# Use getindex/setindex! to get/set an Element's children
21-
doc.root[1]
22-
# <book id="bk101"> (6 children)
77+
```julia
78+
XML.RawData(filename)
79+
80+
RowNode(filename)
2381

24-
doc.root[1][1]
25-
# <author> (1 child)
82+
Node(filename)
83+
84+
# Parsing:
85+
parse(XML.RawData, str)
86+
parse(RowNode, str)
87+
parse(Node, str)
88+
```
89+
90+
## Writing
91+
92+
```julia
93+
XML.write(filename::String, node) # write to file
94+
95+
XML.write(io::IO, node) # write to stream
96+
97+
XML.write(node) # String
98+
```
99+
100+
## Iteration
101+
102+
```julia
103+
doc = XML.RowNode(filename)
26104

27-
# use getproperty/setproperty! to get/set an Element's attributes
105+
foreach(println, doc)
106+
# RowNode DECLARATION <?xml version="1.0">
107+
# RowNode ELEMENT <catalog> (12 children)
108+
# RowNode ELEMENT <book id="bk101"> (6 children)
109+
# RowNode ELEMENT <author> (1 child)
110+
# RowNode TEXT "Gambardella, Matthew"
111+
# RowNode ELEMENT <title> (1 child)
112+
#
28113

29-
doc.root.id = "A new attribute called `id`"
114+
# Use as Tables.jl source:
115+
using DataFrames
30116

31-
write("newfile.xml", doc)
117+
DataFrame(doc)
32118
```
33119

34-
### Types
120+
Note that you can also iterate through `XML.RawData`. However, *BEWARE* that this iterator
121+
has some non-node elements (e.g. just the closing tag of an element).
35122

36123
```julia
37-
# <!DOCTYPE $text>
38-
struct DTD <: AbstractXMLNode
39-
text::String
40-
end
41-
42-
# <?xml $attributes ?>
43-
mutable struct Declaration <: AbstractXMLNode
44-
tag::String
45-
attributes::OrderedDict{Symbol, String}
46-
end
47-
48-
# <![CDATA[$text]]>
49-
mutable struct CData <: AbstractXMLNode
50-
text::String
51-
end
52-
53-
# <!-- $text -->
54-
mutable struct Comment <: AbstractXMLNode
55-
text::String
56-
end
57-
58-
# <$tag $attributes>$children</$tag>
59-
mutable struct Element <: AbstractXMLNode
60-
tag::String
61-
attributes::OrderedDict{Symbol, String}
62-
children::Vector{Union{CData, Comment, Element, String}}
63-
end
64-
65-
mutable struct Document <: AbstractXMLNode
66-
prolog::Vector{Union{Comment, Declaration, DTD}}
67-
root::Element
68-
end
124+
data = XML.RawData(filename)
125+
126+
foreach(println, data)
127+
# 1: RAW_DECLARATION (pos=1, len=20): <?xml version="1.0"?>
128+
# 1: RAW_ELEMENT_OPEN (pos=23, len=8): <catalog>
129+
# 2: RAW_ELEMENT_OPEN (pos=36, len=16): <book id="bk101">
130+
# 3: RAW_ELEMENT_OPEN (pos=60, len=7): <author>
131+
# 4: RAW_TEXT (pos=68, len=19): Gambardella, Matthew
132+
# 3: RAW_ELEMENT_CLOSE (pos=88, len=8): </author> <------ !!! NOT A NODE !!!
133+
# 3: RAW_ELEMENT_OPEN (pos=104, len=6): <title>
134+
#
69135
```

0 commit comments

Comments
 (0)