xml - saiprasadcomp.files.wordpress.com · sees lots of resumés, usually in the form of documents...

14
XML XML stands for EXtensible Markup Language. XML is a markup language much like HTML XML is a software- and hardware-independent tool for carrying information. XML is easy to learn. XML was designed to describe data, not to display data XML tags are not predefined. You must define your own tags XML is designed to be self-descriptive Use of xml So, why should you use XML? Because today's businesses thrive on data, and that data can come from any number of sources, and in any number of formats: databases, Web pages, spreadsheet files, and e-mail, to name just a few. XML enables you to work with more data from more sources, and to get more from that data.

Upload: phamdien

Post on 10-May-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

XML

XML stands for EXtensible Markup Language.XML is a markup language much like HTMLXML is a software- and hardware-independent tool for carrying information.XML is easy to learn.XML was designed to describe data, not to display dataXML tags are not predefined. You must define your own tagsXML is designed to be self-descriptive

Use of xml

So, why should you use XML? Because today's businesses thrive on data, and that data can come from any number of sources, and in any number of formats: databases, Web pages, spreadsheet files, and e-mail, to name just a few. XML enables you to work with more data from more sources, and to get more from that data.

Suppose you run the human resources department of a medium-sized company. Your staff sees lots of resumés, usually in the form of documents or e-mail messages.

Wouldn't it be great if you could automatically copy the names, addresses, and lists of job skills from those resumés? And even better, if you could use your computers to match those skills with open positions? And best of all, if the same process prepared a report on your department's performance for your manager?

You can do those things (and more) with XML. You can extract data from various original sources, store that data in one place so you know just where to find it, and use the data again whenever and wherever you need it.

For example, do you need to report budget data for the past three years? However you store that data, if it's in XML, you can select what you need and import it into a document, worksheet, or database — whatever the situation requires.

Another advantage of XML is its ability to automate any number of business processes. Say that a customer sends you a purchase order. It arrives looking like any document, but the information in this document is in XML. Your computer system can automatically transform that purchase order into a worksheet for your shipping department to use, and automatically import the order data into your accounting database. Depending on how your system is designed, all you do is push a couple of buttons.

The components of a basic XML system.

A typical XML system consists of three types of files:

XML data is your data, plus XML tags that describe the meaning and structure of your data.

XML schemas define rules for what can and cannot reside in your data files. For example, a schemacould ensure that users can't enter words into a date field.

XML transforms enable the use of data in a variety of programs or files. For example, one transformcould add sales data to a workbook, while another transform could insert the same data into a document.

Data: the key component in any XML system.

The key component in an XML system is the data. XML data files contain your data and a set of codes called tags that describe what the data means. You can create any tags you need for your data.That's one reason that XML is so adaptable and so useful that they named it extensible.

Sample XML data.

Suppose you run a veterinary clinic and you want to use XML to store data for your various animal clients. Your XML data files will contain the data for each animal. Each piece of that data is surrounded by a tag, and each tag describes what the piece of data means. The combination of tag and data is called a node.

The illustration shows a sample XML data file for a cat named Izzy. The tags are the combinations of angle brackets and text:<CAT>, <NAME>, <AGE>

and so on.

Tags actually consist of two parts, an opening tag and a closing tag like so:<BREED> … </BREED>

The forward slash (/) is what makes a tag a closing tag. The opening and closing tags surround any data, like so:<BREED>Siamese</BREED>

In XML, the tags are designed to clearly describe every piece of data. If someone asks what all those tags mean, you can say that they mean whatever you need them to mean. That's a part of what

makes XML "extensible." In this case, you know what "yes," "no," and "Izzl138bod" all mean.

Because the tags describe the structure and meaning of the data, then any computer program or system that supports XML can understand that data and put it to use. For instance, you could load the cat's name and the owner's name from the data into a vaccination report and a payment request simultaneously.

Those are just a couple of examples of how you can put XML to work. You can use your data in reports, Web pages, and databases, and that's just for starters. When you need to exchange data, XML can meet almost any business, scientific, or academic need.

More parts of an XML data file.

1 Declarations2 Root element3 Attribute4 Tags and data

A transform: the third basic component of any XML system.

The third main component of a typical XML system is a transform. A transform allows you to use the same data in many different ways. The XML data that you have filed and structured so carefullycan now be put to use in reports, databases, Web pages, and a growing number of other applications.This is where XML can become very exciting.

Transforms can automate data exchange.

A transform provides a set of rules (yes, more rules) for converting data described by one set of tagsinto data described by another set of tags.

For example, say your sales department stores its data in Excel workbooks, and your accounting department needs that information imported into a database. A transform can write data from preselected cells in the worksheet into the correct fields in the database.

1 One set of tags builds a table on a Web page.2 One declaration brings XML data into the table.3 Each table cell shows a specific piece of data.

XML Document Example<?xml version="1.0" encoding="UTF-8"?><note> <to> Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body></note>

The Difference Between XML and HTML

XML is not a replacement for HTML.

XML and HTML were designed with different goals:

XML was designed to describe data, with focus on what data is HTML was designed to display data, with focus on how data looks

HTML is about displaying information, while XML is about carrying information.

XML Separates Data from HTML

If you need to display dynamic data in your HTML document, it will take a lot of work to edit the HTML each time the data changes.

With XML, data can be stored in separate XML files. This way you can concentrate on using HTML/CSS for display and layout, and be sure that changes in the underlying data will not require any changes to the HTML.

