Java XMLOutlineXMLExample HTMLExampleXML is notXML isDifferences from HTMLExamplesVocabularyDocument TypesDTDReading a DTDElementAttribute ListsSlide 16Slide 17ConformityDTD LimitationsProgramming with XMLDOMDOM RepresentationSlide 23Working with the DOMSlide 25Better with ValidationSlide 27Problems with DOMSAXSAX EventsSlide 31How to writeSlide 33XML OutputSlide 35Java XMLIS 3136.3.2003OutlineXML documentsXML in JavaDOMSAXXML OutputXMLHTML-like markupSemantic meaningExample HTML<html><h3>Message</h3><b>To:</b> [email protected]<br><b>From:</b> [email protected]<br><b>Subject:</b>XML Is Really Cool<p><hr>How many ways is XML cool? Let me count the ways...<p></html>Example<message> <to>[email protected]</to> <from>[email protected]</from><subject>XML Is Really Cool</subject> <text> How many ways is XML cool? Let me count the ways... </text></message>XML is nota languageIt is a standard for creating task-specific formal languagesIn the pastevery application has its own representationdifficult for applications to interoperateXML ishuman readablenot platform-specificnot presentationalfixed syntaxvalidity separate from interpretationDifferences from HTMLcase-sensitiveend tags not optionalempty tags have special syntaxattribute values must be in quotesXHTMLExamplesmembersflightsVocabularyHeader<?xml version=“1.0”?>ElementsmembermembersAttributeslevel=“standard”Entities&Document TypesA document type is specification for a family of documentsemail messagesmember account data filesDefining a document typedeciding what can be saidhow it can be saidattaching meaningful labelsDTDDocument Type Definitionone of many proposals for defining XML languagesothersXML Schema, Schematron, DSD, RelaxDTDsbased on the original SGML DTDsnot written in XMLlimited in certain respectsEasiest to learnReading a DTD<!ELEMENTdefines an element<!ATTLISTassociates elements and attributes<!ENTITYdefines entitiesElementElement namecontent modeltext or other dataelementssequenceschoicesAttribute ListsName of elementFor each attributenamecontent typevaluespresencedefaultExamplesElement<!ELEMENT members (member*)><!ELEMENT member (name, enroll-date, last-flight-date?, miles, benefit-level)>Attlist<!ATTLIST member id CDATA #REQUIRED><!ATTLIST benefit-levellevel (standard | silver | gold | platinum) "standard">Examplesflights.dtdmembers.dtdConformityIf I just have an XML documentI have some dataIf I have XML document and DTDI have some idea what the document is supposed to representIf I have XML document validated by DTDI know that the document is organized as the DTD saysDTD LimitationsNo way to specify types of thingsid must be a numberorigin must be a three-character stringDefault values only for attributesMust have a separate DTD parsernot XMLNo import or includeNo inheritanceOO element declarationProgramming with XMLBasic problemuse an XML document in a Java programTwo waysBuild an internal representation of the whole thingExtract just the parts you wantDOMDocument Object ModelModelof the XML documentcomposed of objectsThink of the document as a treeDOM RepresentationDOM RepresentationNodestext nodeselement nodeswhitespace nodesList of childrenordered top to bottomWorking with the DOMNavigate from node to nodefrom Node to ChildListfrom ChildList to NodeEasier with XPath/member[0]/name/lastExampleDOMExtractExtractlast namemilesbenefit levelBetter with ValidationIf we validate the documentwe know that the document obeys the structuresimpler destructuringExampleDOMExtract2Problems with DOMMay not want the whole documentToo bigOnly interested in certain partsMemory intensiveSlowSAXSimple API for XMLWatch the parser parseParsing = a stream of eventseach event calls a methodlike GUI event handlingSAX EventsstartDocumentendDocumentstartElementendElementcharacterserrorsExamplemembers.xmlHow to writestate machinebeginnings and ends of elements change statein each stateprocess relevant informationExampleSAXExtractXML OutputRather awkwardUse XML transformationsbut don’t do any transformingprovides an output streamExampleDOMOutputReads in the fileThen adds 1000 to each miles entryOutputs the
View Full Document