1 / 33

Introduction to XML Programming Models

Explore different programming models for XML, including distributed and web-based models. Learn about XML basics, user-defined types, creating XML documents, and working with XML schemas.

Download Presentation

Introduction to XML Programming Models

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to XML

  2. Programming models

  3. 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

  4. 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

  5. ports App1 sockets Application client App2 App3 Direct Connections App1 Application client Remote Procedures NDS App2 App3 Examples: Java’s rmi, CORBA

  6. XML basics

  7. 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

  8. 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:

  9. 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

  10. 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

  11. 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)> ]>

  12. 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

  13. 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.

  14. 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

  15. XMLSpy

  16. 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.

  17. 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 -- >

  18. How is XML Useful Part I Simple Mortgage Calculator

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

  24. XML-based architecture web browser “hand-rolled” XML WebServer http File system python CGI “hand-rolled” XML XML

  25. 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?

  26. 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!

  27. 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

  28. Lottery application

  29. 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.

  30. 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

  31. Software requirements • Secure login • House name • Password • Remotely accessible • Prototypes: • Standalone excel • Web-based

  32. Architectural requirements filesystem XML Login Info login Web Client Web Server XML XML Student Data XSL lottery

  33. 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

More Related