With a few lines of JavaScript code, you can read an external XML file and update the data content of your web page.

XML Simplifies Data Sharing

In the real world, computer systems and databases contain data in incompatible formats.

XML data is stored in plain text format. This provides a software- and hardware-independent way of storing data.

This makes it much easier to create data that can be shared by different applications.XML Simplifies Data Transport

One of the most time-consuming challenges for developers is to exchange data between incompatible systems over the Internet.

Exchanging data as XML greatly reduces this complexity, since the data can be read by different incompatible applications.

XML Simplifies Platform Changes

Upgrading to new systems (hardware or software platforms), is always time consuming. Large amounts of data must be converted and incompatible data is often lost.

XML data is stored in text format. This makes it easier to expand or upgrade to new operating systems, new applications, or new browsers, without losing data.XML Makes Your Data More Available

Different applications can access your data, not only in HTML pages, but also from XML data sources.

With XML, your data can be available to all kinds of "reading machines" (Handheld computers, voice machines, news feeds, etc.), and make it more available for blind people, or people with other disabilities.

Internet Languages Written in XML

Several Internet languages are written in XML. Here are some examples:

XHTML XML Schema SVG WSDL RSS

XML DTD's

The purpose of a DTD (Document Type Definition) is to define the legal building blocks of an XML document.

A DTD 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>

Open the XML file above in your browser (select "view source" or "view page source" to view the DTD)

The DTD above is interpreted like this:

!DOCTYPE note defines that the root element of this document is note !ELEMENT note defines that the note element contains four elements: "to,from,heading,body" !ELEMENT to defines the to element to be of type "#PCDATA" !ELEMENT from defines the from element to be of type "#PCDATA" !ELEMENT heading defines the heading element to be of type "#PCDATA" !ELEMENT body defines the body element to be of type "#PCDATA"

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 (Open it, and select view source):<?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

Seen from a DTD point of view, all XML documents (and HTML documents) are made up by the

following building blocks:

Elements Attributes Entities PCDATA CDATA

Elements

Elements are the main building blocks of both XML and HTML documents.

Examples of HTML elements are "body" and "table". Examples of XML elements could be "note" and "message". Elements can contain text, other elements, or be empty. Examples of empty HTML elements are "hr", "br" and "img".

Examples:<body>some text</body>

<message>some text</message>

Attributes

Attributes provide extra information about elements.

Attributes are always placed inside the opening tag of an element. Attributes always come in name/value pairs. The following "img" element has additional information about a source file:<img src="computer.gif" />

The name of the element is "img". The name of the attribute is "src". The value of the attribute is "computer.gif". Since the element itself is empty it is closed by a " /".Entities

Some characters have a special meaning in XML, like the less than sign (<) that defines the start of an XML tag.

Most of you know the HTML entity: "&nbsp;". This "no-breaking-space" entity is used in HTML toinsert an extra space in a document. Entities are expanded when a document is parsed by an XML parser.

The following entities are predefined in XML:Entity References Character&lt; <&gt; >&amp; &&quot; "&apos; ' PCDATA

PCDATA means parsed character data.

Think of character data as the text found between the start tag and the end tag of an XML element.

PCDATA is text 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.

How can XML be used?

XML can keep data separated from your HTML XML can be used to store data inside HTML documents XML can be used as a format to exchange information XML can be used to store data in files or in databases

Creating xml file from mysql:

sai@sai-Presario-CQ62-Notebook-PC:~$ mysql -u root -p --xml -e 'SELECT * FROM student.emp' > /home/sai/output.xmlEnter password: sai@sai-Presario-CQ62-Notebook-PC:~$

Use of XML in HTML:

//file name sample.html

<!DOCTYPE html><!--To change this license header, choose License Headers in Project Properties.To change this template file, choose Tools | Templatesand open the template in the editor.--><html><body>

<script>if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); }else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }xmlhttp.open("GET","cd_catalog.xml",false);xmlhttp.send();xmlDoc=xmlhttp.responseXML;

document.write("<table border='1'>");var x=xmlDoc.getElementsByTagName("CD");for (i=0;i<x.length;i++) { document.write("<tr><td>"); document.write(x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue); document.write("</td><td>"); document.write(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue); document.write("</td></tr>"); }document.write("</table>");</script>

//file Name:cd_catolog.xml

<?xml version="1.0" encoding="UTF-8"?><CATALOG>

<CD><TITLE>Empire Burlesque</TITLE><ARTIST>Bob Dylan</ARTIST><COUNTRY>USA</COUNTRY><COMPANY>Columbia</COMPANY><PRICE>10.90</PRICE><YEAR>1985</YEAR>

</CD><CD>

<TITLE>Hide your heart</TITLE><ARTIST>Bonnie Tyler</ARTIST><COUNTRY>UK</COUNTRY><COMPANY>CBS Records</COMPANY><PRICE>9.90</PRICE><YEAR>1988</YEAR>

</CD><CD>

<TITLE>Greatest Hits</TITLE><ARTIST>Dolly Parton</ARTIST><COUNTRY>USA</COUNTRY><COMPANY>RCA</COMPANY><PRICE>9.90</PRICE><YEAR>1982</YEAR>

</CD></CATALOG>

Run sample.xml file Output would be followed

Bob Dylan Empire BurlesqueBonnie Tyler Hide your heartDolly Parton Greatest Hits