280 likes | 534 Views
XML. UNIT 2. Overview. XML stands for EXtensible Markup Language. XML was designed to describe data. XML is a software- and hardware-independent tool for carrying information. XML is easy to learn. Sample XML. <?xml version="1.0" encoding="UTF-8 " ?> <note> <to> Tove </to>
E N D
XML UNIT 2
Overview • XML stands for EXtensible Markup Language. • XML was designed to describe data. • XML is a software- and hardware-independent tool for carrying information. • XML is easy to learn.
Sample XML <?xml version="1.0" encoding="UTF-8"?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>
HTML vs XML • XML was designed to describe data. • HTML was designed to display data. • XML is not a replacement for HTML. • XML and HTML were designed with different goals: • XML was designed to describe data, with focus on what data is • HTML was designed to display data, with focus on how data looks • HTML is about displaying information, while XML is about carrying information.
What is XML? • XML stands for EXtensible Markup Language • XML is a markup language much like HTML • XML was designed to describe data, not to display data • XML tags are not predefined. You must define your own tags • XML is designed to be self-descriptive • XML is a W3C Recommendation
Facts on XML • XML Does Not DO Anything • With XML You Invent Your Own Tags • XML Separates Data from HTML • XML Simplifies Data Sharing • XML data is stored in plain text format. • XML Simplifies Data Transport • XML Simplifies Platform Changes • XML Makes Your Data More Available
XML Syntax Rules • All XML Elements Must Have a Closing Tag • XML Tags are Case Sensitive • XML Elements Must be Properly Nested • XML Documents Must Have a Root Element • XML Attribute Values Must be Quoted • Well Formed XML • XML documents that conform to the syntax rules above are said to be "Well Formed" XML documents.
XML Element • An XML element is everything from (including) the element's start tag to (including) the element's end tag. • An element can contain: • other elements • text • attributes • or a mix of all of the above...
XML Attributes • Attributes provide additional information about an element. • XML Attributes Must be Quoted • <person sex="female"> • <person sex='female'> • XML Elements vs. Attributes
XML Encoding • XML documents can contain international characters, like Norwegian æøå, or French êèé. • To avoid errors, you should specify the encoding used, or save your XML files as UTF-8. • UTF-8 - The Web Standard • The first line in an XML document is called the prolog: • <?xml version="1.0"?>
Displaying XML with CSS • CSS can be used to format an XML document. • Formatting XML with CSS is not the most common method. • W3C recommends using XSLT instead.
Valid XML • An XML document with correct syntax is called "Well Formed". • A "Valid" XML document must also conform to a specified document type. • XML DTD and XML Schema are used validate XML documents.
XML DTD • DTD is to define the structure of an XML document. • It defines the structure with a list of legal elements.
Sample DTD <!DOCTYPE note[<!ELEMENT note (to,from,heading,body)><!ELEMENT to (#PCDATA)><!ELEMENT from (#PCDATA)><!ELEMENT heading (#PCDATA)><!ELEMENT body (#PCDATA)>]>
XML Schema • XML Schema is an XML-based alternative to DTD. • <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>
XML Parser • All modern browsers have a built-in XML parser. • It is a software library (or a package) that provides methods (or interfaces) for client applications to work with XML documents • It checks if XML is well formed • It may validate the document.
DOM Parser • A DOM parser creates an internal structure in memory which is a DOM document object • Client applications get the information of the original XML document by invoking methods on this Document object or on other objects it contains • DOM parser is tree-based (or DOM obj-based) • Client application seems to be pulling the data actively, from the data flow point of view
SAX Parser • It does not first create any internal structure • Client does not specify what methods to call • Client just overrides the methods of the API and place his own code inside there • When the parser encounters start-tag, end-tag, etc., it thinks of them as events
XML DOM • A DOM (Document Object Model) defines a standard way for accessing and manipulating documents. • The XML DOM views an XML document as a tree-structure.
What is the XML DOM? • The XML DOM is: • A standard object model for XML • A standard programming interface for XML • Platform- and language-independent • A W3C standard • The XML DOM defines the objects and properties of all XML elements, and the methods (interface) to access them. • In other words: The XML DOM is a standard for how to get, change, add, or delete XML elements.
XML DOM Nodes • In the DOM, everything in an XML document is a node. • The entire document is a document node • Every XML element is an element node • The text in the XML elements are text nodes • Every attribute is an attribute node • Comments are comment nodes
XML Parser • The XML DOM Parser contains methods to traverse XML trees, access, insert, and delete nodes. • The XML document must be loaded into an XML DOM object first. • An XML parser reads XML, and converts it into an XML DOM object that can be accessed with program. • Most browsers have a built-in XML parser.