by Cheryl M. Hughes

internalchildlikeInternet και Εφαρμογές Web

12 Νοε 2013 (πριν από 3 χρόνια και 5 μήνες)

71 εμφανίσεις

Copyright © 2003 Pearson Education, Inc.

Slide 8
-
1

Created by Cheryl M. Hughes, Harvard University Extension School


Cambridge, MA

The Web Wizard’s Guide to XML

by Cheryl M. Hughes

Copyright © 2003 Pearson Education, Inc.

Slide 8
-
2

CHAPTER 8

XML Programs and Programming


Copyright © 2003 Pearson Education, Inc.

Slide 8
-
3

XML Programming


Some applications for processing XML:


Parsers


Document Object Model (DOM)


The Simple Application Programming
Interface for XML (SAX)


Programming languages (ie


Java)


Databases


Copyright © 2003 Pearson Education, Inc.

Slide 8
-
4

XML Parsers


Two types of parsers:


Validating


checks document against a
document model


Non
-
validating


only checks syntax


Parsers are code libraries written in a
programming language


The XML parser that comes with Internet
Explorer is called the MSXML parser




Copyright © 2003 Pearson Education, Inc.

Slide 8
-
5

The Document Object Model (DOM)


The first DOM specification was released by the W3C in
October of 1998


Three levels of the recommendation:


Level 1
-

provides the core document models



Level 2
-

includes Level 1 and adds a model for style
sheets,


Level 3
-

includes Level 1 and adds a model for content
(DTD or schema)


What DOM does: takes an XML document as input and
creates an object structure in memory, which can then be
accessed by programs


DOM creates a tree
-
like structure, with branches and leaves
to represent the hierarchy of the document


Copyright © 2003 Pearson Education, Inc.

Slide 8
-
6

DOM Example


Microsoft’s XMLDOM


XMLDOM
is a COM component that is included with the
Internet Explorer 5.0


The DOM creates an object in memory that can then be
accessed by programs


Some DOM object properties:


childNodes


firstChild


lastChild


Some DOM object methods:


Load()


CreateNode()


Save()



Copyright © 2003 Pearson Education, Inc.

Slide 8
-
7

DOM Example


XML file

1

<?xml version=
”1.0”
?>

2

<job
-
posting>

3


<title>
Job Title:
<emphasis>
Web master
</emphasis></title>

4


<description>
We are looking for a Web master to oversee the management

5


of our company Web site. The Web master will be responsible for

6


working with other staff members to collect information for the

7


Web site, and for creating and maintaining the Web

8


pages.
</description>

9


<skill
-
list>

10



<skill>
Basic writing skills
</skill>

11



<skill>
good communication skills
</skill>

12



<skill>
HTML
</skill>

13


</skill
-
list>

14

</job
-
posting>

Copyright © 2003 Pearson Education, Inc.

Slide 8
-
8

DOM Example


HTML file with
Javascript

1

<html>

2

<head>

3

<title>
Microsoft DOM Example Using Javascript
</title>

4

<body>

5

<script

language="javascript"
>

6

/* This is a comment in JavaScript */

7

/* Create a new DOM object for our document */

8

var xmlDocument

=
new ActiveXObject(
"Microsoft.XMLDOM"
)
;

9

xmlDocument.async=
"false"
;



10

/* Use the "load" method to read our XML document into memory */

11

xmlDocument.load(
"job
-
posting.xml"
);



12

/* Print some HTML code */

13

document.write
(
"<p><strong>Using the Microsoft DOM</strong></p>"
);



14

/* BEGIN THE EXAMPLES */



15

/* 1. Prints the value of the first child node */

16

document.write(
"<p>1. <strong>firstChild</strong> is: "
);

17

document.write(
xmlDocument.documentElement.firstChild.text
);

18

document.write(
"</p>"
);


Copyright © 2003 Pearson Education, Inc.

Slide 8
-
9

DOM Example


HTML file with
Javascript (cont.)



19

/* 2. Prints the value of the last child node */

20

document.write(
"<p>2. <strong>lastChild</strong> is: "
);

21

document.write(
xmlDocument.documentElement.lastChild.text
);

22

document.write(
"</p>"
);



23

/* 3. Prints the name of the lastChild element */

24

document.write(
"<p>3. <strong>lastChild</strong> node name is: "
);

25

document.write(
xmlDocument.documentElement.lastChild.nodeName
);

26

document.write(
"</p>"
);



27

/* 4. Prints the name of the child node stored in item(1) */

28

/* Then, it checks to see if the node has child elements */

29

document.write(
"<p>4. <strong>item(2)</strong> is: "
);

30

document.write(
xmlDocument.documentElement.childNodes.item(2).nodeName
);

31

document.write(
"</p><p> Does this node have child elements? "
);

32

document.write


(
xmlDocument.documentElement.childNodes.item(2).hasChildNodes()
);

33

document.write(
"</p>"
);



34

/* End the examples */

35

</script>

36

</body>

37

</html>



Copyright © 2003 Pearson Education, Inc.

Slide 8
-
10

The Simple Application Programming
Interface for XML

(SAX)


The SAX 1.0 recommendation was
released by the W3C in May 1998



SAX is an event
-
based API

that
reads the
document in a serial fashion



Sax is much faster than DOM, and does
not store information in memory



Copyright © 2003 Pearson Education, Inc.

Slide 8
-
11

SAX Example


XML File:

<?xml version=
”1.0”
?>


<book id=”1234”>



<title>The Web Wizard’s Guide to XML</title>



<author>C. Hughes</author>


</book>



SAX processor events:


1


Start document


2


Start element (book)


3


Attribute (id=”1234”)


4


Start element (title)


5


Text (The Web Wizard’s Guide to XML)


6


End element (title)


7


Start element (author)


8


Text (C. Hughes)


9


End element (author)


10


End element (book)


11


End document


Copyright © 2003 Pearson Education, Inc.

Slide 8
-
12

Comparing SAX and DOM



When to use SAX:


Processing large documents


Searching


Stopping the program


When to use DOM:


Accessing cross
-
referenced data


Modifying the XML document


Creating new XML documents

Copyright © 2003 Pearson Education, Inc.

Slide 8
-
13

XML and Programming Languages


XML has become popular with object
-
oriented programming languages


DOM created objects that can be easily
accessed


Some popular languages that are
embracing XML:


Java


Perl


C++

Copyright © 2003 Pearson Education, Inc.

Slide 8
-
14

XML and Databases


XML’s structure makes it a good
technology for storing data


XML can support relationships among
pieces of data that relational databases
can not


XML Query Language (XQuery)


being
developed as a standardized query
language that will span all types of XML
documents and data sources

Copyright © 2003 Pearson Education, Inc.

Slide 8
-
15

Examples of XML Programs


Distributed Authoring and Versioning on the
World Wide Web (WebDAV)


Wireless Application Protocol (WAP)



Scalable Vector Graphics (SVG)



Open Financial Exchange (OFX)



Mathematical Markup Language (MathML)


Chemical Markup Language (CML)



Extensible Hypertext Markup Language (XHTML)



Resource Description Framework (RDF)


Copyright © 2003 Pearson Education, Inc.

Slide 8
-
16

MathML Examples


Equation: 3+5



1
<mrow>






2


<mn>
3
</mn>



3


<mo>

+
</mo>



4


<mn>

5
</mn>



5

</mrow>



Equation: ab



1
<mrow>






2


<mi>

x
</mi>



3


<mo>

&InvisibleTimes;
</mo>



4


<mi>

b
</mi>



5

</mrow>