XMLvf25
XMLvf25
Agenda
At the end of this presentation, you will be able to understand :
Understand why we need XML
Understand the features of XML
Create and use XML files
Understand and use DTDs
Understand XSD and XPath
Use XML parsers like SAX and DOM parser
Create and use XSL files
John works on a project
John Suggests the ways!
John Collects Data..
The organization has many departments like HR, Admin, R&D, Marketing, Sales, Technical
Writing etc., Mark gets data from all departments in a different format. Mark is unable to
read the files from the program as the format from each department is different.
Data in XML format
Mark has a doubt..
John explains XML
John explains XML
Extensibility - XML data can be extended with DTD and XSL. DTD is Document Type
definition which provides the format of the XML document and XSL is the way XML data will
be represented/displayed. It is like CSS for HTML.
Openness - Any tool/language can open an xml file and parse it in the programming
language.
XML
Data is inserted in the XML file in tags like HTML.
Ordering and nesting of XML document should be proper. XML file does not
work properly if there is clash in nesting.
XML tags are case sensitive. Opening and closing tags should be exactly the
same without any difference in the case.
Every opening tag must have a close tag else XML file will not work correctly.
XML Namespace
XML NameSpace
XML Name Space
Syntax of Name space is:
DTD Format:
<!DOCTYPE Name [
Document Type Definition
]>
Declaring Elements in DTD
<!ELEMENT Name ContentSpecification>
EMPTY content- element has no child elements
ANY content – element has no content constraints
Element content – can contain specified elements
Mixed Content – can contain elements and character data
Eg:
<!ELEMENT MISCELLANEOUS ANY>
<!ELEMENT IMG EMPTY>
<!ELEMENT NAME (#PCDATA)>
<!ELEMENT CD (TITLE, ARTIST)>
<!ELEMENT DWELLING (HOUSE |ROOM | CONDOMINIUM| TRAILER)>
<!ELEMENT TITLE (#PCDATA | SUBTITLE )>
Default Attributes:
#REQUIRED: attribute must be present
#IMPLIED: attribute is optional
Default value: value to use for default
#FIXED: fixed at certain value
Eg:
<ATTLIST PACKAGE ontime (yes| no) #REQUIRED)
<ATTLIST PACKAGE ontime (yes| no) #IMPLIED)
<ATTLIST PACKAGE ontime CDATA #FIXED “yes”)
DTD
1 <?xml version=“1.0” encoding=“UTF-8”?>
2 <!DOCTYPE TEST SYSTEM “Test.dtd”>
3 <TEST>
4 <Name> Berhanu</Name>
5 <From>Addis</From>
6 <Profession> Teaching</Profession>
7 </TEST>
8
9 Content of the file: Test.dtd
10 <!DOCTYPE TEST>
11 [
12 <!ELEMENT TEST (Name,From,Profession)>
13 <!ELEMENT Name (#PCDATA)>
14 <!ELEMENT From (#PCDATA)>
15 <!ELEMENT Profession (#PCDATA)>
16 ]>
DTD
DOCTYPE says the root element of the XML file.
ELEMENT Test says it has 3 elements which are Name, from and Profession.
All the ELEMENTS are PCDATA, that means all the elements are parsable
character data.
At times you need not parse certain elements then you can use CDATA.
Using XML parser, using validate() we can validate XML whether it is formed
according to DTD or not.
Why DTD?
Even the XML file can be written in different formats by different people.
To avoid this issue and to standardize the XML data in one format, DTD is
helpful and useful.
Features of DTD
DTD can be given in the xml file or it can be given in a separate file having
extension of .dtd and can use it in the XML file.
During the validation of XML, parsers read the DTD file and validates against
the XML file. Finally validator will give the result as valid XML or Invalid XML.
XPath
<xsd:schema xmlns:xsd="http://www.w3.0rg/2001/XMLSchema”>
<xsd:element name="ElemName”>
</xsd:element>
</xsd:schema>
Declaring Elements in Schema
Elements can have min and max times they can occur
Eg of XML:
< InvitedGuests> 0 to 50</ InvitedGuests>
Declaring Elements in Schema
Can restrict content to a range of choices
<xsd:element name="dwelling”>
<xsd:simpleType>
<xsd:restriction base="xsd:string”>
<xsd:enumeration value="house”>
<xsd:enumeration value="apartment”>
<xsd:enumeration value="trailer”>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
Eg of XML:
< dwelling > house/ apartment / trailer </ dwelling >
Declaring Elements in Schema
<xsd:element name="EmptyElem”>
<xsd:complexType>
</xsd:complexType>
</xsd:element>
Eg of XML:
< EmptyElem ></ EmptyElem>
Declaring Complex Types
Declaring elements with mixed content
<xsd:element name="title”>
<xsd:complexType mixed="true”>
<xsd:sequence>
<xsd:element name="subtitle”>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
Eg of XML:
< title>
< subtitle> </ subtitle>
</ title >
Declaring Complex Types
Declaring elements with element content
<xsd:element name="address”>
<xsd:complexType>
<xsd:sequence>
<xsd:element name="num” type="xsd:integer”/>
<xsd:element name="street” type="xsd:string”/>
<xsd:element name="city” type="xsd:string”/>
<xsd:element name="state” type="xsd:string”/>
</xsd: sequence>
</xsd:complexType>
</xsd:element>
Eg of XML:
< address >
< num >123 </num>
< street > abc</ street >
< city >Adama </ city >
< state > Oromia</ state >
</ address >
Declaring Complex Types
Declaring elements with element content
<xsd:element name="vehicle”>
<xsd:complexType>
<xsd:choice>
<xsd:element name="car” type="xsd:string”/>
<xsd:element name="bus” type="xsd:string”/>
<xsd:element name="van” type="xsd:string”/>
<xsd:element name="truck” type="xsd:string”/>
</xsd:choice>
</xsd:complexType>
</xsd:element>
Eg of XML:
< vehicle >
< car >Yaris </ car > or
< bus > tata</ bus > or
< van >hilex </ van > or
< truck > mahindra</ truck >
</ vehicle >
Declaring Complex Types
Declaring elements with element content
<xsd:element name="CapitalCity”>
<xsd:complexType>
<xsd:all>
<xsd:element name="name” type="xsd:string”/>
<xsd:element name="state” type="xsd:string”/>
<xsd:element name="pop” type="xsd:positiveInteger”/>
</xsd:all>
</xsd:complexType>
</xsd:element>
Eg of XML: any order
< CapitalCity >
< name >berhanu </ name >
< state > oromia</ state >
< pop >12333 </ pop >
</ CapitalCity >
Declaring Attributes in Schema
Declared using <xsd:attribute> tag
<xsd:element name="Name”>
<xsd:complexType mixed="true”>
<xsd:attribute name="Age” type="xsd:integer”>
</xsd:complexType>
</xsd:element>
Eg of XML:
< Name Age =“35”> </ Name >
Declaring Attributes in Schema
Adding attribute to element that has empty content
<xsd:element name="Image”>
<xsd:complexType>
<xsd:attribute name="src” type="xsd:string”>
</xsd:complexType>
</xsd:element>
Declaring Attributes in Schema
Adding attributes to element that has mixed content
<xsd:element name="car”>
<xsd:complexType>
<xsd:sequence>
<xsd:element name="make” type="xsd:string”>
<xsd:element name="model” type="xsd:string”>
</xsd:sequence>
<xsd:attribute name="year” type="xsd:gYear”>
</xsd: complexType>
</xsd:element>
XSD for Business card XML
<?xml version="1.0" encoding="utf-8" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="BusinessCard"> Businesscard XML:
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Name" type="xsd:string"/> <?xml version="1.0" encoding="utf-8" ?>
<BusinessCard>
<xsd:element name="phone" maxOccurs="unbounded">
<xsd:complexType mixed="true"> <Name>Abebe</Name>
<xsd:attribute name="type" use="required"> <phone type="mobile">(251) 9621-03206</phone>
<xsd:simpleType>
<xsd:restriction base="xsd:string"> <phone type="work">(251) 9621-03207</phone>
<xsd:enumeration value="mobile"/> <phone type=”home">(251) 9621-4444</phone>
<xsd:enumeration value=”work"/>
<xsd:enumeration value=”home"/> <phone type="fax">(251) 9621-1234</phone>
<xsd:enumeration value=”fax"/> <email>[email protected]</email>
</xsd:restriction>
</xsd:simpleType> </BusinessCard>
</xsd:attribute>
</xsd:complexType>
</xsd:element>
To avoid this, Java/programming language has given parsers like DOM and
SAX to read the xml file.
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
DOM to Create XML file
DOM to Create XML file
Finally, transformer Transform() method is used to convert the document object to XML file.
Parsing XML file using DOM-Required Imports
Parsing XML file using DOM
SAX Parser – Required Imports
SAX Parser
In the main () method, XML file is parsed by SAXParser object and is attached with a
mySaxHandler class.
For every node or data read, it calls a corresponding method in mysaxHandler class.
StartDocument() and endDocument() methods are called when an XML document is started
to read and after reading the entire document.
When the data in-between the tag is read, characters() method is called.
John works on a project
John works on a project
Why XSL?
Like the way CSS is stylesheet for HTML, XSL is the stylesheet for
XML.
XSL can navigate all the nodes/elements of the XML file and can
display the XML data in the particular format. This is done by XSLT
which stands for Extensible stylesheet Language Transformations.
Features of XSL
XSL file reads the XML data, looks for the query (If it is given in the
XSL file) and displays the data on browser in the format given in the
XSL file.
XSL File
Note: Browser should support XSLT, other wise the above code will not work.
This line should be used in the XML file to use the XSL file.
This line will use the XML data and use XSL format and displays XHTML in the
browser.
Sample XML File