330 likes | 341 Views
Learn about different programming models for XML, including distributed and web-based models. Explore XML basics, data manipulation, and user-defined types. Discover how XML can be used to represent structured information and create XML documents using XMLSpy.
E N D
Distributed programming modelsTypical Web-based Easy to deploy but slow, not great user experience database html browser WebServer http • Many programming models • JSP • Servlets • PHP • CGI (python, perl, C) • Cold Fusion Dynamically Generated html html plus optionally JavaScript to jazz up html
Distributed programming modelsTypical Web-based Better user experience. Heavier, less portable, requires socket programming to stream to server. html database WebServer http applet socket Dynamically Generated html html + applet
ports App1 sockets Application client App2 App3 Direct Connections App1 Application client Remote Procedures NDS App2 App3 Examples: Java’s rmi, CORBA
XML Basics, cont • Most modern languages have method of representing structured data. • Typical flow of events in application Read data (file, db, socket) Marshal objects Manipulate in program 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
Fortran Java C type Student character(len=*) :: name character(len=*) :: ssn integer :: age real :: gpa end type Student class Student{ public String name; public String ssn; public int age; public float gpa; } struct Student{ char* name; char* ssn; int age; float gpa; } User-defined types in programming languages • XML is a text-based, programming-language-neutral way of representing structured information. Compare:
Sample 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 this For now
Alternative 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: 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 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) :: s1 s1%name = ‘Andrew’ . C Java Fortran
Creating XML documents • XML is not a programming language! • In XML we make a Student “object” in an xml file (Student.xml): <Student> <name>Andrew</name> <ssn>123-45-6789</ssn> <age>36</age> <gpa>2.0</gpa> </Student> • Think of this as like a serialized object.
XML and Schema • Note that there are two parts to what we did • Defining the “structure” layout • Defining an “instance” of the structure • The first is done with an appropriate Schema or DTD. • The second is the XML part • Both can go in the same file, or an XML file can refer to an external Schema or DTD (typical) • From this point on we use only Schema
XMLSpy • Excellent tool for both learning and developing XML. • Many XML books contain free 90-day license. Otherwise, free 30-day license on web page for professional and totally free home edition • Try to use XMLSpy to create example just covered.
Aspects of XML syntax • It is illegal to omit closing tags • XML tags are case-sensitive • XML elements must be properly nested • XML elements must have a root element • XML preserves whitespaces • XML comments: < -- This is a comment -- >
How is XML Useful Part I Simple Mortgage Calculator
Mortgage payment calculator • Design a simple application which does the following: • Accepts user input • Loan amount • Loan term • Interest rate • Extras (assessments + taxes) • Returns per-month table of • total payment • interest • Principal • Some other fun stuff
Mortgage Calculator General Requirements • Must be • Clean simple interface (easy) • Remotely accessible with security • Portable • Not require too much installation on the part of the user • Sufficiently fast not to be embarrassing
Some possible architectures • Web server • Server-side scripting with pure html • Server-side scripting with html+javascript • Server-side scripting with html+applet • Direct connection • Raw sockets • Distributed objects
Initial architecture • Front-end: pure html form • Back end: python cgi (similar to java servlet) • Python generates web page dynamically after making calculations • No use of higher-level web generation libraries at this point • What are advantages/disadvantages of this architecture? • Run application: • http://masters.cs.uchicago.edu/~asiegel/courses/cspp53025/caseStudies/mortgage
Disadvantages • Two obvious disadvantages are: • Formatted web content in print statements low-level, ugly error prone • Data is not decoupled from formatting. What if we want to switch to an application client? • Several strategies can help with both of these (higher-level htmlgen libraries, server-side scripting model, beans, etc.) and XML • We will look at how XML fits in
XML-based architecture web browser “hand-rolled” XML WebServer http File system python CGI “hand-rolled” XML XML
Observations/questions • What does browser do with XML? • Can it display • Does it even understand XML? • If not, what good is this? • Do we have to hand roll our programming language objects from XML?
Some answers • Regarding first point, try this with your web browser • Note that XML is displayed/formatted nicely, but not nearly to the same level of utility as the html table • To add formatting instructions, we must associate a separate XSL file with the XML file. We will study XSL soon. • Regarding XML-language conversion, we will study language binding for various high-level ways of doing this! For now, we will hand-roll ourselves!
XSL • We will not cover details of XSL until the third week. • However, for now we can easily create XSL at a high level using XMLSpy • See example application
Lottery overview • Given a list of student members of a dormitory, perform an ordered randomized sort of the students to determine a room draft order.
Lottery details • Students are defined by • Last name • First name • Seniority • Quarters in the House • Quarters in the College • The sort keys are • Quarters in House • Quarters in College • Random
Software requirements • Secure login • House name • Password • Remotely accessible • Prototypes: • Standalone excel • Web-based
Architectural requirements filesystem XML Login Info login Web Client Web Server XML XML Student Data XSL lottery
Next Step • Implement Lottery as specified • You must have the following: • Student Schema • Password Schema • Sample student xml • Sample password xml • Simple XSL for display