Verarbeitung mit SAX- und DOM

Werbung
!
"
#
$
%
&
0
)
% *
( ) "
% +
,
( ) "
%
. / +
(
-
)
" "
.
( ) "
'
& 2
3
5
4
% *
& 2
( ) "
7
-
&
6
83
4
2) "
"
6
&
)9
*
1
6
5
7
5
6
5
7
5
% *
%
% *
3
,
8
-
6
*
4;
,
2
)
5
"
) &
2
<
7 ) &
% *
2
:
=
7
5
7
5
7
5
% *
3
,
7
3
-
2
4;
<
)
7 ) &
5
%
,
,
% *
2
2
?
7
7
A%
(?
A%
%
(? 4 (
% 4
"
"7
A%
4A & % + 8
A
5
? @
% +8
>
B
!
83
% +
7 3
$*
A & % +
*
3 $ 4 D D EE
)
&
-
8
'D
74FF* * * 2 /7
7
@
2
F
C
"
#
%
%
5
,
"
H
4
)
7
5&
% *
G
5
$
<priceList>
<coffee>
<name>
Mocha Java
</name>
<price>
11.95
</price>
</coffee>
</priceList>
5
% *
8
-
45 *
%
8
8
8
8
8
8
8
8
8
8
4
startElement(…,priceList,…) 82
startElement(…,coffee,…) 82
startElement(…,name,…) 82
characters("Mocha Java",…) 82
endElement(…,name,..) 82
startElement(…,price,…) 82
characters("11.95",…) 82
endElement(…,price,…) 82
endElement(…,coffee,…) 82
endElement(…,priceList,…) 82
5
7
*
*
2
)
-
"
8
=
#
5&
& "
83 @
-
G
8
I
8
% *
J
*
,
)
5
2 24
startDocument
endDocument
startElement
endElement
characters
processingInstruction
D
)
DefaultHandler
+" 7 "
@ *
)9
D
=
3
*
)
4
startElement
public void startElement(java.lang.String
uri,
startElement
java.lang.String localName,
java.lang.String qName,
Attributes attributes)
throws SAXException
uri4
" - $
uri ! "
localName4
! "
localName )
qName4
68 /
qName ! " "
attributes4
" 5 "
attributes $
%
)9
3
! "
$
88 *
endElement 6
@
68 /
9
%
I+
/J
attributes
'
characters
public void characters(char[]
buffer,
characters
int offset,
int length)
throws SAXException
buffer:
&
offset4 % 8
0
/
offset+length
String s = new String(buffer, offset, length);
1
$
<priceList>
<coffee>
<name>
% 8
&
Mocha Java
</name>
<price>
11.95
</price>
</coffee>
</priceList>
G
$*
4K
A &
83
(
2
9
=
*
4
%
2 7
D
)
:
&
#
'
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParserFactory
8
SAXParser saxParser = factory.newSAXParser();
SAXParser
8
saxParser.parse("priceList.xml", handler);
9H
%
priceList.xml: $ 7
"
handler: +
D
) N
$&
)
(
)
DefaultHandler " 7 "
LM
"(
)
5
G
*
* 8 "
" A
;(
O&
K ""
I CC:J
8
? @ )
? @ )
+
8
+
$
)
"
2
)
F+
8
&
"
"7 "
-
7
4 Iterator i = list.iterator();
-
7
4 SAXParser saxParser = factory.newSAXParser();
>
%
<priceList>
<coffee>
<name>
</name>
<price>
</price>
</coffee>
</priceList>
0
#
5 "
Mocha Java
P
inName
P
11.95
"
A &
inMochaJava
5 "
P7
inMJPrice
6
P
print(s)
B
%
#
+
public void startElement(..., String elementName, ...) {
if (elementName.equals("name")){ inName = true; }
else if (elementName.equals("price") && inMochaJava ){
inMJPrice = true;
inMochaJava = false; } }
public void characters(char [] buf, int offset, int len) {
String s = new String(buf, offset, len);
if (inName && s.equals("Mocha Java")) {
inMochaJava = true;
inName = false; }
else if (inMJPrice) {
System.out.println("The price of Mocha Java is: " + s);
inMJPrice = false; } }
C
! <name> ,
public void startElement(...,
String elementName, ...){
startElement
if (elementName.equals("name")){ inName = true; }
else if (elementName.equals("price") && inMochaJava ){
inMJPrice = true;
<name>Mocha
Java</name>
<name>
inMochaJava = false; } }
<price>11.95</price>
public void characters(char [] buf, int offset, int len) {
String s = new String(buf, offset, len);
if (inName && s.equals("Mocha Java")) {
inMochaJava = true;
inName = false; }
else if (inMJPrice) {
% 8 price$of Mocha Java is: " + s);
System.out.println("The
inMJPrice = false;) } }
0
&
0
&
P8
inName
inName:
! "Mocha Java" ,
public void startElement(..., String elementName, ...){
if (elementName.equals("name")){ inName = true; }
else if (elementName.equals("price") && inMochaJava ){
inMJPrice = true;
<name>Mocha Java</name>
Java
inMochaJava = false; } }
<price>11.95</price>
public void characters(char
[] buf, int offset, int len) {
characters
String s = new String(buf, offset, len);
if (inName && s.equals("Mocha Java")) {
inMochaJava = true;
inName = false; }
else if (inMJPrice) {
System.out.println("The price of Mocha Java is: " + s);
inMJPrice = false; } }
"
inName
inMochaJava
#
public void characters(char
[] buf, int offset, int len) {
characters
String s = new String(buf, offset, len);
if (inName) { if (s.equals("Mocha Java")) {
inMochaJava = true;
<name>Mocha Java</name>
Java
inName = false; }
<price>11.95</price>
else inName = false; }
else if (inMJPrice) {
System.out.println("The price of Mocha Java is: " + s);
inMJPrice = false; } }
inName
inMochaJava
inMochaJava
! <price> ,
public void startElement(...,
String elementName, ...){
startElemen
if (elementName.equals("name")){ inName = true; }
else if (elementName.equals("price") && inMochaJava ){
inMJPrice = true;
<name>Mocha Java</name>
inMochaJava = false; } }
<price>11.95</price>
<price>
public void characters(char [] buf, int offset, int len) {
String s = new String(buf, offset, len);
if (inName && s.equals("Mocha Java")) {
inMochaJava = true;
inName = false; }
else if (inMJPrice) {
System.out.println("The price of Mocha Java is: " + s);
inMJPrice = false; } }
inName
inMochaJava
inMJPrice
'
inMJPrice
#
public void startElement(..., String elementName, ...){
if (elementName.equals("name")){ inName = true; }
else if (elementName.equals("price") && inMochaJava ){
inMJPrice = true;
<name>Mocha Java</name>
inMochaJava = false; } }
<price>11.95</price>
11.95
public void characters(char
[] buf, int offset, int len) {
characters
String s = new String(buf, offset, len);
if (inName && s.equals("Mocha Java")) {
inMochaJava = true;
inName = false; }
else if (inMJPrice) {
System.out.println("The price of Mocha Java is: " + s);
inMJPrice = false; } }
inMJPrice
print(s)
1
)
#
<priceList>
<coffee>
<name>
</name>
<name>
</name>
<price>
5 "
Mocha Java
P
"
inName
P
MS Java
A &
inMochaJava
5 "
11.95
P7
inMJPrice
</price>
</coffee>
</priceList>
P
print(s)
:
)
#
public void startElement(..., String elementName, ...){
if (elementName.equals("name")){ inName = true; }
else if (elementName.equals("price") && inMochaJava ){
inMJPrice = true;
<name>Mocha Java</name>
inMochaJava = false; } }
<name>MS Java</name>
inMochaJava
) ""
)
& 6
0
<price>11.95</price>
<price>
*
inName
<name> *
inMochaJava
inMochaJava
<price> *
inMJPrice
) ""
0
&
A & *
)
=
inMJPrice
)
#
%
3
7 38
2
( ) "
)
"
"
,
""
0 6
7 38
3
8
)
"
$ 2
(. (
$2-2 (name price)+ &
)
/
)
8
)
%
8
D
)
$ 6
)9
( ) "
&
*
8
"
2
>
E
88 $
Q
/ I
&
*
H
*
Q
" J"
5
"9
&
&
% *
2
&
Q
( ) "
,
)
( ) "
/
8)
B
.
/ 0
0
1
0
!!
% *
(
(
%%
startElement
characters
)
I
% *
$
)
%
6
& "
% J2
getPriceList()
8
2
&
,
8
8
getPriceList()
+" "
% *
(
$
88
0 2-2 )9
$
$8
8
&
% +
&
/
C
12
'
1
2 #3
41 2
5
(?
""
)
$ " 0
5
&
% *
)
8
8$
-6 "
'D
6
&
""
7
83
83 G .
( ) "
" 5
5
'
12
$/
(
<?xml version="1.0" ?>
<priceList>
<coffee>
<name>Mocha Java</name>
<price>11.95</price>
</coffee>
</priceList>
-
4( ) "
(
"
!
version="1.0" $
Document Node
!
!
4 PriceList
!
5 "
!
5 "
$
4&
7 6
"
I(
"
( ) "
Element Node " "
4 coffee
!
!
priceList
J
$
" $2-2
NodeList
'
6
$
<?xml version="1.0" ?>
<priceList>
<coffee>
<name>Mocha Java</name>
<price>11.95</price>
5 "
!
</coffee>
!
</priceList>
5 "
4 name
!
4 D (%. % *
2
4 coffee
5 "
!
. / !
4 price
!
!
4 Mocha Java
4 11.95
. / !
''
-
7
7
&
!
/
!
8
D
( )
0
88 3
! "
getElementsByTagName
D
"9
4
'1
$
<priceList>
<coffee>
<name>
% 8
Mocha Java
G
$*
</name>
<price>
11.95
</price>
</coffee>
</priceList>
4K
A &
83
(
&
=
9
4
2
(?
2
7
88 "
0
*
':
&
#
12
'
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
8
DocumentBuilderFactory
DocumentBuilder builder = factory.newDocumentBuilder();
8
(?
Document document = builder.parse("priceList.xml");
parse().
(?
8
) "7
"
(?
-
"
'
&
.
!!
'
NodeList coffeeNodes = document.getElementsByTagName("coffee");
for (int i=0; i < coffeeNodes.getLength(); i++) {
thisCoffeeNode = coffeeNodes.item(i);
Node thisNameNode = thisCoffeeNode.getFirstChild();
String data = thisNameNode.getFirstChild().getNodeValue();
if (data.equals("Mocha Java")) {
Node thisPriceNode = thisNameNode.getNextSibling();
String price = thisPriceNode.getFirstChild().getNodeValue();
break; } }
PA &
""
(?
$
'>
coffee "
#
7
NodeList coffeeNodes = document.getElementsByTagName("coffee");
getElementsByTagName4
)
0
88 8
5 "
3
! "
*
5 "
M
""
NodeList
<?xml version="1.0" ?>
<priceList>
<coffee>
<name>Mocha Java</name>
<price>11.95</price>
</coffee>
</priceList>
'B
$
coffee
"
7
NodeList coffeeNodes = document.getElementsByTagName("coffee");
for (int i=0; i < coffeeNodes.getLength(); i++) {
thisCoffeeNode = coffeeNodes.item(i);
…
}
coffeeNodes.item(0)
<?xml version="1.0" ?>
<priceList>
<coffee>
<name>Mocha Java</name>
<price>11.95</price>
</coffee>
</priceList>
'C
#
coffee7
NodeList coffeeNodes = document.getElementsByTagName("coffee");
for (int i=0; i < coffeeNodes.getLength(); i++) {
thisCoffeeNode = coffeeNodes.item(i);
Node thisNameNode = thisCoffeeNode.getFirstChild();
String data = thisNameNode.getFirstChild().getNodeValue();
if (data.equals("Mocha Java")) {
Node thisPriceNode = thisCoffeeNode.getNextSibling();
<?xml version="1.0" ?>
String price = thisPriceNode.getFirstChild().getNodeValue();
<priceList>
break; } }
<coffee>
<name>Mocha Java</name>
8
D
<price>11.95</price>
</coffee>
</priceList>
1
name7
#
NodeList coffeeNodes = document.getElementsByTagName("coffee");
for (int i=0; i < coffeeNodes.getLength(); i++) {
thisCoffeeNode = coffeeNodes.item(i);
Node thisNameNode = thisCoffeeNode.getFirstChild();
String data = thisNameNode.getFirstChild().getNodeValue();
if (data.equals("Mocha Java")) {
Node thisPriceNode = thisCoffeeNode.getNextSibling();
<?xml version="1.0" ?>
String price = thisPriceNode.getFirstChild().getNodeValue();
<priceList>
8
D
break; } }
<coffee>
Element Node: coffee
<name>Mocha Java</name>
NodeList
<price>11.95</price>
Element Node: name
Element Node: price
</coffee>
NodeList
NodeList
</priceList>
Text Node: Mocha Java
Text Node: 11.95
1
#
,
"
7
NodeList coffeeNodes = document.getElementsByTagName("coffee");
for (int i=0; i < coffeeNodes.getLength(); i++) {
thisCoffeeNode = coffeeNodes.item(i);
Node thisNameNode = thisCoffeeNode.getFirstChild();
String data = thisNameNode.getFirstChild().getNodeValue();
if (data.equals("Mocha Java")) {
Node thisPriceNode = thisNameNode.getNextSibling();
String price = thisPriceNode.getFirstChild().getNodeValue();
<?xml version="1.0" ?>
<priceList>
break; } }
<coffee>
<name>Mocha Java</name>
/
<price>11.95</price>
</coffee>
</priceList>
1
price7
#
<?xml version="1.0" ?>
NodeList coffeeNodes = document.getElementsByTagName("coffee");
<priceList>
for (int i=0; i < coffeeNodes.getLength(); i++) {
<coffee>
thisCoffeeNode = coffeeNodes.item(i);
<name>Mocha Java</name>
Node thisNameNode = thisCoffeeNode.getFirstChild();
<price>11.95</price>
</coffee>
String data = thisNameNode.getFirstChild().getNodeValue();
8
D
</priceList>
if (data.equals("Mocha Java")) {
Node thisPriceNode = thisNameNode.getNextSibling();
String price = thisPriceNode.getFirstChild().getNodeValue();
break; } }
1'
E
12
/ I
&
E
*
)
*
0
" J"
&
2
88
85 "
3
! "
E
8)
5
Q 7
Q
% *
&
( ) "
&
&
,
/
11
12
'
%
"
$
( ) "
0 7 )
""
.
$ 8
$
.
)
&
"
( ) "
76
9
$
5
8$
&
( ) "
(?
"
( ) "
$ $
5
( ) "
8
$ &
.
7
0
)
8
8$
&
1:
8#
0
1
8#
0
F
Datenabstraktion
XMLSchema
Übersetzen
Va
l
Instanz
XMLDokument
*
id
i
er
JavaKlassen
en
2
0*
)4
&
Instanzen
Deserialisieren
Serialisieren
JavaObjekte
8$
&
5
( ) "
A% -4
- A & %
.
&
A &
8
&
( &
7
)
1>
$
<priceList>
<coffee>
<name>Mocha Java</name>
<price>11.95</price>
</coffee>
</priceList>
"
A% -
public interface PriceList {
java.util.List getCoffee();
getCoffee
public interface CoffeeType {
String getName();
getName
void setName(String
value)
setName
java.math.BigDecimal getPrice();
getPrice
void setPrice(java.math.BigDecimal
value) } }
setPrice
1B
8#
0
xsd:choice
<xs:element name="BoolCommentOrValue">
<xs:complexType>
<xs:choice>
<xs:element name="bool" type="xs:boolean"/>
<xs:element name="comment" type="xs:string"/>
<xs:element name="value" type="xs:int"/>
</xs:choice>
</xs:complexType>
</xs:element>
)
(
,7
A & 3
$
*
1C
8#
0
xsd:choice
<xs:element name="BoolCommentOrValue">
public interface BoolCommentOrValue {
<xs:complexType>
int getValue();
<xs:choice>
getValue
value);
void setValue(int
<xs:element name="bool" type="xs:boolean"/>
setValue
boolean
isSetValue();
<xs:element name="comment"
type="xs:string"/>
isSetValue
<xs:element name="value" type="xs:int"/>
java.lang.String getComment();
getComment
</xs:choice>
void setComment(java.lang.String value);
</xs:complexType>
boolean isSetComment();
isSetComment
</xs:element>
boolean getBool();
getBool
void setBool(boolean
value);
setBoo
boolean isSetBool();
isSetBool
Object getContent();
getContent
boolean isSetContent();
isSetContent
void unSetContent();
unSetContent }
:
&
'
"
#
$
DR
G
4
$ *
3
"
)9
"
)
A &
2
( ) "
"
*
"
8$
2
"
"
68
:
9*
" $
3
0
"
&
K
"3
*
68
8
3
* )
%
8
+
2
8
"
2
:
" $
3
K
68
*
K
7
K
-
68
7 6
*
K
0
4-
8 *
N
%
)
"
"
* )
"
"
-
2
:'
" $
3
XML
-
N
"
"
"
"
6
,
/
% 8
M
$*
8 *
""
$
)9
&
2
"
#
$
2
:1
&
#
+4
8 *
+
& "
"
2
++4
++
$
%
/!
'
)
7
68
-
""
)9
"
&
#
$
2
@ )
8*
++
+
SB T
::
&
,
/
'
(?
!
"
#
&
%
(?
$
#
'2 #
4 2
2K
77
6
.
8
"
&
( ) "
"
.
:
Herunterladen