-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathprimates.jl
executable file
·39 lines (34 loc) · 1.01 KB
/
primates.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env julia
# Load the package.
using EzXML
# Parse an XML string
# (use `readxml(<filename>)` to read a document from a file).
doc = parsexml("""
<primates>
<genus name="Homo">
<species name="sapiens">Human</species>
</genus>
<genus name="Pan">
<species name="paniscus">Bonobo</species>
<species name="troglodytes">Chimpanzee</species>
</genus>
</primates>
""")
# Get the root element from `doc`.
primates = root(doc) # or `doc.root`
# Iterate over child elements.
for genus in eachelement(primates)
# Get an attribute value by name.
genus_name = genus["name"]
println("- ", genus_name)
for species in eachelement(genus)
# Get the content within an element.
species_name = nodecontent(species) # or `species.content`
println(" └ ", species["name"], " (", species_name, ")")
end
end
println()
# Find texts using XPath query.
for species_name in nodecontent.(findall("//species/text()", primates))
println("- ", species_name)
end