Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Конспект(слайды) лекций (4 сем)

.pdf
Скачиваний:
86
Добавлен:
15.06.2014
Размер:
3.66 Mб
Скачать

Listener

event

 

handlers

 

delegation

Source

Java API for XML Processing (JAXP) –

XML ,

Java Architecture for XML Binding (JAXB) –

XML

Java

Java API for XML Messaging (JAXM) –

SOAP (Simple Object Access Protocol) Internet

!"#"$ %

!"#"$ %

& '

!"#"$( ' " ($) "$) !"#"$ %

**+,-

!"#"$( ' " ($) "$) !"#"$ %

"$) !"#"$ %

22.05.2007

Java API for XML Registries (JAXR) –

Java API for XML-based RPC (JAX-RPC)

SOAP

Internet

.

. /0 1 2

SAXParserFactory factory =

SAXParserFactory.newInstance( );

SAXParser saxParser = factory.newSAXParser(); saxParser.parse( "priceList.xml" , handler);

1

3 / / 4 1

public void startElement(..., String elementName,...)

{

if(elementName.equals("name" )){

inName = true;

} else if(elementName.equals("price" ) && inMochaJava) {

inPrice = true; inName = false;

}

}

<=$) '" " " "

:"") & )& ; " )";"$)"$ 7#;&

>$ $ 7 " '" ! <= "

import org.apache.xerces.parsers.*;

import org.xml.sax.*; // Xerces DOM runs on top of SAX import org.w3c.dom.*;

public class SomeClass {

DOMParser parser = new DOMParser();

}

3 / / 4 1

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 (inPrice) {

System.out.println("The price is: " + s); inMochaJava = false; inPrice = false;

}

}

<" " #7$7$? :&)" @A;"

;BC!7 &7) ""D ! E:&)" $&)"( 7$ D 7 " &B F GH7 ' E$&)"+?" :&)"@A;"EFF

G

" :&)"+ @@I>JK@ L:=< M ;& " 7CEE F$&)"( &B FN C " N

" :&)"+<=OK :@L:=< M ; & " <& EE<& B#"$ F$&)"( &B FN C " N

" :&)"+ :@L:=< M

; & " !"#EE !"#"$ F$&)"( &B FN C " N

P

Q

Q

22.05.2007

5. 1

& '

**+,-

$"6 7$ & 7&$ &8 !"#"$ 997$: #" 7

$"6 7$ & 7&$ &8 ' " 997$ & ' 7

$"6 7$ & 7&$ &8 !"#"$ 997$ 7 " 7

$"6 7$ & 7&$ &8 ' " 99; 7$ ; 7 "

R/ / 1 . 1S E. F

;BC!7 &7) ; & " <& E<& B#"$ )& ( 7$ D 7 " &B F G

78 E '7!)L!7 TU $B!!F G 7$ &B$ U

8& E7$ 7 U VN 7 W &B$ N XX7F G""D ! E &B FN

Q

Q

Q

2

R/ / 1 S

;BC!7 &7) ; & " !"#E:&)" $&)"( 7$ D 7 " &B F G

! "

YY & " 7CB " 8& E7$ 7 U VN 7 W XX7F G

""D ! E &B FN

Q YY & " '7!) "$( 78 $A 78 E F G

8& E7$ 7 U VN 7 W XX7F G""D ! E &B FN

Q

Q

Q

3 / 2

3 / 2 <= M

\ /] . 2 . / 1 ^ 5 S1 S_

. 1S

