Mastering XML: The Essential Guide to Understanding and Using XML Language
E N D
Presentation Transcript
XML: The new standard -Eric Taylor
Overview • What is XML? • Why use XML? • Details of the language • Incorporating XML • Business Application • Small Assignment
What is XML? • eXtensible Markup Language • Markup language like HTML • Designed to carry data, not display it • W3C recommended • World Wide Web Consortium
What is XML? • Plain text. • What does it do? • Not an independent language. • The Design: • Compatibility • Straightforward • Fast • Wide support
Why use XML? • Simplifies HTML changes based on data changes. • Standardized data sharing. • Too many possibilities without it. • Incompatible applications not affected. • Hardware/Software independent • Minor exceptions
Language Details • Tag based. • Unlike HTML, all tags are user-defined. • <random_tag_name> data_to_be_stored </random_tag_name> • Data stored between tags, or within tags as attributes. • Attributes are normally used for information that describes the tag itself, not necessarily the information you want to store. • Ex: <book internal_id=“11421” />
Language Details • Tree structure • <library> • <book internal_id=“11421”> • <name> A Game of Thrones </name> • <isbn> 123456789 </isbn> • … • </book> • <book internal_id=“11422”> • <name> A Clash of Kings </name> • <isbn> 123456788 </isbn> • … • </book> • </library>
Language Details Example 2: From w3schools.com
Language Details • Syntax Details: • Case sensitive tags. • Defined values • Quoted values • Nesting: bold, italic, etc… • Naming rules
Language Details • Namespaces: • Think of <include> statements. • Allows reference to different types of data with similar names. • Allows reference to custom data-types. • Integer, decimal, string fields, etc, with restrictions and/or patterns.
Language Details <root> <h:table xmlns:h="http://www.w3.org/TR/html4/"> <h:tr> <h:td>Apples</h:td> <h:td>Bananas</h:td> </h:tr> </h:table> <f:table xmlns:f="http://www.w3schools.com/furniture"> <f:name>African Coffee Table</f:name> <f:width>80</f:width> <f:length>120</f:length> </f:table> </root>
Language Details • Validation: • How do we know an XML document is correct? • How do we know we are getting the information we want? • “Well formed” • Adheres to syntax rules • Easily done with XML software and processing code. • Validity: • Answers the “are we getting what we want?” question. • Follows the given XML schema (known as an XSD).
Language Details • What is a schema (XSD)? • XML file with the extension .xsd • Dictates the allowed structure of a corresponding XML file. • Defines possible fields, field values, etc. • Used in process of ‘Schema Validations’. • Pass or fail check if the corresponding XML file contains the appropriate information. • Structure and data types, NOT content (elements vs. their value).
Language Details XML File: <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> XSD File: <xs:element name="note"> <xs:complexType> <xs:sequence> <xs:element name="to" type="xs:string"/> <xs:element name="from" type="xs:string"/> <xs:element name="heading" type="xs:string"/> <xs:element name="body" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element>
Incorporating XML • Parsing XML to use it: • DOM (Document Object Model). • Standard way for accessing and manipulating XML documents. • xmlDoc - the XML document created by the parser. • getElementsByTagName(“tag_name")[0] - the first <tag_name> element • childNodes[0] - the first child of the <tag_name> element (the text node) • nodeValue - the value of the node (the text itself) • some_variable=xmlDoc.getElementsByTagName(“book”)[0].childNodes[0].nodeValue;
Incorporating XML • Parsing commonly done via JavaScript in web applications. • Java can import libraries to use the standard DOM for parsing.
Incorporating XML From w3schools.com
Incorporating XML • XSLT used to display and format XML. • Basically “transforming XML to HTML before it is displayed by the browser”.
XML in Practice • Allows for extension to legacy systems. • Create a new ‘front end’ process to make the data match to legacy. • Things to consider: • Use similar XML software to eliminate small differences. • Establish a common schema • Schema validations on both ends of transmission. • Business logic to process the data.
Small Assignment: • Download the 30 day free trial of XML Spy. (very quick installation) • Go through the ‘getting started’ tutorial. • Create a sample schema based on some of the previous examples. • Create an XML document based on the schema, and validate. • Load the XML file into an HTML document and display the values. • Use Tomcat, or any IDE with a preview function.
References http://www.tizag.com/xmlTutorial/ www.w3schools.com Amiano, Mitch, Conrad D'cruz, Kay Ethier, and Michael D. Thomas. XML Problem-Design-Solution. Indianapolis: Wiley Inc, 2006.