Professional Documents
Culture Documents
XML
XML
1
XML - Introduction
What is XML?
Difference between XML and HTML?
How XML Can be Used?
XML Syntax.
XML Elements and Attributes.
XML WellFormedNess and Validity.
What is XSL.?
DOM and SAX
XML CDATA & Entity References
2
What is XML ?
XML tags are not predefined in XML. You must define your own
tags .
3
HTML
<html>
<body>
<b>Hello World</b><br>
</body>
</html>
4
XML = EXtensible Markup Language
5
DTD = DOCUMENT TYPE DEFINITION
6
XSL= EXtensible Stylesheet Language
7
Difference between XML and HTML
XML and HTML were designed with different goals.XML is not a replacement for HTML.
XML was designed to describe data and to focus on what data is.
HTML was designed to display data and to focus on how data looks.
8
How to Make Data Portable
• Tell what the data means
• Tell how the data is structured SO COMPUTERS CAN
UNDERSTAND IT
• Tell how it should look
10
XML at a glance
Well Formed Document:
<Book>
<Author>George Soros</Author>
<Title>The Crisis of Global Capitalism</Title>
<Year>1998</Year>
<Publ>Public Affairs</Publ>
<Price>26.00</Price>
<ISBN>1-891620-27-4<ISBN>
</Book>
11
Xml Document
<?xml version="1.0"?>
<!-- Reference to a DTD to validate our customer data -->
<!DOCTYPE customer SYSTEM "http://www.example.com/customer.dtd">
<customer id="123456">
<first-name>John</first-name>
<last-name>Doe</last-name>
<address>
<street>123 Main</street>
<city>Anytown</city>
<state>CA</state>
<zip>99999</zip>
</address>
<phone>800-555-9999</phone>
<email-address>john@doe.com</email-address>
</customer>
12
XML Recipe Example
<?xml version="1.0"?>
<Recipe>
<Name>Apple Pie</Name>
<Ingredients>
<Ingredient>
<Qty unit=pint>1</Qty>
<Item>milk</Item>
</Ingredient>
<Ingredient>
<Qty unit=each>10</Qty>
<Item>apples</Item>
</Ingredient>
</Ingredients>
<Instructions>
<Step>Peel the apples</Step>
<Step>Pour the milk into a 10-inch saucepan</Step>
<!-- And so on... -->
</Instructions>
</Recipe>
XML Example
<?XML version="1.0" ?>
<PRESCRIPTION>
<MEDNAME MED="Amoxil"> Prescribed medication:
Amoxil</MEDNAME>
<FORM TYPE="capsule"> Form: capsule </FORM>
<DOSAGE AMOUNT="25 mg"> Dosage: 25 mg</DOSAGE>
<RXINSTRUCTIONS FREQUENCY="daily"> daily
</RXINSTRUCTIONS>
</PRESCRIPTION>
15
DTD
<?xml version="1.0">
<!DOCTYPE computer [
<!ELEMENT computer
Company_Name, CPU, RAM,
HDD)>
<!ELEMENT Company_Name
#PCDATA>
]>
16
DOM Tree
17
XML Syntax
The syntax rules of XML are very simple and very strict. The rules are very easy to learn, and
very easy to use.
Because of this, creating software that can read and manipulate XML is very easy to do.
Comments in XML
The syntax for writing comments in XML is similar to that of HTML.
<!-- This is a comment -->
18
XML Elements and Attributes
XML Elements
XML Elements are Extensible
XML documents are Extensible.
XML Elements have Relationships
Elements are related as parents and children.
Elements have Content
Elements can have different content types.
XML Attributes
XML elements can have attributes.
<person sex="female">
Use of Elements vs. Attributes
Data can be stored in child elements or in attributes.
Take a look at these examples:
<person sex="female"> <firstname>Anna</firstname> <lastname>Smith</lastname> </person>
<person> <sex>female</sex> <firstname>Anna</firstname> <lastname>Smith</lastname> </person>
There are no rules about when to use attributes, and when to use child elements. My experience is
that attributes are handy in HTML, but in XML you should try to avoid them. Use child elements if the
information feels like data.
19
WellFormedNess & Validity
"Well Formed" XML documents
20
XSL(the eXtensible Stylesheet Language)
SAX and DOM are two different APIs for accessing information in XML documents.
Using DOM Parsers ,XML documents are represented as a hierarchical tree structure.
SAX-based parsers invoke methods when markup (e.g. a start tag, an end tag, etc.) is encountered. No tree
structure is created - data is passed to the application from the XML document as it is found.
23
24
DOM
DocumentBuilderFactory documentBuilderFactory;
DocumentBuilder documentBuilder;
docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilder = docBuilderFactory.newDocumentBuilder();
DocName=docBuilder.parse(input);
root=(Node)DocName.getDocumentElement(); // Root Node
25
SAX
26
All text in an XML document will be parsed by the parser.
Escape Characters
To avoid this, you have to replace the "<" character with an entity reference, like this:
' ‘ apostrophe
" “ quotation
mark
27
CDATA
Everything inside a CDATA section is ignored by the parser.
If your text contains a lot of "<" or "&" characters - like program code often does - the XML
element can be defined as a CDATA section.
A CDATA section starts with "<![CDATA[" and ends with "]]>":
<![CDATA[
function matchwo(a,b) {
if (a < b && a < 0) then { return 1 } else { return 0 }
}
]]>
28
Any Quiestions…..?
29
DTD’S and Schema’s
30
Introduction to DTD
The purpose of a Document Type Definition is to define the legal building blocks of an XML document.
Seen from a DTD point of view, all XML documents are made up by the following simple building
blocks:
•Elements
•Tags
•Attributes
•Entities
•PCDATA
•CDATA
31
DTD Elements
Empty elements
<!ELEMENT element-name EMPTY>
Elements with only character data
Elements with only character data are declared with #PCDATA inside parentheses:
<!ELEMENT element-name (#PCDATA)>
32
Declaring only one occurrence of the same element
33
In a DTD, Attributes are declared with an ATTLIST declaration.
Declaring Attributes
An attribute declaration has the following syntax:
example:
DTD example: <!ATTLIST payment type CDATA "check">
XML example: <payment type="check" />
Attribute Type
CDATA
(en1|en2|..)
ID
IDREF
NMTOKEN
NMTOKENS
IDREF
IDREFS
34
The default-value can have the following values:
Value
Value
#REQUIRED
#IMPLIED
#FIXED value
35
36
37
38
39
40
XML Schemas
XML ("Extensible Markup Language") is rapidly establishing itself as a useful
tool for data exchange because it has the incredible potential to become a
universal format for structuring information.
To use XML effectively in a community such as the Internet,
there must be some constraints on the valid tags and tag sequences so that the
data exchange can actually make sense to someone other than the creator.
Still commonly used, DTDs (Document Type Definition) fulfilled this need.