0% found this document useful (0 votes)
11 views5 pages

XML 1

java advanced

Uploaded by

ayush2003kanpur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

XML 1

java advanced

Uploaded by

ayush2003kanpur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

12/6/2019

XML Features and Advantages of XML


• Xml (eXtensible Markup Language) is a mark up 1) XML separates data from HTML
language. 2) XML simplifies data sharing
• XML is designed to store and transport data. 3) XML simplifies data transport
• Xml was released in late 90’s. it was created to provide
an easy to use and store self describing data.
4) XML simplifies Platform change
• XML became a W3C Recommendation on February 10, 5) XML increases data availability
1998. 6) XML can be used to create new internet
• XML is not a replacement for HTML. languages
• XML is designed to be self-descriptive.
• XML is designed to carry data, not to display data.
• XML tags are not predefined. You must define your own
tags.
• XML is platform independent and language independent.

1 2

HTML XML
HTML is used to display XML is a software and hardware XML Template
data and focuses on how data independent tool used to <root>
looks. transport and store data. It
focuses on what data is. <child>
HTML is a markup XML provides a framework to <subchild>.....</subchild>
language itself. define markup languages.
HTML is not case sensitive. XML is case sensitive.
</child>
HTML is a presentation language. XML is neither a presentation </root>
language nor a programming • XML documents must contain a root element.
language.
• This element is "the parent" of all other elements.
HTML has its own predefined You can define tags according
• The elements in an XML document form a document tree.
tags. to your need.
• The tree starts at the root and branches to the lowest level of the
In HTML, it is not necessary to XML makes it mandatory to use
use a closing tag. a closing tag.
tree.
HTML is static because it is used XML is dynamic because it is used
• All elements can have sub elements (child elements)
to display data. to transport data. • The terms parent, child, and sibling are used to describe the
HTML does not preserve XML preserve whitespaces. relationships between elements. Parent elements have children.
whitespaces. Children on the same level are called siblings (brothers or sisters).

3 4

XML Example XML Attributes


<?xml version="1.0" encoding="ISO-8859-1"?> • XML elements can have attributes. By the use
<note> of attributes we can add the information about
<to>Tove</to> the element.
<from>Jani</from> • XML attributes enhance the properties of the
<heading>Reminder</heading> elements.
<body>Don't forget me this weekend!</body>
<book publisher="Tata McGraw Hill"></book>
</note>
• The first line is the XML declaration. It defines the XML
version (1.0) and the encoding used (ISO-8859-1 = Metadata should be stored as attribute and
Latin-1/West European character set). data should be stored as element.
• The next line describes the root element of the
document (like saying: "this document is a note")
• The next 4 lines describe 4 child elements of the root
(to, from, heading, and body).

5 6

1
12/6/2019

XML XML DTD

XML Validation employee.xml


XML file can be validated by 2 ways: <?xml version="1.0"?>
1. against DTD (Document Type Definition) <!DOCTYPE employee SYSTEM "employee.dtd">
2. against XSD (XML Schema Definition)
both are used to define XML structure. <employee>
<firstname>Tom</firstname>
<lastname>brian</lastname>
<email>[email protected]</email>
</employee>

7 8

XML Schema Valid XML document


• It should be behave according to predefined
employee.xml DTD or XML schema
<?xml version="1.0"?>
• An XML document is called "well-formed" if it
<employee xmlns="http://www. abc.com"
contains the correct syntax.
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.abc.com employee.xsd">
<firstname>Tom</firstname>
<lastname>brian</lastname>
<email>[email protected]</email>
</employee>

9 10

DTD and XML schema both are used to form a well formed
Rules for well formed XML XML document.

1. It must begin with the XML declaration. XML DTD


2. It must have one unique root element. • A DTD defines the document structure with a list of legal
elements and attributes.
3. All start tags of XML documents must match
• XML schema is a XML based alternative to DTD.
end tags.
4. XML tags are case sensitive. XML schema
5. All elements must be closed. • It is defined as an XML language
6. All elements must be properly nested. • Uses namespaces to allow for reuses of existing definitions
• It supports a large number of built in data types and definition
7. All attributes values must be quoted. of derived data types
8. XML entities must be used for special
characters. We should avoid errors in XML documents because they
will stop the XML programs.

11 12

2
12/6/2019