\ S` ^ S1 S S . 1S E ` (

] 1 a b ^ [ ( . / ^ +.+F

\ S` 2 / ^ 5 1 2 S

. 1S S

3 / 2 M

\ 2 . 1 2 5 E + + c

/ ._2_2 5 F

\ S` ^1 1 ^1 1 9

. 1S

\ / ^ 1 2_a b ^1 .

Z / [ .

NodeList list = document.getElementsByTagName("name" );

Node thisNode = list.item(0); // loop through list Node thisChild = thisNode.getChildNode(); if(thisNode.getFirstChild() instanceof org.w3c.dom.TextNode){

String data = thisNode.getFirstChild().getData();

}

if (data. equals("Mocha Java")) {

// Mocha Java

Node newNode = document.createElement("coffee");

#;!" &)" 8& @$8& # 7&$

• O&)"$7;;"

@ $ 8& #" d & A 8 & A U @ $ 8& #" d & A+$"H>$ $ "EFN @ $ 8& #" &Ce U 8 & A+$"H@ $ 8& #" E$"H " # &B "Ef#A+6 !gFFN&Ce+ $ 8& #E$"H " # &B "Ef#A+6#!gF( $"H

" #I" B! Ef " B! +6#!gFFN

• d& ";" 7$? " &8$8& # 7&$B " @"#;! "

@ $ 8& #" d & A 8 & A U @ $ 8& #" d & A+$"H>$ $ "EFN @"#;! " "#;! " U 8 & A+$"H@"#;! " E$"H " # &B "Ef#A+6 !gFFN @ $ 8& #" &Ce U "#;! "+$"H@ $ 8& #" EFN&Ce+ $ 8& #E$"H <= &B "E)& F(

$"H " #I" B! E " B! +?" =B ;B " #EFFFN

22.05.2007

Z / [ .

Node nameNode = document.createElement("name"); TextNode textNode = document.createTextNode("Kona"); nameNode.appendChild(textNode);

Node priceNode = document.createElement("price");

TextNode tpNode = document.createTextNode("13.50");

priceNode.appendChild(tpNode);

newNode.appendChild(nameNode);

newNode.appendChild(priceNode);

thisNode.InsertBefore(newNode, thisNode);

h ] 1 S J

3

M

<orders-list>

<order>

<number>1001</number> <date>2005-01-01</date> <description>First Order</description> <amount>10</amount>

</order>

<order>

<number>1002</number> <date>2005-01-02</date> <description>Second Order</description> <amount>20.2</amount>

</order>

</orders-list>

M '"#

<xsd:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xsd:element name="orders-list">

<xsd:complexType>

<xsd:sequence>

<xsd:element ref="order" maxOccurs="unbounded"/> </xsd:sequence>

</xsd:complexType>

</xsd:element>

<xsd:element name="order"> <xsd:complexType>

<xsd:sequence>

<xsd:element name="number" type="xsd:long"/> <xsd:element name="date" type="xsd:date"/> <xsd:element name="description" type="xsd:string"/> <xsd:element name="amount" type="xsd:double"/>

</xsd:sequence>

</xsd:complexType>

</xsd:element>

</xsd:schema>

22.05.2007

M i 1

OrdersListType.java

public interface OrdersListType {

java.util.List getOrder();

}

OrderType.java public interface OrderType {

double getAmount();

void setAmount(double value);

java.lang.String getDescription();

void setDescription(java.lang.String value);

java.util.Calendar getDate();

void setDate(java.util.Calendar value);

long getNumber();

void setNumber(long value);

}

M / J

XML

JAXBContext jaxbCtx = JAXBContext.newInstance("generated");

Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller();

OrdersListType orders

= (OrdersListType)unmarshaller.unmarshal( new File("simple-order.xml"));

List ordersList = orders.getOrder();

for (int i = 0; i < ordersList.size(); i++) { OrderType order = (OrderType)ordersList.get(i);

System.out.println("Number = " + order.getNumber() + ", amount = " + order.getAmount());

}

...

M / J

XML

...

ObjectFactory objectFactory = new ObjectFactory();

Order newOrder = objectFactory.createOrder(); newOrder.setNumber(2006); newOrder.setDate(Calendar.getInstance()); newOrder.setDescription("New Order"); newOrder.setAmount(100);

orders.getOrder().add(newOrder);

Marshaller marshaller = jaxbCtx.createMarshaller(); marshaller.marshal(orders,

new FileOutputStream(new File("simple-order.xml")));

1 5 5

#$%&'%() &*)+,-../

0,+1,22.%3

4)0,+1,22.%3

*+&')+5, 6

javax.xml.bind.JAXBContext - " " JAXB API

public abstract class JAXBContext {

static JAXBContext newInstance(String contextPath) static JAXBContext newInstance(String contextPath,

ClassLoader contextPathCL)

abstract Unmarshaller createUnmarshaller(); abstract Marshaller createMarshaller(); abstract Validator createValidator();

}

4

1 . 1S

7!89 *+)4&$:,'2;): :+. ',+.,%:, *+&')+5. 8 4&5<0)%:,/

'& '+)0; 4)0,+1,22.%3, = 6

*& :+)>&',%.? = 6

@%, 2):<@ = 6

Validator

public interface Validator {

ValidationEventHandler getEventHandler()

void setEventHandler(ValidationEventHandler handler)

boolean validate(java.lang.Object subrootObject) boolean validateRoot(java.lang.Object rootObject)

}

Z c i

Unmarshaller XML ,

!

public interface Unmarshaller {

boolean isValidating() throws JAXBException;

void setValidating(boolean b) throws JAXBException;

ValidationEventHandler getEventHandler() throws JAXBException;

void setEventHandler(ValidationEventHandler handler) throws JAXBException;

Object unmarshal(File file) throws JAXBException; Object unmarshal(InputStream is) throws JAXBException; Object unmarshal(URL url) throws JAXBException; Object unmarshal(Source source) throws JAXBException; Object unmarshal(Node node) throws JAXBException; Object unmarshal(InputSource is) throws JAXBException;

}

