Sie sind auf Seite 1von 12

Introduction to XML

(Extensible Markup Language)


XML ?
Extensible: It lets you define your own tags.
Markup: To do anything we use tags.
Language: similar to HTML but you create your own
tags. HTML was designed to display data with focus
on how data looks. Whereas XML was designed to
carry data with focus on what data is.
XML:
XML is used to store data in plain text format
irrespective of any platform.
XML doesn’t have any predefined tags.
XML provides the facility to design your own tags to
structure your data.
XML as a document doesn’t do anything. It is the
software which uses this document to connect with
database and processes it.
XML can be used to design new markup languages.
XML Documents form a tree like structure that has root and various
branches.
<?xml version=“1.0” ?>
<Name>
<Fname>Ram</Fname>
<Mname>Kumar</Mname>
<Lname>Singh</Lname>
</Name>

The First line is xml declaration which defines the xml version. The next
line is the root element of the document. All xml documents must have the
root element to mark the beginning and end of the document. The next
three lines are the child elements.
Example for Elements and Attributes:
<?xml version="1.0"?>
<Student>
<Stream type="BCA">
<Duration>3 Years</Duration>
<Fee>1,80,000/-</Fee>
</Stream>
<Stream type="MCA">
<Duration>3 Years</Duration>
<Fee>1,86,000/-</Fee>
</Stream>
</Student>

<Student> is the Root Element. <Stream> is the element with attribute


“type” which has values like BCA and MCA.
Rules for XML:
XML tags are case sensitive.
All XML elements should have closing tags.
XML elements can be nested but it should be done properly.
All attributes value must be in quotes.

XML tag names can’t start with xml, XML or Xml etc. Names can
contain letters, numbers and characters but can’t start with a
number. Names can’t have spaces.
Validating XML
Document Type Definition (DTD)
XML Schema

DTD
A DTD defines the structure and the legal elements and
attributes of an XML document so that independent groups of
people can agree on a standard DTD for interchanging data. An
application can use a DTD to verify that XML data is valid.
Internal DTD Declaration
<?xml version="1.0"?>
<!DOCTYPE Student [
<!ELEMENT Student (Stream, Duration, Fee)>
<!ELEMENT Stream (#PCDATA)>
<!ELEMENT Duration (#PCDATA)>
<!ELEMENT Fee (#PCDATA)>
<!ATTLIST Stream type CDATA "">
]>
<Student>
<Stream type="MCA">Master of Computer Application</Stream>
<Duration>3 Years</Duration>
<Fee>1,80,000/-</Fee>
</Student>
External DTD Declaration
<?xml version="1.0"?>
<!DOCTYPE Student SYSTEM “xyz.dtd">
<Student>
<Stream type=“BCA”>Bachelor of Computer Application</Stream>
<Duration>3 Years</Duration>
<Fee>1,80,000/-</Fee>
</Student> xyz.dtd
<!ELEMENT Student (Stream, Duration, Fee)>
<!ELEMENT Stream (#PCDATA)>
<!ELEMENT Duration (#PCDATA)>
<!ELEMENT Fee (#PCDATA)>
<!ATTLIST Stream type CDATA “”>
The DTD above is interpreted like this:

!DOCTYPE Student defines that the root element of this document is note
!ELEMENT Student defines that the Student element must contain four
elements: “Stream, Duration, Fee"
!ELEMENT Stream defines the Stream element to be of type "#PCDATA"
!ELEMENT Duration defines the Duration element to be of type
"#PCDATA"
!ELEMENT Fee defines the Fee element to be of type "#PCDATA“
!ATTLIST Stream Type defines the attribute Type to be of type CDATA.
PCDATA
PCDATA means parsed character data that will be parsed by a
parser. The text will be examined by the parser for entities and
markup. Tags inside the text will be treated as markup and
entities will be expanded. However, parsed character data should
not contain any &, <, or > characters; these need to be
represented by the &amp; &lt; and &gt; entities, respectively.
CDATA
CDATA means character data. CDATA is text that will
not be parsed by a parser. Tags inside the text will not
be treated as markup and entities will not be expanded.

Das könnte Ihnen auch gefallen