introduction to xml. programming models distributed programming models typical web-based easy to...

33
Introduction to Introduction to XML XML

Post on 20-Dec-2015

233 views

Category:

Documents


0 download

TRANSCRIPT

Introduction to XMLIntroduction to XML

Programming modelsProgramming models

Distributed programming modelsDistributed programming modelsTypical Web-basedTypical Web-based

Easy to deploy but slow, not great user experienceEasy to deploy but slow, not great user experience

htmlbrowser

WebServer

http

DynamicallyGenerated

html

Many programming models•JSP•Servlets•PHP•CGI (python, perl, C)•Cold Fusion

html

plus optionallyJavaScript to jazz up html

database

Distributed programming modelsDistributed programming modelsTypical Web-basedTypical Web-based

Better user experience. Heavier, less portable, requires Better user experience. Heavier, less portable, requires socket programming to stream to server.socket programming to stream to server.

WebServer

http

DynamicallyGenerated

html

html + applet

databaseapplet

html

socket

Direct ConnectionsDirect Connections

Application client

App1sockets

App2

App3

ports

Application client

App1

Remote ProceduresApp2

App3N

DS

Examples: Java’s rmi, CORBA

XML basicsXML basics

XML Basics, contXML Basics, cont

Most modern languages have method of Most modern languages have method of representing structured data.representing structured data.

Typical flow of events in applicationTypical flow of events in application

Read data(file, db, socket)

Marshalobjects

Manipulate inprogram

Unmarshal (file, db, socket)

•Many language-specific technologies to reduce these steps: RMI, object serialization in any language, CORBA (actually somewhat language neutral), MPI, etc.

•XML provides a very appealing alternative that hits the sweet spot for many applications

User-defined types in programming User-defined types in programming languageslanguages

XML is a text-based, programming-language-XML is a text-based, programming-language-neutral way of representing structured neutral way of representing structured information. Compare:information. Compare:

struct Student{ char* name; char* ssn; int age; float gpa;}

class Student{ public String name; public String ssn; public int age; public float gpa;}

C Java Fortrantype Student character(len=*) :: name character(len=*) :: ssn integer :: age real :: gpaend type Student

Sample XML SchemaSample XML Schema

•In XML, datatype description is called a schema.

<?xml version="1.0" encoding="UTF-8"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:element name="student"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="ssn" type="xs:string"/> <xs:element name="age" type="xs:integer"/> <xs:element name="gpa" type="xs:decimal"/> </xs:sequence> </xs:complexType> </xs:element></xs:schema>

Ignore thisFor now

Alternative schemaAlternative schema

In this example studentType is defined separately rather than anonymously

<xs:schema> <xs:element name="student" type="studentType“/> <xs:complexType name="studentType"> <xs:sequence>

<xs:element name="name" type="xs:string"/><xs:element name="ssn" type="xs:string"/><xs:element name="age" type="xs:integer"/><xs:element name="gpa" type="xs:decimal"/>

</xs:sequence> </xs:complexType></xs:schema>

new type defined separately

Alternative: DTDAlternative: DTD

Can also use a DTD (Document Type Descriptor), but this is probably becoming obsolete (notice the lack of types)

