Professional Documents
Culture Documents
PRASAD B
Assoc. Prof.
Dept. of Computer and Engineering
Email: bprasad@mlritm.ac.in
UNIT - 2
Objective
Email: bprasad@mlritm.ac.in
UNIT - 2
Scope
Email: bprasad@mlritm.ac.in
UNIT – 2
Course Outcomes
Email: bprasad@mlritm.ac.in
UNIT - 2
XML
( Extensible Markup Language)
What You Should Already Know
looks
XML can work behind the scene to simplify the creation of HTML documents for
XML can be used to exchange the information between organizations and systems.
XML can be used to store and arrange the data, which can customize your data
handling needs.
XML can easily be merged with style sheets to create almost any desired output.
package.
XML declaration
other elements.
05/02/23 PRASAD B, Assoc. prof., 19
MLRITM,JNTU-H
XML- Syntax
It is written as below:
<?xml version="1.0" encoding="UTF-8"?>
Where version is the XML version and encoding specifies the
number attribute.
XML-nodes or XML-tags.
as shown below:.
<element>
instructions.
Start Tag
End Tag
Empty Tag
<hr></hr>
<hr />
05/02/23 PRASAD B, Assoc. prof., 32
MLRITM,JNTU-H
Syntax Rules for Tags and Elements
Element Syntax
Nesting of elements
Root element
Case sensitivity
<element>....</element>
<element/>
children elements must not overlap. i.e., an end tag of an element must
have the same name as that of the most recent unmatched start tag.
Following example shows
<root>
<child>
<subchild>.....</subchild>
</child>
</root>
That means the name of the start and the end elements need to
For example
<?xml version="1.0"?>
<contact-info>
<contact-info>
name/value pair.
For example:
<a href="http://www.tutorialspoint.com/">Tutorialspoint!</a>
Attribute names are defined without quotation marks, whereas attribute values
String Type
TokenizedT ype
Enumerated Type
CDATA is a StringType.
of the attribute.
The validity constraints noted in the grammar are applied after the
Entity References
Character References
An entity reference contains a name between the start and the
end delimiters.
and/or markup.
sensitive, which means the name of start and end elements need
and terms.
They are visible only in the source code; not in the XML code.
<!-------Your comment----->
language.
Internal DTD
External DTD
• System identifiers
• Public identifiers
05/02/23 PRASAD B, Assoc. prof., 64
MLRITM,JNTU-H
Internal DTD
source.
// Start Declaration
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
// DTD
<!DOCTYPE address [
// DTD Body
<!ELEMENT address (name,company,phone)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT company (#PCDATA)>
<!ELEMENT phone (#PCDATA)>
// End Declaration
]>
// XML document
<address>
<name>Tanmay Patil</name>
<company>TutorialsPoint</company>
<phone>(011) 123-4567</phone>
</address>
The DOCTYPE informs the parser that a DTD is associated with this
XML document.
05/02/23 PRASAD B, Assoc. prof., 68
MLRITM,JNTU-H
DTD Body- The DOCTYPE declaration is followed by body of the
source.
05/02/23 PRASAD B, Assoc. prof., 71
MLRITM,JNTU-H
External DTD - Syntax
<!DOCTYPE root-element SYSTEM "file-name">
standalone="no" ?>
System identifiers or
Public identifiers.
Syntax is as follows:
XML data.
database.
05/02/23 PRASAD B, Assoc. prof., 78
MLRITM,JNTU-H
XML Schemas are More Powerful than DTD
own format.
data types:
Another great strength about XML Schemas is that they are written
in XML:
You can use your XML editor to edit your Schema files
You can use your XML parser to parse your Schema files
Create your own data types derived from the standard types
A well-formed XML document is a document that conforms to the XML syntax rules, like:
it must begin with the XML declaration
it must have one unique root element
start-tags must have matching end-tags
elements are case sensitive
all elements must be closed
all elements must be properly nested
all attribute values must be quoted
entities must be used for special characters
Even if documents are well-formed they can still contain errors, and those
errors can have serious consequences.
05/02/23 PRASAD B, Assoc. prof., 85
MLRITM,JNTU-H
XML – Schemas - Syntax
<?xml version="1.0"?>
<xs:schema>
...
...
</xs:schema>
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
...
...
</xs:schema>
indicates that the elements and data types used in the schema
come from the "http://www.w3.org/2001/XMLSchema" namespace.
It also specifies that the elements and data types that come from
the "http://www.w3.org/2001/XMLSchema" namespace should be
prefixed with xs:
targetNamespace=http://www.w3schools.com
indicates that the elements defined by this schema (note, to, from,
heading, body.) come from the "http://www.w3schools.com" namespace.
elementFormDefault="qualified">
indicates that any elements used by the XML instance document which were
declared in this schema must be namespace qualified.
XML document.
XML document.
Simple Type
Complex Type
Global Types
xs:string, xs:date.
For example:
documents.
<xs:element name="Address">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string" />
<xs:element name="company" type="xs:string" />
<xs:element name="phone" type="xs:int" />
</xs:sequence>
</xs:complexType>
</xs:element>
05/02/23 PRASAD B, Assoc. prof., 96
MLRITM,JNTU-H
Global Types
<xs:element name="Address1">
<xs:complexType>
<xs:sequence>
<xs:element name="address" type="AddressType" />
<xs:element name="phone1" type="xs:int" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Address2">
<xs:complexType>
<xs:sequence>
<xs:element name="address" type="AddressType" />
<xs:element name="phone2" type="xs:int" />
</xs:sequence>
</xs:complexType>
</xs:element>
in memory.
an XML document
that are somehow associated with other nodes in the tree but
document object
contains
Client application seems to be pulling the data actively, from the data
Disadvantage:
It is memory inefficient
Following are the steps used while parsing a document using DOM
Parser.
1. Import XML-related packages.
2. Read name of XML document using command prompt.
3. Invoke the parser
4. Call the method
<?xml version="1.0"?>
<student> <name>SHARAN</name>
<?xml version="1.0"?>
<student>
<name>SHARAN</name>
</student>
Client just overrides the methods of the API and place his
of them as events
Following are the steps used while parsing a document using DOM
Parser.
1. Import XML-related packages.
2. Read name of XML document using command prompt.
3. Invoke the XML reader parser
4. Call the method-parser
4
catch (Exception e) {
System.out.println(file_name + " is not well-formed."); System.exit(1);
} }
else
{
System.out.println("File is not present: " + file_name);
}}
catch (IOException ex)
{
ex.printStackTrace();
}}}
05/02/23 PRASAD B, Assoc. prof., 129
MLRITM,JNTU-H
File name : sax.xml
<?xml version="1.0"?>
<student> <name>SHARAN</name>
<?xml version="1.0"?>
<student>
<name>SHARAN</name>
</student>