22.05.2007

j c i

Marshaller ! XML

public interface Marshaller {

ValidationEventHandler getEventHandler() throws JAXBException; void setEventHandler(ValidationEventHandler handler)

throws JAXBException;

void marshal(Object o, OutputStream os) throws JAXBException; void marshal(Object o, Writer writer) throws JAXBException; void marshal(Object o, Result result) throws JAXBException; void marshal(Object o, Node node) throws JAXBException;

void marshal(Object o, ContentHandler ch) throws JAXBException; Node getNode(Object o) throws JAXBException;

Object getProperty(String s) throws PropertyException;

void setProperty(String s, Object o) throws PropertyException;

String JAXB_ENCODING = "jaxb.encoding";

String JAXB_FORMATTED_OUTPUT = "jaxb.formatted.output";

}

5 M

8 A0;

A0; B2,$$,

A0; C):&4,

A0; B&%$:,%:(

 

 

 

 

#76")O ": #"

76")O ": #"

?" 76")O ": #"

> <LO L:

 

 

 

 

$ H" kl

$ H" kl

?" $ H" kl

: D ILkl

 

 

 

 

$ #"9H7 '9) '"

: #"D7 '< '"

?" : #"D7 '< '"

: LD>@mL< m

 

 

 

 

& '" L;B$ 9 '

= '" B$ O'

?" = '" B$ O'

=@m IL K:O@LOm I

 

 

 

 

5 ] 2 . ]

" XML Java

<xsd:complexType name="personType"> <xsd:sequence>

<xsd:element name="name" type="xsd:string"/> <xsd:element name="departmentId" type="xsd:long"/>

</xsd:sequence>

<xsd:attribute name="id" type="xsd:long"/> </xsd:complexType>

public interface PersonType

{

long getDepartmentId();

void setDepartmentId(long value); java.lang.String getName();

void setName(java.lang.String value); long getId();

void setId(long value);

}

_E &;" 7" F

#$ XML Java

D+&$:&) $'&E$:'& F G H

<xsd:element name="name" type="xsd:string"/>

java.lang.String getName();

void setName(java.lang.String value);

% (Collection Property)

<xsd:element name="item" type="xsd:string" maxOccurs="unbounded"/>

java.util.List getItem();

5

4 E@A;" 8" $B#" 7&$F

&

« » & (Type Safe Enumeration)

<xsd:simpleType name="countryType">

<xsd:restriction base="xsd:NCName">

<xsd:enumeration value="FRANCE"/>

<xsd:enumeration value="SWITZERLAND"/>

</xsd:restriction>

</xsd:simpleType>

public class CountryType { private final String value;

protected CountryType(String v) { value = v; } public final static CountryType FRANCE

= new CountryType("FRANCE");

public final static CountryType SWITZERLAND

= new generated.CountryType("SWITZERLAND");

...

}

5 ] 2 . ]

I,>2.-, $&&:'):$:'.; '$:+&)%%(J :.*&'F 9 = KH

XML

Java

 

 

xsd:string

java.lang.String

 

 

xsd:integer

java.math.BigInteger

 

 

xsd:int

int

 

 

xsd:long

long

 

 

xsd:decimal

java.math.BigDecimal

 

 

xsd:float

float

 

 

xsd:double

double

 

 

xsd:boolean

boolean

 

 

xsd:byte

byte

 

 

xsd:dateTime

java.util.Calendar

 

 

xsd:anySimpleType

java.lang.String

 

 

22.05.2007

n .

L,$2)4&',%.) 8 :.*&' M)+)N &3+,%.M)%.)

.2. +,$1.+)%.) O 7 6 %,$2)4&',%.)

<xsd:complexType name="Address"> <xsd:sequence>

<xsd:element name="name" type="xsd:string"/> <xsd:element name="street" type="xsd:string"/> <xsd:element name="city" type="xsd:string"/>

</xsd:sequence>

</xsd:complexType> <xsd:complexType name="USAddress">

<xsd:complexContent> <xsd:extension base="Address">

<xsd:sequence>

<xsd:element name="state" type="xsd:string"/> <xsd:element name="zip" type="xsd:integer"/>

</xsd:sequence>

</xsd:extension>

</xsd:complexContent>

</xsd:complexType>

 

 

n .

 

 

J +

 

 

o . 4 2

L,$2)4&',%.) 8 :.*&' M)+)N &3+,%.M)%.)

 

7!89 $2)4<): .$*&2PN&',:P 5&34, %<Q%&/

 

 

.2. +,$1.+)%.) O 7 6 %,$2)4&',%.)

 

 

^ 2 E<@<F 2 9

 

 

public interface Address

 