<!DOCTYPE employee :
XML DTD It defines that the root element of the document is employee.
Valid and well-formed XML document with DTD
employee.xml <!ELEMENT employee:
<?xml version="1.0"?> It defines that the employee element contains 3 elements
<!DOCTYPE employee SYSTEM "employee.dtd"> "firstname, lastname and email".
<employee>
<firstname>Tom</firstname> <!ELEMENT firstname:
<lastname>brian</lastname> It defines that the firstname element is #PCDATA typed.
<email>[email protected]</email>
(parse-able data type).
</employee>
employee.dtd
<!ELEMENT lastname: It defines that the lastname element is
<!ELEMENT employee (firstname,lastname,email)> #PCDATA typed. (parse-able data type).
<!ELEMENT firstname (#PCDATA)>
<!ELEMENT lastname (#PCDATA)>
<!ELEMENT email (#PCDATA)> <!ELEMENT email: It defines that the email element is #PCDATA
typed. (parse-able data type).

13 14

XML DTD with entity declaration XML Schema


A doctype declaration can also define special strings that • XML schema is a language which is used for
can be used in the XML file. expressing constraint about XML documents.
An entity has three parts: There are so many schema languages which
An ampersand (&), An entity name and A semicolon (;) are used now a days for example Relax- NG
Syntax to declare entity: and XSD (XML schema definition).
<!ENTITY entity-name "entity-value">
employee.xml
• An XML schema is used to define the structure
<?xml version="1.0"? standalone=‘yes’>
<!DOCTYPE author [ of an XML document. It is like DTD but provides
<!ELEMENT author (#PCDATA)> more control on XML structure.
<!ENTITY tb “Tom Brian">
]>
<author>&tb;</author>

In the above example, tb is an entity that is used inside the author


element. In such case, it will print the value of tb entity that is “Tom
Brian".

15 16

XML Schema XML Schema


Valid and well-formed XML document with Schema Valid and well-formed XML document with Schema
employee.xml employee.xsd
<?xml version="1.0"?> <?xml version="1.0"?>
<employee xmlns="http://www. abc.com" <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.abc.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.abc.com"
xsi:schemaLocation="http://www.abc.com employee.xsd"> elementFormDefault="qualified">
<firstname>Tom</firstname>
<xs:element name="employee">
<lastname>brian</lastname> <xs:complexType>
<email>[email protected]</email> <xs:sequence>
</employee> <xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="email" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>

</xs:schema>

17 18

3
12/6/2019

XML Schema Data types


<xs:element name="employee"> : There are two types of data types in XML schema.
It defines the element name employee. • simpleType
<xs:complexType> : • complexType
It defines that the element 'employee' is complex type.
<xs:sequence> : SimpleType
It defines that the complex type is a sequence of elements. The simpleType allows you to have text-based
<xs:element name="firstname" type="xs:string"/> : elements. It contains less attributes, child elements,
It defines that the element 'firstname' is of string/text type. and cannot be left empty.
<xs:element name="lastname" type="xs:string"/> : complexType
It defines that the element 'lastname' is of string/text type. The complexType allows you to hold multiple attributes
<xs:element name="email" type="xs:string"/> : and elements. It can contain additional sub elements
It defines that the element 'email' is of string/text type. and can be left empty.

19 20

DTD XSD CDATA: (Unparsed Character data): CDATA contains the text
DTDs are derived XSDs are written in XML.
which is not parsed further in an XML document. Tags inside
from SGML syntax. the CDATA text are not treated as markup and entities will not
be expanded.
DTD doesn't support datatypes. XSD supports datatypes for
elements and attributes. Let's take an example for CDATA:
DTD doesn't support namespace. XSD supports namespace. <?xml version="1.0"?>
<!DOCTYPE employee SYSTEM "employee.dtd">
DTD doesn't define order for child XSD defines order for child
elements. elements. <employee>
<![CDATA[
DTD is not extensible. XSD is extensible. <firstname>tom</firstname>
<lastname>brian</lastname>
DTD is not simple to learn. XSD is simple to learn because you <email>[email protected]</email>
don't need to learn new language. ]]>
DTD provides less control on XML XSD provides more control on XML </employee>
structure. structure. In the above CDATA example, CDATA is used just after the element employee to
make the data/text unparsed, so it will give the value of employee:
<firstname>tom</firstname><lastname>brian</lastname><email>[email protected]</email>

21 22

PCDATA: (Parsed Character Data): XML parsers are used to parse all the text
in an XML document. PCDATA stands for Parsed Character data. PCDATA is
XML Parsers
the text that will be parsed by a parser. Tags inside the PCDATA will be treated An XML parser is a software library or package that provides
as markup and entities will be expanded. interfaces for client applications to work with an XML document.
In other words you can say that a parsed character data means the XML parser The XML Parser is designed to read the XML and create a way for
examine the data and ensure that it doesn't content entity if it contains that will programs to use XML.
be replaced. XML parser validates the document and check that the document
Let's take an example for CDATA: is well formatted.
<?xml version="1.0"?> Let's understand the working of XML parser by the figure given
<!DOCTYPE employee SYSTEM "employee.dtd"> below:
<employee> These are the two main types of XML Parsers:
<firstname>tom</firstname>
1. DOM 2. SAX
<lastname>brian</lastname>
<email>[email protected]</email>
</employee> XML Client
In the above example, the employee element contains 3 more elements 'firstname', Document XML Parser API’s Application
'lastname', and 'email', so it parses further to get the data/text of firstname,
lastname and email to give the value of employee as:
tom brian [email protected]

23 24

4
12/6/2019

XML Namespace
Example :
<book
xmlns:lit=‘http://www.literature.com/schema’>
</book>
<

25

You might also like