A.M.Senthilkumar: Changepond Technologies LTD

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 15

A.M.

Senthilkumar
(eMeter-PIPe)

Changepond Technologies Ltd

What is XML?
data. XML stands for EXtensible Markup Language XML is a markup language much like HTML XML was designed to carry data, not to display data XML tags are not predefined. You must define your own tags XML is designed to be self-descriptive XML is a W3C Recommendation XML uses a DTD (Document Type Definition) to formally describe the

An

XML Syntax All XML elements must have a example XML document closing tag <?xml version="1.0"?> XML tags are case sensitive <note> All XML elements must be <to>Tove</to> properly nested <from>Jani</from> All XML documents must have <heading>Reminder</heading> a root tag <body>Don't forget me this weekend!</body> Attribute values must always be quoted </note>

XML Validation

"Well Formed" XML documents A "Well Formed" XML document is a document that conforms to the XML syntax rules The following is a "Well Formed" XML document: <?xml version="1.0"?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>
"Valid" XML documents A "Valid" XML document is a "Well Formed" XML document which conforms to the rules of a Document Type Definition (DTD). The following is the same document as above but with an added reference to a DTD: <?xml version="1.0"?> <!DOCTYPE note SYSTEM "InternalNote.dtd"> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>

Intoduction to DTD
A Document Type Definition (DTD) defines the legal building blocks of an XML document. It defines the document structure with a list of legal elements and attributes. A DTD can be declared inline inside an XML document, or as an external reference.

Internal DTD Declaration

If the DTD is declared inside the XML file, it should be wrapped in a DOCTYPE definition with the following syntax: <!DOCTYPE root-element [element-declarations]> Example XML document with an internal DTD: <?xml version="1.0"?> <!DOCTYPE note [ <!ELEMENT note (to,from,heading,body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)> ]> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend</body> </note>

External DTD Declaration If the DTD is declared in an external file, it should be wrapped in a DOCTYPE definition with the following syntax: <!DOCTYPE root-element SYSTEM "filename"> This is the same XML document as above, but with an external DTD <?xml version="1.0"?> <!DOCTYPE note SYSTEM "note.dtd"> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> And this is the file "note.dtd" which contains the DTD: <!ELEMENT note (to,from,heading,body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)>

The Building Blocks of XML Documents


CDATA Elements Attributes Entities PCDATA

DTD Elements Declaring Elements


In a DTD, XML elements are declared with an element declaration with the following syntax: <!ELEMENT element-name category> or <!ELEMENT element-name (element-content)>

Empty Elements
Empty elements are declared with the category keyword EMPTY: <!ELEMENT element-name EMPTY> Example: <!ELEMENT br EMPTY> XML example: <br />

Elements with Parsed Character Data


Elements with only parsed character data are declared with #PCDATA inside parentheses: <!ELEMENT element-name (#PCDATA)> Example: <!ELEMENT from (#PCDATA)>

Elements with any Contents

Elements declared with the category keyword ANY, can contain any combination of parsable data: <!ELEMENT element-name ANY> Example: <!ELEMENT note ANY>

Elements with Children (sequences)

Elements with one or more children are declared with the name of the children elements inside parentheses: <!ELEMENT element-name (child1)> or <!ELEMENT element-name (child1,child2,...)> Example: <!ELEMENT note (to,from,heading,body)>

The full declaration of the "note" element is: <?xml version="1.0"?> <!ELEMENT note (to,from,heading,body)> <!DOCTYPE note SYSTEM "note.dtd"> <!ELEMENT to (#PCDATA)> <note> <!ELEMENT from (#PCDATA)> <to>Tove</to> <!ELEMENT heading (#PCDATA)> <from>Jani</from> <!ELEMENT body (#PCDATA)>

<heading>Reminder</heading> <body>Don't forget me this weekend!</body> <!ELEMENT element-name (child-name)> </note> Example:

Declaring Only One Occurrence of an Element


<!ELEMENT note (message)>

Declaring Minimum One Occurrence of an Element(+)


<!ELEMENT element-name (child-name+)> Example: <!ELEMENT note (message+)>

Declaring Zero or More Occurrences of an Element(*)


<!ELEMENT element-name (child-name*)> Example: <!ELEMENT note (message*)> <!ELEMENT element-name (child-name?)> Example: <!ELEMENT note (message?)>

Declaring Zero or One Occurrences of an Element(?)

Declaring either/or Content(|)


Example: <!ELEMENT note (to,from,header,(message|body))>

Declaring Mixed Content(|*)


Example: <!ELEMENT note (#PCDATA|to|from|header|message)*>

JAXP (Java API for XML Parsing)


DOM(Document Object Model) SAX(Simple API for XML)

DOM(Document Object Model)


DOM is a platform- and language-neutral interface, that provides a standard model of how the objects in an XML object are put together, and a standard interface for accessing and manipulating these objects and their inter-relationships. The DOM is an interface that exposes an XML document as a tree structure comprised of nodes. The DOM allows you to programmatically navigate the tree and add, change and delete any of its elements. The DOM programming interface standards are defined by the World Wide Web Consortium (W3C).

Examples (DOM Parser) Creating Blank DOM Document Creating DOM Child Elements Getting The XML Root Element To Count XML Element XML Well-Formed-ness Searching an Element in the given XML Document Create - XML File (Document) Regenerating XML file XML Error checker and locater (DOM) Getting all XML Elements Adding DOCTYPE to a XML File Getting Dom Tree Elements and their Corresponding XML Fragments Cloning a XML Element Remove Element from XML Document Getting Data from XML File (Document) Storing Data (Retrieved from a XML Document) to a File XML Validate DTD

DOM Vs. SAX


Both SAX and DOM are used to parse the XML document. Both has advantages and disadvantages and can be used in our programming depending on the situation DOM 1. Stores the entire XML document into memory before processing 2. Occupies more memory 3. We can insert or delete nodes 4. Traverse in any direction. SAX 1. Parses node by node 2. Doesn't store the XML in memory 3. We cant insert or delete a node 4. Top to bottom traversing Event driven, so reacting when it finds certain elements in the XML code (e.g. Tags, properties, ) goes from top to bottom, and if it encounters e.g. Begin tag, it fires an event. e.g. end tag, it fires and event. e.g. begin of file, it fires an event If we need to find a node and doesn't need to insert or delete we can go with SAX itself otherwise DOM, provided we have more memory.

SAX Parser Examples


XML Well-Formed Verifier Get XML Elements Get Data From the XML File XML Count Elements XML Error Checking and Locating Getting Attributes and its Value Locating the Notified Events

References

http://www.w3schools.com
http://www.roseindia.net http://www.xmlfiles.com/xml/xml_intro.asp

http://www.techinterviews.com/?p=203

Thank You...

You might also like