R&N4,',:P &>S)5:%() *+)4$:,'2)%.; 4,%%(J 8

 

 

. 1S

{

 

 

#>+,>,:(',:P :&2P5& ')+%() 4,%%()

 

Z . 1S ( / ^ i

 

 

java.lang.String

getCity();

 

 

 

 

void setCity(java.lang.String value);

 

D+)&>+,N&'(',:P 4,%%() 5 +,N2.M%(0 :.*,0

 

 

2 / 5 . Si_. 1S

 

 

java.lang.String

getStreet();

 

 

 

 

 

 

 

 

void setStreet(java.lang.String value);

 

 

 

 

 

 

 

 

java.lang.String

getName();

 

JAXP $ :

 

 

 

 

 

void setName(java.lang.String value);

 

 

 

 

 

 

 

 

' , DTD

 

 

 

}

 

 

 

 

 

 

 

 

 

 

 

 

' , !

 

 

 

 

 

public interface USAddress extends Address

 

 

 

 

 

 

{

 

 

 

& XSLT

 

 

 

 

 

java.lang.String

getState();

 

 

 

 

 

 

 

' ( XML

 

 

 

 

 

void setState(java.lang.String value);

 

 

 

 

 

 

 

 

 

 

 

 

 

 

long getZip();

 

 

 

 

 

 

 

 

 

void setZip(long value);

 

 

 

 

 

 

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

6

. 4 2 i 2

^ <@<9 2 2 9. 1S (

2 pq i . S] 5_2 1+

<list>

<listElement id=“1” next=“2”/> <listElement id=“2” next=“3” prev=“1”/> <listElement id=“3” prev=“2”/>

</list>

<!ELEMENT list listElement* > <!ELEMENT listElement EMPTY> <!ATTLIST listElement

id ID #REQUIRED next ID #IMPLIED prev ID #IMPLIED >

22.05.2007

. 4 i 2

 

2 i

Z 9. 1S

 

<xsl:stylesheet>

<d>

 

<xsl:template match="/“>

 

 

<e t=“a”/>

 

<xsl:for-each select=“*">

 

 

<e t=“b”/>

 

<xsl:element name=“{@t}">

 

<xsl:attribute name=“t">

<e t=“c”/>

 

 

<xsl:value-of select=“name()”/>

</d>

 

 

</xsl:attribute>

^ @92 i S 2 / 5 . 1S M

 

 

</xsl:element>

<d>

 

</xsl:for-each>

<a t=“e”/>

 

</xsl:template>

<b t=“e”/>

 

</xsl:stylesheet>

<c t=“e”/>

 

 

</d>

 

 

 

 

 

7

" )/01234356+

,

-.$

' (-. (

- &-#$

( ( #-

(7

!

"

#$

!

% & !

&

' (

(

8$( 9:

!

" # $" !

% & " !

' () " *

22.05.2007

" )* # +

,

(-. (

! !

" 9:

(middleware),

1

22.05.2007

 

; & 9:

 

 

, +) '

 

 

 

, )

 

% +) '

 

'

 

 

; < = #$#

 

 

 

 

7

 

 

 

)

 

 

– -% ' ' ' ' '

 

 

>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

– . "/ '

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

& " '

 

 

– 0 ) ' 1 "$+$

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

1 ($2 ($* *

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

!

 

 

 

 

3 4 " 2 4 (

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

– , )$2 4$* *

 

 

 

 

!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

– 5 % ' ( " 6 7 89:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

– ; 4

 

 

"

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

#

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

( )

 

 

 

 

 

 

 

 

 

 

 

 

 

5 )

 

 

<

 

= & " '

?$

 

% #$ # .

 

* & #

 

 

 

 

 

 

– @ # ) ( #

 

 

 

 

 

 

@ # #$

 

?#$# # =-

 

 

!+

 

% #$ = !-# AB6C25D1C

 

 

!

 

 

– L = &

 

 

 

9C50B0603B EDBFGDFC )A9E+

 

 

 

 

 

 

 

 

 

" = ! =#-(-

 

– N & )

 

 

 

 

.-#-7 7

 

# +

 

 

 

% = ! 7 " #$ 7

 

 

 

 

 

 

 

 

 

% ! )0B6C23HC2DI0J06K+

 

 

 

 

 

 

)H326DI0J06K+

 

 

 

 

 

L 7 M = #-. !

 

 

 

 

 

 

(- $( !7

 

 

 

 

 

" (

 

 

 

 

 

 

 

 

 

2

, " & " '

 

 

 

 

$

# "

 

 

$

% " "

 

 

$

& "

 

 

" &

8 ! OPQ

22.05.2007

> ? % & "

%$

! OPQ

3.

4.!

8.

 

 

9.

 

 

10.

"

OPQ )OCR36C P231CSG2C QDJJ+

@

3