Upload
others
View
22
Download
0
Embed Size (px)
Citation preview
2
•Tw
o i
mp
ort
an
t is
sue
s fa
ced
by
ap
pli
cati
on
s se
cure
ly e
xch
an
gin
g d
ata
on
th
e w
eb
in
clu
de
da
ta s
ize
an
d t
ran
spa
ren
cy.
•S
ize
is
cle
arl
y i
mp
ort
an
t b
eca
use
sm
all
da
ta p
ack
ets
re
qu
ire
le
ss
tra
nsm
issi
on
tim
e t
ha
n e
qu
iva
len
t la
rge
r ve
rsio
ns
of
the
sa
me
da
ta.
•Tr
an
spa
ren
cy g
ive
s d
ata
a h
um
an
-re
ad
ab
le q
ua
lity
th
at
all
ow
s
de
velo
pe
rs t
o b
ett
er
un
de
rsta
nd
th
e n
atu
re o
f th
eir
da
ta a
n c
rea
te
po
rta
ble
cro
ss-p
latf
orm
so
luti
on
s m
ore
ra
pid
ly.
•In
th
is l
ess
on
we
wil
l d
iscu
ss X
ML
an
d J
SO
N,
two
pu
bli
c-d
om
ain
stra
teg
ies
com
mo
nly
use
d f
or
en
cod
ing
da
ta t
o b
e e
xch
an
ge
d o
n a
cro
ss-p
latf
orm
en
vir
on
me
nt.
Pro
cess
ing
XM
L a
nd
JS
ON
En
cod
ed
Da
ta
Wh
y I
nte
rne
t d
ata
is
en
cod
ed
?
3
Wh
at
is X
ML?
•
Ex
ten
sib
le M
ark
up
La
ng
ua
ge
(X
ML)
is
a s
et
of
rule
s e
sta
bli
she
d b
y
the
W3
C o
rga
niz
ati
on
. T
he
ru
les
pro
vid
e a
fra
me
wo
rk f
or
un
ifo
rmly
en
cod
ing
do
cum
en
ts i
n a
hu
ma
n r
ea
da
ble
fo
rm.
•X
ML
is s
imil
ar
to H
TM
L b
ut
all
th
e <
tag
s>
are
use
r-d
efi
ne
d.
Exa
mp
le:
<?xml version='1.0' encoding='UTF-8'?>
<time golfcourse="Augusta Ntl" tournament="The Masters" >
<hour> 21 </hour>
<minutes> 25 </minutes>
<seconds> 45 </seconds>
<zone> UTC−05:00 </zone>
</time>
Re
ad
ing
XM
L D
ata
Wh
at
is X
ML
da
ta ?
4
•T
he
ma
in r
ole
of
XM
L is
to
fa
cili
tate
a t
ran
spa
ren
t e
xch
an
ge
of
da
ta
ove
r th
e I
nte
rne
t.
•E
xam
ple
of
tech
no
log
ies
usi
ng
XM
L in
clu
de
: R
SS
, A
tom
, S
OA
P,
XH
TM
L, K
ML,
Xq
ue
ry,
Xp
ath
, O
pe
nD
ocu
me
nt,
Op
en
Ma
th,
etc
.(se
e
htt
p:/
/en
.wik
ipe
dia
.org
/wik
i/Li
st_
of_
XM
L_m
ark
up
_la
ng
ua
ge
s)
•S
eve
ral
do
cum
en
t m
an
ag
em
en
t p
rod
uct
ivit
y t
oo
ls d
efa
ult
to
XM
L
form
at
for
inte
rna
l d
ata
sto
rag
e.
Exa
mp
le:
Mic
roso
ft O
ffic
e,
Op
en
Off
ice
.org
, a
nd
Ap
ple
's i
Wo
rk.
•A
nd
roid
OS
re
lie
s h
ea
vil
y o
n X
ML
to s
ave
its
va
rio
us
reso
urc
es
such
as
layo
uts
, s
trin
g-s
ets
, m
an
ife
st,
etc
.
Re
ad
ing
XM
L D
ata
Wh
y s
ho
uld
XM
L b
e u
sed
?
5
XM
L is
use
d f
or
de
fin
ing
(.x
sd f
ile
s)
an
d d
ocu
me
nti
ng
(.x
ml)
cla
sse
s.
Co
nsi
de
r th
e c
om
ple
x E
mp
loye
e c
lass
de
pic
ted
he
re.
Ea
ch n
od
e i
s a
n X
ML
ele
me
nt.
Id
an
d T
itle
are
att
rib
ute
s o
f
the
cla
ss.
Re
ad
ing
XM
L D
ata
Exa
mp
le 1
. H
ow
is
XM
L u
sed
?
EMPLOYEE
Id
Title
Name
First
Middle
Last
Street
City
Zip
Country Name
Office
Phone
Photo
6
Th
is i
ma
ge
wa
s
ma
de
usi
ng
Mic
roso
ft X
ML
No
tep
ad
.
On
th
e l
eft
, th
e
stru
ctu
re o
f th
e
Em
plo
yee
cla
ss
is d
ep
icte
d a
s a
tre
e.
On
th
e r
igh
t, a
da
ta s
am
ple
fro
m t
he
curr
en
t n
od
e i
s
pro
vid
ed
.
Re
ad
ing
XM
L D
ata
Exa
mp
le 1
. H
ow
is
XM
L u
sed
?
7
<?xm
l vers
ion=
"1.0
" encodin
g=
"utf
8"
?>
<Em
plo
yees x
mln
s=
"htt
p:/
/Em
plo
yees">
<
Em
plo
yee
id=
"12
61
5"
title=
"Arch
itect"
>
<! This is a comment >
<N
am
e>
<
First>
Nan
cy<
/Fir
st>
<
Mid
dle
>J.<
/Mid
dle
>
<
Last>
Davo
lio<
/Last>
</N
am
e>
<
Str
eet>
50
7
20
th A
ve.
E.
Ap
t. 2
A<
/Str
eet>
<
City>
Seatt
le<
/City>
<
Zip
>9
81
22
</Z
ip>
<Countr
y>
<N
am
e>
U.S
.A.<
/Nam
e>
</C
ountr
y>
<
Off
ice>
5/
76
82
</O
ffic
e>
<
Phone>
(2
06
) 5
55
98
57
</P
hone>
<
Photo
>P
ho
to.j
pg<
/Photo
>
</E
mplo
yee>
<
Em
plo
yee>
.
. .
<
/ Em
plo
yee>
<
/ Em
plo
yees>
Ele
me
nt:
Str
ee
t
Att
rib
ute
s: i
d,
titl
e
Re
ad
ing
XM
L D
ata
Exa
mp
le 1
. H
ow
is
XM
L u
sed
?
Exa
mp
le t
ake
n f
rom
:
Mic
roso
ft X
mlN
ote
pa
d 2
00
7
htt
p:/
/ww
w.m
icro
soft
.co
m/d
ow
nlo
ad
s/e
n/d
eta
ils.
asp
x?fa
mil
yid
=7
2d
6a
a4
97
87
d4
11
8b
a5
f4f3
0fe
91
36
28
&d
isp
lay
lan
g=
en
Th
e X
ML
fra
gm
en
t b
elo
w d
ep
icts
th
e s
tru
ctu
re a
nd
da
ta f
or
a s
et
of
Em
plo
yee
ob
ject
s.
<?xm
l vers
ion=
"1.0
" ?>
<
xs:s
chem
a x
mln
s:x
s=
"htt
p:/
/w
ww
.w3
.org
/2
00
1/
XM
LS
ch
em
a"
ele
mentF
orm
Defa
ult=
"qu
ali
fied"
a
ttribute
Form
Defa
ult=
"un
qu
ali
fied
" ta
rgetN
am
espace=
"htt
p:/
/Em
plo
yees"
xm
lns=
"htt
p:/
/Em
plo
yees">
<xs:c
om
ple
xType n
am
e=
"Co
un
try">
<
xs:s
equence>
<
xs:e
lem
ent
nam
e=
"Nam
e"
type=
"xs:s
trin
g"
defa
ult=
"U.S
.A."
/>
</x
s:s
equence>
<
xs:a
ttribute
nam
e=
"co
de"
type=
"xs:l
an
gu
ag
e">
<
xs:a
nnota
tion>
<xs:d
ocum
enta
tion>
Reg
iste
red
IA
NA
co
un
try c
od
e –
Fo
rm
at
xxxx.
Exam
ple
: en
us.
</x
s:d
ocum
enta
tion>
<
/xs:a
nnota
tion>
</x
s:a
ttribute
>
<
/xs:c
om
ple
xType>
<xs:s
imple
Type n
am
e=
"Cit
y">
<xs:r
estr
iction b
ase=
"xs:s
trin
g">
<xs:m
inLength
valu
e=
"1"
/>
<xs:m
axLength
valu
e=
"50
" />
<
/xs:r
estr
iction>
<
/xs:s
imple
Type>
<xs:s
imple
Type n
am
e=
"Zip
">
<xs:r
estr
iction b
ase=
"xs:p
osit
iveIn
teg
er">
<
xs:m
axIn
clu
siv
e v
alu
e=
"99
99
9"
/>
<
xs:m
inIn
clu
siv
e v
alu
e=
"00
00
1"
/>
<
/xs:r
estr
iction>
<
/xs:s
imple
Type>
8
On
ly a
fe
w l
ine
s sh
ow
n h
ere
Re
ad
ing
XM
L D
ata
Exa
mp
le 1
. E
mp
loy
ee
.xsd
– S
che
ma
De
fin
itio
n (f
rag
me
nt)
9
Ke
yH
ole
Ma
rku
p l
an
gu
ale
(K
ML)
is
a f
ile
fo
rma
t u
sed
to
dis
pla
y g
eo
gra
ph
ic d
ata
in
an
Ea
rth
Bro
wse
r su
ch a
s:
Go
og
le E
art
h,
Go
og
le M
ap
s, a
nd
G
oo
gle
Ma
ps
for
Mo
bil
e A
pp
s.
Re
fere
nce
: h
ttp
://c
od
e.g
oo
gle
.co
m/a
pis
/km
l/d
ocu
me
nta
tio
n/k
ml_
tut.
htm
l
Re
ad
ing
XM
L D
ata
Exa
mp
le 2
. E
xam
ple
: K
ML
an
d G
eo
gra
ph
ic D
ata
10
R
efe
ren
ce:
htt
p:/
/co
de
.go
og
le.c
om
/ap
is/k
ml/
do
cum
en
tati
on
/km
l_tu
t.h
tml
Re
ad
ing
XM
L D
ata
Exa
mp
le 2
. E
xam
ple
: K
ML
an
d G
eo
gra
ph
ic D
ata
Pla
cem
ark
Name
Description
Point
Co
ord
inate
s
Lat,
Lo
n
Do
cu
men
t
Pla
cem
ark
In t
his
exa
mp
le a
Do
cum
en
t
con
sist
s o
f a
se
t o
f P
lace
ma
rks.
Ea
ch o
f o
ur
pla
cem
ark
s in
clu
de
s a
na
me
, d
esc
rip
tio
n,
an
d g
eo
-
loca
tio
n o
r p
oin
t (a
po
int
con
sist
s
of
the
va
lue
s, l
ati
tud
e,
lon
git
ud
e
an
d [
op
tio
na
lly
] a
ltit
ud
e )
.
11
1
1
<?xm
l vers
ion=
"1.0
" encodin
g=
“utf
-8"
?>
<
km
l xm
lns=
"htt
p:/
/w
ww
.op
en
gis
.net/
km
l/2
.2">
<
Docum
ent>
<
gcPla
ce g
cN
am
e=
"Man
akik
i G
olf
Co
urse"
gcCity=
"Wil
lou
gh
by H
ills
" gcSta
te=
"Oh
io"
/>
<Pla
cem
ark
>
<
nam
e
par=
"4"
yard
s=
"39
0"
>Tee H
ole
1<
/nam
e>
<Poin
t>
<
coord
inate
s>
81
.43
24
18
22
71
95
7,4
1.5
98
42
73
63
98
79
,0<
/coord
inate
s>
<
/Poin
t>
<
/Pla
cem
ark
>
<Pla
cem
ark
>
<
nam
e>
Fro
nt
of
Green
H
ole
1<
/nam
e>
<Poin
t>
<
coord
inate
s>
81
.43
31
82
65
67
65
,41
.59
55
73
04
79
59
1,0
</c
oord
inate
s>
<
/Poin
t>
<
/Pla
cem
ark
>
<
Pla
cem
ark
>
<
nam
e>
Mid
dle
of
Green
H
ole
1<
/nam
e>
<Poin
t>
<
coord
inate
s>
81
.43
31
66
56
35
10
9,4
1.5
95
46
47
29
89
64
,0<
/coord
inate
s>
<
/Poin
t>
<
/Pla
cem
ark
>
<
/Docum
ent>
<
/km
l>
Re
ad
ing
XM
L D
ata
Exa
mp
le2
. M
ap
pin
g w
ith
KM
L (f
rag
me
nt)
12
R
efe
ren
ce:
htt
p:/
/co
de
.go
og
le.c
om
/ap
is/k
ml/
do
cum
en
tati
on
/km
l_tu
t.h
tml
Re
ad
ing
XM
L D
ata
Exa
mp
le 3
. H
elp
ing
Go
lfe
rs w
ith
KM
L
Aft
er
a r
ath
er
me
dio
cre
Te
e-s
ho
t, t
he
pla
yer
on
th
e p
ictu
re i
s tr
yin
g t
o r
ea
ch t
he
gre
en
.
Ho
w f
ar
aw
ay
is
it ?
, w
ha
t cl
ub
sh
ou
ld h
e p
ick
?
By
th
e e
nd
of
the
le
sso
n y
ou
sh
ou
ld
kn
ow
ho
w t
o c
rea
te a
go
lf G
PS
de
vic
e.
13
Clu
b
Men
W
om
en
Driver
200-2
30-2
60
150-1
75-2
00
3-w
ood
180-2
15-2
35
125-1
50-1
80
2-H
ybrid
170-1
95-2
10
105-1
35-1
70
3-H
ybrid
160-1
80-2
00
100-1
25-1
60
4-i
ron
150-1
70-1
85
90-1
20-1
50
5-i
ron
140-1
60-1
70
80-1
10-1
40
6-i
ron
130-1
50-1
60
70-1
00-1
30
7-i
ron
120-1
40-1
50
65-9
0-1
20
8-i
ron
110-1
30-1
40
60-8
0-1
10
9-i
ron
95-1
15-1
30
55-7
0-9
5
PW
80-1
05-1
20
50-6
0-8
0
SW
60-8
0-1
00
40-5
0-6
0
Typ
ica
l Dis
tan
ces
for
(Go
od
) A
ma
teu
r P
laye
rs
So
urc
e:
htt
p:/
/go
lfca
rto
on
s.b
log
spo
t.co
m/
Go
lf M
on
thly
20
09
Re
ad
ing
XM
L D
ata
Exa
mp
le 3
. H
elp
ing
Go
lfe
rs w
ith
KM
L
Re
ad
ing
XM
L D
ata
Str
ate
gie
s fo
r R
ea
din
g/P
ars
ing
a
n X
ML
Fil
e
14
•S
eve
ral a
pp
roa
che
s a
re a
vail
ab
le
•H
ere
we
wil
l e
xplo
re t
wo
op
tio
ns:
SA
X
Sim
ple
AP
I fo
r X
ML
Re
fere
nce
s:
htt
p:/
/ww
w.s
axp
roje
ct.o
rg/
htt
p:/
/ww
w.w
3.o
rg/D
OM
/
OP
TIO
N 1
A S
AX
(S
imp
le A
PI
for
XM
L)
Xm
lPu
llP
ars
er
OP
TIO
N 2
W3
C D
ocu
me
nt
Bu
ild
er
You
tra
vers
e t
he
do
cum
en
t
pro
gra
mm
ati
call
y lo
ok
ing
fo
r th
e
be
gin
nin
g a
nd
en
din
g o
f e
lem
en
t ta
gs,
the
ir a
sso
cia
ted
te
xt a
nd
in
tern
al
att
rib
ute
s.
A D
ocu
me
nt
Bu
ild
er
ob
ject
dis
sect
s th
e
XM
L d
ocu
me
nt
pro
du
cin
g a
n e
qu
iva
len
t
tre
e-l
ike
re
pre
sen
tati
on
. N
od
es
in t
he
tre
e
are
tre
ate
d a
s fa
mil
iar
Java
Arr
ay
List
s.
Th
e W
orl
d W
ide
We
b C
on
sort
ium
(W
3C
.org
) is
an
“in
tern
ati
on
al co
mm
un
ity t
ha
t d
eve
lop
s o
pe
n s
tan
da
rds
to
en
sure
th
e lo
ng
-te
rm g
row
th o
f th
e W
eb
”.
15
1
5
•In
th
is e
xam
ple
we
wil
l re
ad
an
XM
L fi
le s
ave
d i
n t
he
ap
p’s
/r
es/
xm
l
fold
er.
Th
e f
ile
co
nta
ins
a s
et
of
KM
L p
lace
ma
rke
rs p
oin
tin
g t
o lo
cati
on
s in
a g
olf
co
urs
e (
tee
-bo
xes,
fro
nt/
cen
ter/
ba
ck o
f e
ach
gre
en
, o
bst
acl
es,
etc
)
•A
SA
X (
Sim
ple
AP
I fo
r X
ML)
Xm
lPu
llP
ars
er
wil
l tr
ave
rse
th
e d
ocu
me
nt
usi
ng
th
e .
ne
xt(
) m
eth
od
to
de
tect
th
e f
oll
ow
ing
ma
in e
ve
ntT
yp
es
START_TAG
TEXT
END_TAG
END_DOCUMENT
•W
he
n t
he
be
gin
nin
g o
f a
ta
g i
s re
cog
niz
ed
, w
e w
ill
use
th
e .
ge
tNa
me
()
me
tho
d t
o g
rab
th
e t
ag
’s n
am
e.
•W
e w
ill
use
th
e m
eth
od
.g
etT
ex
t()
to e
xtra
ct d
ata
aft
er
TE
XT
eve
nt.
Re
fere
nce
: h
ttp
://w
ww
.sa
xpro
ject
.org
/
Re
ad
ing
XM
L D
ata
Exa
mp
le 4
. S
AX
-Pa
rsin
g a
Re
sou
rce
XM
L F
ile
SA
X
Sim
ple
AP
I fo
r X
ML
16
Inn
er
att
rib
ute
s fr
om
an
<e
lem
en
t> c
an
be
ext
ract
ed
usi
ng
th
e m
eth
od
s:
.getAttributeCount()
.getAttributeName()
.getAttributeValue()
Co
nsi
de
r th
e n
am
e-e
lem
en
t in
th
e e
xam
ple
be
low
: <name par="4" yards="390" >Tee Hole 1</name>
Ele
me
nt:
“
na
me
” Te
xt:
“Te
e H
ole
1”
Att
rib
ute
s
Att
rib
ute
Na
me
A
ttri
bu
teV
alu
e
pa
r 4
ya
rds
39
0
2
att
r
Re
ad
ing
XM
L D
ata
Exa
mp
le 4
. S
AX
-Pa
rsin
g a
Re
sou
rce
XM
L F
ile
SA
X
Sim
ple
AP
I fo
r X
ML
17
Dia
gra
m s
ho
win
g t
he
lif
e-c
ycle
of
the
Xm
lPu
llP
ars
er
cla
ss.
An
y w
ell
-fo
rme
d X
ML
inp
ut
do
cum
en
t co
uld
be
pro
cess
ed
as
sug
ge
ste
d i
n t
he
fig
ure
.
Re
ad
ing
XM
L D
ata
Exa
mp
le 4
. S
AX
-Pa
rsin
g o
f a
n A
rbit
rary
XM
L F
ile
SA
X
Sim
ple
AP
I fo
r X
ML
18
Re
ad
ing
XM
L D
ata
Exa
mp
le 4
. S
AX
-Pa
rsin
g a
Re
sou
rce
XM
L F
ile
SA
X
Sim
ple
AP
I fo
r X
ML
Pa
rsin
g t
he
Te
e-T
ime
XM
L fi
le l
iste
d b
elo
w
<?xml version='1.0' encoding='UTF-8'?>
<time golfcourse="Augusta Ntl"
tournament="The Masters" >
<hour> 21 </hour>
<minutes> 25 </minutes>
<seconds> 45 </seconds>
<zone> UTC−05:00 </zone>
</time>
19
Re
ad
ing
XM
L D
ata
Exa
mp
le 4
. S
AX
-Pa
rsin
g o
f a
Re
sou
rce
KM
L F
ile
SA
X
Sim
ple
AP
I fo
r X
ML
Th
e s
am
e X
MLP
ull
Pa
rse
r u
sed
in
th
e p
rev
iou
s
exa
mp
le i
s n
ow
use
d t
o d
isse
ct a
KM
L fi
le
ho
ldin
g p
lace
ma
rke
r fr
om
a g
olf
co
urs
e.
20
<?xml version='1.0' encoding='UTF-8'?>
<kml xmlns='http://www.opengis.net/kml/2.2'>
<Document>
<gcPlace
gcName="Manakiki Golf Course"
gcCity="Willoughby Hills"
gcState="Ohio" >
</gcPlace>
<Placemark>
<name>Tee - Hole 1</name>
<Point>
<coordinates>-81.4324182271957,41.5984273639879,0</coordinates>
</Point>
</Placemark>
<Placemark>
<name>Front of Green - Hole 1</name>
<Point>
<coordinates>-81.433182656765,41.5955730479591,0</coordinates>
</Point>
</Placemark>
. . .
</Document>
</kml>
Th
is is
an
ab
bre
via
ted
vers
ion
of
the
ge
og
rap
hic
KM
L fi
le
rea
d b
y t
he
ap
p
Re
ad
ing
XM
L D
ata
Exa
mp
le 4
. S
AX
-Pa
rsin
g o
f a
Re
sou
rce
KM
L F
ile
SA
X
Sim
ple
AP
I fo
r X
ML
21
2
1
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btnReadXml"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Read XML data" />
<ScrollView
android:id="@+id/ScrollView01"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:padding="10dp">
<TextView
android:id="@+id/txtMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffeeeeee" />
</ScrollView>
</LinearLayout>
Re
ad
ing
XM
L D
ata
Exa
mp
le 4
. S
AX
-Pa
rsin
g o
f a
Re
sou
rce
KM
L F
ile
SA
X
Sim
ple
AP
I fo
r X
ML
public class ActivityMain extends Activity {
private TextView txtMsg;
Button btnGoParser;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtMsg = (TextView) findViewById(R.id.txtMsg);
btnGoParser = (Button) findViewById(R.id.btnReadXml);
btnGoParser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btnGoParser.setEnabled(false);
// do slow XML reading in a separated thread (AsyncTask)
Integer xmlResFile = R.xml.manakiki_hole1_v2;
new backgroundAsyncTask().execute(xmlResFile);
}
});
}// onCreate
2
2
Re
ad
ing
XM
L D
ata
Exa
mp
le 4
. S
AX
-Pa
rsin
g o
f a
Re
sou
rce
KM
L F
ile
SA
X
Sim
ple
AP
I fo
r X
ML
1
2
public class backgroundAsyncTask extends
AsyncTask<Integer, Void, StringBuilder> {
ProgressDialog dialog = new ProgressDialog(ActivityMain.this);
@Override
protected void onPostExecute(StringBuilder result) {
super.onPostExecute(result);
dialog.dismiss();
txtMsg.setText(result.toString());
}
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog.setMessage("Please wait...");
dialog.setCancelable(false);
dialog.show();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
// Nothing here. Needed by the interface
}
23
Re
ad
ing
XM
L D
ata
Exa
mp
le 4
. S
AX
-Pa
rsin
g o
f a
Re
sou
rce
KM
L F
ile
SA
X
Sim
ple
AP
I fo
r X
ML
3
4
@Override
protected StringBuilder doInBackground(Integer... params) {
int xmlResFile = params[0];
XmlPullParser parser = getResources().getXml(xmlResFile);
StringBuilder stringBuilder = new StringBuilder();
String nodeText = "";
String nodeName = "";
try {
int eventType = -1;
while (eventType != XmlPullParser.END_DOCUMENT) {
eventType = parser.next();
if (eventType == XmlPullParser.START_DOCUMENT) {
stringBuilder.append("\nSTART_DOCUMENT");
} else if (eventType == XmlPullParser.END_DOCUMENT) {
stringBuilder.append("\nEND_DOCUMENT");
} else if (eventType == XmlPullParser.START_TAG) {
nodeName = parser.getName();
stringBuilder.append("\nSTART_TAG: " + nodeName);
stringBuilder.append(getAttributes(parser));
} else if (eventType == XmlPullParser.END_TAG) {
nodeName = parser.getName();
stringBuilder.append("\nEND_TAG: " + nodeName );
24
Re
ad
ing
XM
L D
ata
Exa
mp
le 4
. S
AX
-Pa
rsin
g o
f a
Re
sou
rce
KM
L F
ile
SA
X
Sim
ple
AP
I fo
r X
ML
5
6
7
} else if (eventType == XmlPullParser.TEXT) {
nodeText = parser.getText();
stringBuilder.append("\n TEXT: " + nodeText);
}
}
} catch (Exception e) {
Log.e("<<PARSING ERROR>>", e.getMessage());
}
return stringBuilder;
}// doInBackground
25
Re
ad
ing
XM
L D
ata
Exa
mp
le 4
. S
AX
-Pa
rsin
g o
f a
Re
sou
rce
KM
L F
ile
SA
X
Sim
ple
AP
I fo
r X
ML
private String getAttributes(XmlPullParser parser) {
StringBuilder stringBuilder = new StringBuilder();
// trying to detect inner attributes nested inside a node tag
String name = parser.getName();
if (name != null) {
int size = parser.getAttributeCount();
for (int i = 0; i < size; i++) {
String attrName = parser.getAttributeName(i);
String attrValue = parser.getAttributeValue(i);
stringBuilder.append("\n Attrib <key,value>= "
+ attrName + ", " + attrValue);
}
}
return stringBuilder.toString();
}// getAttribures
}// backgroundAsyncTask
}// ActivityMain
26
Re
ad
ing
XM
L D
ata
Exa
mp
le 4
. S
AX
-Pa
rsin
g o
f a
Re
sou
rce
KM
L F
ile
SA
X
Sim
ple
AP
I fo
r X
ML
8
1.
Th
e X
ML
file
is
he
ld a
s a
n i
nte
rna
l re
sou
rce
in
th
e /
res/
xm
l fo
lde
r.
2.
Invo
ke t
he
re
ad
ing
-pa
rsin
g p
roce
ss i
nsi
de
an
Asy
ncT
ask
. P
ass
th
e X
ML
file
id a
s a
rgu
me
nt
to t
he
slo
w b
ack
gro
un
d t
hre
ad
.
3.
Th
e p
ars
ing
pro
cess
ha
s fi
nis
he
d.
Th
e p
rog
ress
dia
log
bo
x is
dis
mis
sed
.
4.
Ho
use
kee
pin
g.
Cre
ate
an
d s
ho
w a
sim
ple
Pro
gre
ssD
ialo
g b
ox
so t
he
use
r
ge
ts r
ea
ssu
red
ab
ou
t h
is t
ask
be
en
ta
ken
ca
re o
f.
5.
Cre
ate
an
Xm
lPu
llP
ars
er
usi
ng
th
e s
up
pli
ed
fil
e r
eso
urc
e.
6.
Th
e w
hil
e-l
oo
p i
mp
lem
en
ts t
he
pro
cess
of
ste
pp
ing
th
rou
gh
th
e S
AX
’s
pa
rse
r st
ate
dia
gra
m.
Ea
ch c
all
to
.n
ext
() p
rov
ide
s a
ne
w t
oke
n.
Th
e i
f-
the
n l
og
ic d
eci
de
s w
ha
t e
ven
t is
in
pro
gre
ss a
nd
fro
m t
he
re t
he
pro
cess
con
tin
ue
s lo
ok
ing
fo
r te
xt,
att
rib
ute
s, o
r e
nd
eve
nt.
7.
Wh
en
a S
TAR
T_
TAG
eve
nt
is d
ete
cte
d t
he
pa
rse
r ch
eck
s fo
r p
oss
ible
in
ne
r
att
rib
ute
s. I
f fo
un
d,
the
y a
re r
ep
ort
ed
as
a s
eq
ue
nce
of
<ke
y, v
alu
e>
pa
irs.
8.
Th
e m
eth
od
ge
tAtt
rib
ute
s()
ext
ract
s a
ttri
bu
tes
(if
po
ssib
le).
A l
oo
p d
rive
n
by
th
e c
ou
nt
of
tho
se a
ttri
bu
tes
att
em
pts
to
ge
t th
e n
am
e a
nd
va
lue
of
ea
ch p
air
‘n
am
e=
va
lue
’ fo
r th
e c
urr
en
t e
lem
en
t. T
he
re
sult
is
retu
rne
d a
s
a s
trin
g.
Re
ad
ing
XM
L D
ata
Exa
mp
le 4
. C
om
me
nts
SA
X
Sim
ple
AP
I fo
r X
ML 2
7
28
In t
his
exa
mp
le w
e w
ill
exp
lore
a s
eco
nd
ap
pro
ach
fo
r d
eco
din
g a
n X
ML
do
cum
en
t.
1.
A W
3C
Do
cum
en
tBu
ild
er
pa
rse
r w
ill
be
use
d f
or
de
cod
ing
an
arb
itra
ry
(we
ll-f
orm
ed
) X
ML
file
.
2.
In o
ur
exa
mp
le,
the
in
pu
t fi
le i
s st
ore
d e
xte
rna
lly
in
th
e S
D c
ard
.
3.
Th
e f
ile
in
clu
de
s va
rio
us
ele
me
nts
: <
cou
rse
>,
<n
am
e>
, <
coo
rdin
ate
>.
4.
Th
e p
ars
er
wil
l cr
ea
te J
ava
-lik
e l
ists
to
sto
re a
ll t
he
te
xt a
nd
att
rib
ute
s
he
ld i
n e
ach
no
de
ty
pe
.
5.
For
inst
an
ce,
ou
r fi
le i
s a
bo
ut
a r
eg
ula
tio
n g
olf
co
urs
e.
Th
e <
na
me
>
ele
me
nts
id
en
tify
im
po
rta
nt
loca
tio
ns
in t
he
co
urs
e s
uch
as:
‘Te
e-B
ox
Ho
le1
’, ‘F
ron
t o
f G
ree
n –
Ho
le1
’, ‘B
un
ker1
-Gre
en
Left
-Ho
le1
’, …
, ‘B
ack
of
Gre
en
– H
ole
18
’. T
he
No
de
List
ma
de
fo
r th
e <
coo
rdin
ate
> e
lem
en
ts
con
tain
th
e l
ati
tud
e a
nd
lo
ng
itu
de
of
ea
ch e
ntr
y i
n t
he
<n
am
e>
lis
t.
Re
ad
ing
XM
L D
ata
Exa
mp
le 5
. T
he
W3
C D
ocu
me
ntB
uil
de
r C
lass
Pla
cem
ark
Name
Coordinates
Do
cu
men
t
Pla
cem
ark
Course
29
2
9
Parser’s
Str
ate
gy
<
Ele
me
nts
> f
rom
th
e i
np
ut
XM
L fi
le b
eco
me
no
de
s in
an
in
tern
al
tre
e r
ep
rese
nta
tio
n o
f
the
da
tase
t. T
he
no
de
la
be
led
<D
ocu
me
nt>
act
s a
s th
e r
oo
t o
f th
e t
ree
. Yo
ur
Tu
rn
PH
AS
E 1
. Fo
r e
ach
XM
L e
lem
en
t yo
u r
eq
ue
st
the
co
nst
ruct
ion
of
a N
od
eLi
st (
Arr
ay
List
-lik
e
coll
ect
ion
) u
sin
g t
he
me
tho
d:
.getElementsByTagName().
PH
AS
E2
. E
xplo
re a
n i
nd
ivid
ua
l n
od
e f
rom
a
No
de
List
usi
ng
th
e m
eth
od
s:
. item(i),
. getName() ,
. getValue() ,
. getFirstChild() ,
. getAttributes(), etc.
Re
ad
ing
XM
L D
ata
Exa
mp
le 5
. T
he
W3
C D
ocu
me
ntB
uil
de
r C
lass
30
Re
ad
ing
XM
L D
ata
Exa
mp
le 5
. T
he
W3
C D
ocu
me
ntB
uil
de
r C
lass
<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<course phone="(440)942-2500" length="6">Manakiki Golf Course</course>
<Placemark>
<name>Tee Box - Hole 1</name>
<Point>
<coordinates>-81.4324182271957,41.5984273639879,0</coordinates>
</Point>
</Placemark>
<Placemark>
<name>Front of Green - Hole 1</name>
<Point>
<coordinates>-81.433182656765,41.5955730479591,0</coordinates>
</Point>
</Placemark>
<Placemark>
<name>Middle of Green - Hole 1</name>
<Point>
<coordinates>-81.4331665635109,41.5954647298964,0</coordinates>
</Point>
</Placemark>
On
ly a
fe
w e
ntr
ies
are
sh
ow
n f
or
the
in
pu
t X
ML
file
use
d i
n t
his
exa
mp
le.
Late
r, w
e w
ill
req
ue
st l
ists
to
be
ma
de
fo
r th
e e
lem
en
ts:
cou
rse
, n
am
e,
an
d c
oo
rdin
ate
.
31
Re
ad
ing
XM
L D
ata
Exa
mp
le 5
. T
he
W3
C D
ocu
me
ntB
uil
de
r C
lass
Aft
er
pa
rsin
g t
he
KM
L fi
le s
ho
wn
in
the
pre
vio
us
ima
ge
we
ob
tain
th
e
foll
ow
ing
No
de
List
s.
Th
e f
irst
arr
ow
s p
oin
ts t
o t
he
en
try
<n
am
e>
[2
] w
hic
h h
old
s th
e v
alu
e
“Mid
dle
of
the
Gre
en
– H
ole
1”.
Th
e s
eco
nd
arr
ow
po
int
to i
ts
coo
rdin
ate
s (<
coo
rdin
ate
>[2
] )
32
Re
ad
ing
XM
L D
ata
Exa
mp
le 5
. T
he
W3
C D
ocu
me
ntB
uil
de
r C
lass
1.
Aft
er
scro
llin
g t
he
vie
w s
ho
wn
in
th
e p
rev
iou
s im
ag
e,
we
ge
t th
e <
cou
rse
>
info
rma
tio
n.
Ob
serv
e t
he
re i
s o
nly
on
e n
od
e w
ith
th
e t
ag
<co
urs
e>
ho
ldin
g t
he
text
‘Ma
na
kik
i G
olf
Co
urs
e’
2.
Th
e a
rro
ws
po
ints
to
<co
urs
e>
att
rib
ute
s: p
ho
ne
an
d l
en
gth
.
public class MainActivity extends Activity {
private TextView txtMsg;
Button btnGoParser;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtMsg = (TextView) findViewById(R.id.txtMsg);
btnGoParser = (Button) findViewById(R.id.btnReadXml);
btnGoParser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btnGoParser.setEnabled(false);
// this xml file includes elements: <course>,
// <name>, <coordinates> for justa few holes
String xmlFile = "manakiki_holes1and2.kml";
// do slow XML reading in a separated thread
new backgroundAsyncTask().execute(xmlFile);
}
});
}// onCreate
33
Re
ad
ing
XM
L D
ata
Exa
mp
le 5
. T
he
W3
C D
ocu
me
ntB
uil
de
r C
lass
We
use
in
th
is e
xam
ple
th
e s
am
e s
cre
en
-la
you
t o
f th
e p
rev
iou
s a
pp
.
1
private class backgroundAsyncTask extends AsyncTask<String, Void, String> {
ProgressDialog dialog = new ProgressDialog(MainActivity.this);
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
dialog.dismiss();
txtMsg.setText(result.toString());
}
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog.setMessage("Please wait...");
dialog.setCancelable(false);
dialog.show();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
34
Re
ad
ing
XM
L D
ata
Exa
mp
le 5
. T
he
W3
C D
ocu
me
ntB
uil
de
r C
lass
2
@Override
protected String doInBackground(String... params) {
String xmlFileName = params[0];
return useW3CParser(xmlFileName);
}// doInBackground
}// backgroundAsyncTask
private String useW3CParser(String xmlFileName) {
StringBuilder str = new StringBuilder();
try {
String kmlFile = Environment.getExternalStorageDirectory()
.getPath() + "/" + xmlFileName;
InputStream is = new FileInputStream(kmlFile);
DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
Document document = docBuilder.parse(is);
if (document == null) {
Log.v("REALLY BAD!!!!", "document was NOT made by parser");
return "BAD-ERROR";
}
35
Re
ad
ing
XM
L D
ata
Exa
mp
le 5
. T
he
W3
C D
ocu
me
ntB
uil
de
r C
lass
2
3
// put all data into NodeLists
NodeList listNameTag = document.getElementsByTagName("name");
NodeList listCoordinatesTag = document
.getElementsByTagName("coordinates");
NodeList listCourseTag = document.getElementsByTagName("course");
// traverse NodeLists for elements: <name>, <coordinates>, <course>.
str.append(getAllDataFromNodeList(listNameTag, "name"));
str.append(getAllDataFromNodeList(listCoordinatesTag, "coordinates"));
str.append(getAllDataFromNodeList(listCourseTag, "course"));
} catch (FileNotFoundException e) {
Log.e("W3C Error", e.getMessage());
} catch (ParserConfigurationException e) {
Log.e("W3C Error", e.getMessage());
} catch (SAXException e) {
Log.e("W3C Error", e.getMessage());
} catch (IOException e) {
Log.e("W3C Error", e.getMessage());
}
return str.toString();
}// useW3cOrgDocumentBuilder
36
Re
ad
ing
XM
L D
ata
Exa
mp
le 5
. T
he
W3
C D
ocu
me
ntB
uil
de
r C
lass
4
5
private Object getAllDataFromNodeList(NodeList list, String strElementName) {
StringBuilder str = new StringBuilder();
// dealing with the <strElementName> tag
str.append("\n\nNodeList for: <" + strElementName + "> Tag");
for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
int size = node.getAttributes().getLength();
String text = node.getTextContent();
str.append("\n " + i + ": " + text);
// get all attributes of the current element (i-th hole)
for (int j = 0; j < size; j++) {
String attrName = node.getAttributes().item(j).getNodeName();
String attrValue = node.getAttributes().item(j).getNodeValue();
str.append("\n attr. info-" + i + "-" + j + ": " + attrName
+ " " + attrValue);
}
}
return str;
}//getAllDataFromNodeList
}// ActivityMain
37
Re
ad
ing
XM
L D
ata
Exa
mp
le 5
. T
he
W3
C D
ocu
me
ntB
uil
de
r C
lass
6
7
1.
Ide
nti
fy t
he
in
pu
t X
ML
file
co
nta
inin
g t
ag
-ele
me
nts
: co
urs
e,
na
me
, a
nd
coo
rdin
ate
s. D
o t
he
po
ten
tia
lly
slo
w p
ars
ing
of
the
fil
e i
n a
se
pa
rate
d
thre
ad
usi
ng
an
Asy
ncT
ask
ob
ject
, p
ass
ing
to
it
the
SD
-re
sid
en
t fi
le’s
na
me
.
2.
Th
e d
oIn
Ba
ckg
rou
nd
me
tho
d c
all
s useW3CParser
wh
ere
all
th
e w
ork
is
to b
e a
ctu
all
y d
on
e.
3.
Th
e m
eth
od
use
W3
CP
ars
er
inst
an
tia
tes
a D
ocu
me
ntB
uil
de
r w
ork
er
to
acc
ep
t th
e d
ata
str
ea
m c
om
ing
fro
m t
he
XM
L fi
le.
Th
is m
eth
od
cre
ate
s a
n
inte
rna
l tr
ee
-lik
e r
ep
rese
nta
tio
n o
f th
e s
tru
ctu
red
XM
L-d
ocu
me
nt.
4.
Th
e t
ree
ve
rsio
n o
f th
e d
ocu
me
nt
is t
rave
rse
d a
nd
No
de
List
s a
re m
ad
e f
or
the
ele
me
nts
: <
na
me
>,
<co
ord
ina
tes>
an
d <
cou
rse
>.
5.
Ea
ch o
f th
e l
ists
is
vis
ite
d t
o r
ep
ort
th
eir
co
rre
spo
nd
ing
co
nte
nts
.
6.
For
ea
ch n
od
e e
xtra
ct t
he
te
xt (
if a
ny
) h
eld
be
twe
en
th
e b
eg
inn
ing
an
d
en
d t
ag
s.
7.
For
ea
ch n
od
e e
xtra
ct i
ts i
nte
rna
l a
ttri
bu
te (
if a
ny
) in
th
e f
orm
of
<ke
y,
valu
e>
pa
irs.
38
Re
ad
ing
XM
L D
ata
Exa
mp
le 5
. C
om
me
nts