<!DOCTYPE Student [ <! – Each XML file is stored in a document whose name is the same as the root node -- > <! ELEMENT Student (name,ssn,age,gpa)> <! – Student has four attributes -- > <!ELEMENT name (#PCDATA)> <! – name is parsed character data -- > <!ELEMENT ssn (#PCDATA)> <!ELEMENT age (#PCDATA)> <!ELEMENT gpa (#PCDATA)>]>

Creating instances of typesCreating instances of types

In programming languages, we instantiate objects:

struct Student s1, s2;s1.name = “Andrew”s1.ssn=“123-45-6789”;

Student s = new Student();s1.name = “Andrew”;s1.ssn=“123-45-6789”;.type(Student) :: s1s1%name = ‘Andrew’.

C

Java

Fortran

Creating XML documentsCreating XML documents

XML is XML is notnot a programming language! a programming language!

In XML we make a Student “object” in an xml file In XML we make a Student “object” in an xml file (Student.xml):(Student.xml):

<Student><Student>

<name>Andrew</name><name>Andrew</name>

<ssn>123-45-6789</ssn><ssn>123-45-6789</ssn>

<age>36</age><age>36</age>

<gpa>2.0</gpa><gpa>2.0</gpa>

</Student> </Student>

Think of this as like a serialized object.Think of this as like a serialized object.

XML and SchemaXML and Schema

Note that there are two parts to what we didNote that there are two parts to what we did Defining the “structure” layoutDefining the “structure” layout Defining an “instance” of the structureDefining an “instance” of the structure

The first is done with an appropriate Schema The first is done with an appropriate Schema or DTD.or DTD.The second is the XML partThe second is the XML partBoth can go in the same file, or an XML file Both can go in the same file, or an XML file can refer to an external Schema or DTD can refer to an external Schema or DTD (typical)(typical)From this point on we use only SchemaFrom this point on we use only Schema

XMLSpyXMLSpy

XMLSpyXMLSpy

Excellent tool for both Excellent tool for both learninglearning and and developing developing XML.XML.

Many XML books contain free 90-day license. Many XML books contain free 90-day license. Otherwise, free 30-day license on web page for Otherwise, free 30-day license on web page for professional and totally free home editionprofessional and totally free home edition

Try to use XMLSpy to create example just Try to use XMLSpy to create example just covered.covered.

Aspects of XML syntaxAspects of XML syntax

It is illegal to omit closing tagsIt is illegal to omit closing tags

XML tags are case-sensitiveXML tags are case-sensitive

XML elements must be properly nestedXML elements must be properly nested

XML elements must have a root elementXML elements must have a root element

XML preserves whitespacesXML preserves whitespaces

XML comments:XML comments:

< -- This is a comment -- >< -- This is a comment -- >

How is XML UsefulHow is XML Useful

Part IPart I

Simple Mortgage CalculatorSimple Mortgage Calculator

Mortgage payment calculatorMortgage payment calculator

Design a simple application which does the Design a simple application which does the following:following: Accepts user inputAccepts user input

Loan amountLoan amount

Loan termLoan term

Interest rateInterest rate

Extras (assessments + taxes)Extras (assessments + taxes) Returns per-month table ofReturns per-month table of

total paymenttotal payment

interestinterest

PrincipalPrincipal

Some other fun stuffSome other fun stuff

Mortgage Calculator General Mortgage Calculator General RequirementsRequirements

Must beMust be Clean simple interface (easy)Clean simple interface (easy) Remotely accessible with securityRemotely accessible with security PortablePortable Not require too much installation on the part Not require too much installation on the part

of the userof the user Sufficiently fast not to be embarrassingSufficiently fast not to be embarrassing

Some possible architecturesSome possible architectures

Web serverWeb server Server-side scripting with pure htmlServer-side scripting with pure html Server-side scripting with html+javascriptServer-side scripting with html+javascript Server-side scripting with html+appletServer-side scripting with html+applet

Direct connectionDirect connection Raw socketsRaw sockets Distributed objectsDistributed objects

Initial architectureInitial architecture

Front-end: pure html formFront-end: pure html form

Back end: python cgi (similar to java servlet)Back end: python cgi (similar to java servlet) Python generates web page dynamically after making Python generates web page dynamically after making

calculationscalculations No use of higher-level web generation libraries at this pointNo use of higher-level web generation libraries at this point

What are advantages/disadvantages of this architecture?What are advantages/disadvantages of this architecture?

Run application: Run application: http://masters.cs.uchicago.edu/~asiegel/courses/cspp53025/caseStudies/mortgagehttp://masters.cs.uchicago.edu/~asiegel/courses/cspp53025/caseStudies/mortgage

DisadvantagesDisadvantages

Two obvious disadvantages are:Two obvious disadvantages are: Formatted web content in print statements low-level, Formatted web content in print statements low-level,

ugly error proneugly error prone Data is not decoupled from formatting. What if we Data is not decoupled from formatting. What if we

want to switch to an application client?want to switch to an application client?

Several strategies can help with both of these Several strategies can help with both of these (higher-level htmlgen libraries, server-side (higher-level htmlgen libraries, server-side scripting model, beans, etc.) and XMLscripting model, beans, etc.) and XML

We will look at how XML fits in We will look at how XML fits in

XML-based architectureXML-based architecture

webbrowser

WebServer

http

“hand-rolled”XML

XML

pyth

on C

GI

“hand-rolled”XML

File system

Observations/questionsObservations/questions

What does browser do with XML?What does browser do with XML? Can it displayCan it display Does it even understand XML?Does it even understand XML?

If not, what good is this?If not, what good is this?

Do we have to hand roll our programming Do we have to hand roll our programming language objects from XML?language objects from XML?

Some answersSome answers

Regarding first point, try this with your web Regarding first point, try this with your web browserbrowser Note that XML is displayed/formatted nicely, but not Note that XML is displayed/formatted nicely, but not

nearly to the same level of utility as the html tablenearly to the same level of utility as the html table To add formatting instructions, we must associate a To add formatting instructions, we must associate a

separate separate XSLXSL file with the XML file. We will study XSL file with the XML file. We will study XSL soon.soon.

Regarding XML-language conversion, we will Regarding XML-language conversion, we will study language binding for various high-level study language binding for various high-level ways of doing this! For now, we will hand-roll ways of doing this! For now, we will hand-roll ourselves!ourselves!

XSLXSL

We will not cover details of XSL until the We will not cover details of XSL until the third week.third week.

However, for now we can easily create However, for now we can easily create XSL at a high level using XMLSpyXSL at a high level using XMLSpy

See example applicationSee example application

Lottery applicationLottery application

Lottery overviewLottery overview

Given a list of student members of a Given a list of student members of a dormitory, perform an ordered randomized dormitory, perform an ordered randomized sort of the students to determine a room sort of the students to determine a room draft order.draft order.

Lottery detailsLottery details

Students are defined byStudents are defined by Last nameLast name First nameFirst name SenioritySeniority

Quarters in the HouseQuarters in the HouseQuarters in the CollegeQuarters in the College

The sort keys areThe sort keys are1.1. Quarters in HouseQuarters in House2.2. Quarters in CollegeQuarters in College3.3. RandomRandom

Software requirementsSoftware requirements

Secure loginSecure login House nameHouse name PasswordPassword

Remotely accessibleRemotely accessible

Prototypes:Prototypes: Standalone Standalone excelexcel Web-basedWeb-based

Architectural requirementsArchitectural requirements

XMLLoginInfo

XMLStudent

Data

filesystem

WebServer

login

lottery

WebClient

XML

XSL

Next StepNext Step

Implement Lottery as specifiedImplement Lottery as specified

You must have the following:You must have the following: Student SchemaStudent Schema Password SchemaPassword Schema Sample student xmlSample student xml Sample password xmlSample password xml Simple XSL for displaySimple XSL for display