What is XML? XML stands for EXtensible Markup Language XML is a markup language much like HTML XML...

58
What is XML? •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-

Transcript of What is XML? XML stands for EXtensible Markup Language XML is a markup language much like HTML XML...

Page 1: What is XML? 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.

What is XML?

•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

Page 2: What is XML? 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.

• XML is not a replacement for HTML.

• XML and HTML were designed with different goals:– XML was designed to transport and store

data, with focus on what data is. – HTML was designed to display data, with

focus on how data looks.

Page 3: What is XML? 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.

• XML does not DO anything.

• XML was created to structure, store, and transport information.

• But still, this XML document does not DO anything. It is just pure information wrapped in tags.

Page 4: What is XML? 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.

Examplexml1.xml

<CATALOG><BOOK>

<TITLE>5 points for someone</TITLE><ARTIST>Chetan B.</ARTIST><COUNTRY>India</COUNTRY><PRICE>10.90</PRICE><YEAR>2004</YEAR>

</BOOK></CATALOG>

Page 5: What is XML? 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.

XML trees

• An xml document is a hierarchical structure called an xml tree which consist of various kinds arranged as a tree.

Page 6: What is XML? 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.

CATLOG

BOOK

TITLE AUTHOR COUNTRY PRICE YEAR

Page 7: What is XML? 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.

An XML document contains XML Elements.<bookstore>

  <book category="CHILDREN">    <title>Harry Potter</title>    <author>J K. Rowling</author>    <year>2005</year>    <price>29.99</price>  </book>

  <book category="WEB">    <title>Learning XML</title>    <author>Erik T. Ray</author>    <year>2003</year>    <price>39.95</price>  </book>

</bookstore>

Page 8: What is XML? 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.

XML (tag) Naming Rules

XML elements must follow these naming rules:• Names can contain letters, numbers, and other

characters • Names cannot start with a number or

punctuation character • Names cannot start with the letters xml (or XML,

or Xml, etc) • Names cannot contain spaces • Any name can be used, no words are reserved.

Page 9: What is XML? 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.

Best Naming Practices• Make names descriptive. Names with an underscore separator are nice:

<first_name>, <last_name>.

• Names should be short and simple, like this: <book_title> not like this: <the_title_of_the_book>.

• Avoid "-" characters. If you name something "first-name," some software may think you want to subtract name from first.

• Avoid "." characters. If you name something "first.name," some software may think that "name" is a property of the object "first.“

• Avoid ":" characters. Colons are reserved to be used for something called namespaces (more later).

• XML documents often have a corresponding database. A good practice is to use the naming rules of your database for the elements in the XML documents.

• Non-English letters like éòá are perfectly legal in XML, but watch out for problems if your software vendor doesn't support them.

Page 10: What is XML? 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.

XML Elements are Extensible

<bookstore> <book category="CHILDREN">    <title>Harry Potter</title>    <author>J K. Rowling</author>    <year>2005</year>    <price>29.99</price>

<edition> 1st </edition>  </book>

<book category="WEB">    <title>Learning XML</title>    <author>Erik T. Ray</author>    <year>2003</year>    <price>39.95</price>  </book>

</bookstore>

Page 11: What is XML? 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.

Xml syntax

• All tags tag should have closing tag.

• All tags are case sensitive.

• Xml elements must be properly nested

• Xml document must have root element is must.

• Attributes must be in double quotes.

Page 12: What is XML? 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.

• Entity References

• Some characters have a special meaning in XML.

• If you place a character like "<" inside an XML element, it will generate an error because the parser interprets it as the start of a new element.

Page 13: What is XML? 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.

• This will generate an XML error:

<message>if salary < 1000 then</message>

To avoid this error, replace the "<" character with an entity reference:

<message>if salary &lt; 1000 then</message>

Page 14: What is XML? 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.

• There are 5 predefined entity references in XML:

&lt; <less than

&gt; >greater than

&amp; ampersand 

&apos; 'apostrophe

&quot; "quotation mark

Page 15: What is XML? 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.

• Comments in XML

The syntax for writing comments in XML is similar to that of HTML.

<!-- This is a comment -->

• White-space is Preserved in XML

HTML truncates multiple white-space characters to one single white-space:

HTML: Hello           my name is Tove Output: Hello my name is Tove.

Page 16: What is XML? 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.

Xml elements attributes

• XML elements can have attributes in the start tag, just like HTML.

• Attributes provide additional information about elements.

Page 17: What is XML? 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.

• <img src="computer.gif"><a href="demo.asp">

Page 18: What is XML? 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.

<person sex="female">

or like this:

<person sex='female'>

If the attribute value itself contains double quotes you can use single quotes, like in this example:

<gangster name='George "Shotgun" Ziegler'>

Or you can use character entities:

<gangster name="George &quot;Shotgun&quot; Ziegler">

Page 19: What is XML? 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.

• Avoid XML Attributes?

• Some of the problems with using attributes are:• attributes cannot contain multiple values

(elements can) • attributes cannot contain tree structures

(elements can) • attributes are not easily expandable (for future

changes) • Attributes are difficult to read and maintain. Use

elements for data. Use attributes for information that is not relevant to the data.

Page 20: What is XML? 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.

Id <messages>

  <note id="501">    <to>Tove</to>    <from>Jani</from>    <heading>Reminder</heading>    <body>Don't forget me this weekend!</body>  </note>

  <note id="502">    <to>Jani</to>    <from>Tove</from>    <heading>Re: Reminder</heading>    <body>I will not</body>  </note></messages>

Page 21: What is XML? 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.

Xml and css

• With css you can add display information to xml.

• Eg

• Cd_catlog.css

• Cd_catlog.xml

• Cd_catlog1.xml

Page 22: What is XML? 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.

Other way isXSLT

Page 23: What is XML? 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.

• Xsl : extensible stylesheet language.

• Xslt : xsl transform.

(language for xml transformation )

XSLT is a language for transforming XML documents into XHTML documents or to other XML documents.

Page 24: What is XML? 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.

• CSS = Style Sheets for HTML– HTML uses predefined tags, and the meaning of each tag is well

understood.– The <table> tag in HTML defines a table - and a browser knows

how to display it.– Adding styles to HTML elements are simple. Telling a browser to

display an element in a special font or color, is easy with CSS. 

• XSL = Style Sheets for XML– XML does not use predefined tags (we can use any tag-names

we like), and therefore the meaning of each tag is not well understood.

– A <table> tag could mean an HTML table, a piece of furniture, or something else - and a browser does not know how to display it.

– XSL describes how the XML document should be displayed!

Page 25: What is XML? 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.

XSLT = XSL Transformations

• XSLT is the most important part of XSL.• XSLT is used to transform an XML document into

another XML document, or another type of document that is recognized by a browser, like HTML and XHTML. Normally XSLT does this by transforming each XML element into an (X)HTML element.

• With XSLT you can add/remove elements and attributes to or from the output file. You can also rearrange and sort elements, perform tests and make decisions about which elements to hide and display, and a lot more.

• A common way to describe the transformation process is to say that XSLT transforms an XML source-tree into an XML result-tree.

Page 26: What is XML? 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.

• Correct Style Sheet Declaration• The root element that declares the document to be an

XSL style sheet is <xsl:stylesheet> or <xsl:transform>.

• <xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform

or• <xsl:transform version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

Page 27: What is XML? 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.

XSLT <xsl:template> Element

• An XSL style sheet consists of one or more set of rules that are called templates.

• A template contains rules to apply when a specified node is matched

Page 28: What is XML? 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.

• The <xsl:template> element is used to build templates.

• The match attribute is used to associate a template with an XML element. The match attribute can also be used to define a template for the entire XML document. The value of the match attribute is an XPath expression (i.e. match="/" defines the whole document).

Page 29: What is XML? 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.

<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0“ xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">  <html>   <body>   <h2>My CD Collection</h2>  <table border="1">    <tr bgcolor="#9acd32">      <th>Title</th>      <th>Artist</th>    </tr>    <tr>      <td>.</td>      <td>.</td>    </tr>  </table>  </body>  </html></xsl:template></xsl:stylesheet>

Page 30: What is XML? 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.

XSLT <xsl:value-of> Element

• The <xsl:value-of> element is used to extract the value of a selected node.

Page 31: What is XML? 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.

Book.xml<?xml version="1.0" encoding="ISO-8859-1"?>

<?xml-stylesheet type="text/xsl" href="simplexslt.xsl"?>

<CATALOG><BOOK>

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

</BOOK><CATALOG>

Page 32: What is XML? 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.

<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">  <html>  <body>  <h2>My Collection</h2>  <table border="1">    <tr>      <th>Title</th>      <th>author </th>    </tr>    <tr>      <td><xsl:value-of select="catalog/book/title"/></td>      <td><xsl:value-of select="catalog/book/author"/></td>    </tr>  </table>  </body>  </html></xsl:template>

</xsl:stylesheet>

Page 33: What is XML? 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.

• The <xsl:for-each> Element

• The XSL <xsl:for-each> element can be used to select every XML element of a specified node-set:

Page 34: What is XML? 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.

<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">  <html>  <body>  <h2>My CD Collection</h2>  <table border="1">    <tr bgcolor="#9acd32">      <th>Title</th>      <th>Artist</th>    </tr>    <xsl:for-each select="catalog/cd">    <tr>      <td><xsl:value-of select="title"/></td>      <td><xsl:value-of select="artist"/></td>    </tr>    </xsl:for-each>  </table>  </body>  </html></xsl:template></xsl:stylesheet>

Page 35: What is XML? 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.

• The <xsl:if> Element

• To put a conditional if test against the content of the XML file, add an <xsl:if> element to the XSL document.

• Syntax

• <xsl:if test="expression">  ...some output if the expression is true...</xsl:if>

Page 36: What is XML? 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.

• <?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">  <html>  <body>  <h2>My CD Collection</h2>  <table border="1">    <tr bgcolor="#9acd32">      <th>Title</th>      <th>Artist</th>    </tr>    <xsl:for-each select="catalog/cd">      <xsl:if test="price &gt; 10">        <tr>          <td><xsl:value-of select="title"/></td>          <td><xsl:value-of select="artist"/></td>        </tr>      </xsl:if>    </xsl:for-each>  </table>  </body>  </html></xsl:template></xsl:stylesheet>

Page 37: What is XML? 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.

<xsl:for-each select="catalog/cd[artist='Bob Dylan']">

Page 38: What is XML? 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.

• The <xsl:sort> element is used to sort the output.

Page 39: What is XML? 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.

<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">  <html>  <body>  <h2>My CD Collection</h2>  <table border="1">    <tr bgcolor="#9acd32">      <th>Title</th>      <th>Artist</th>    </tr>    <xsl:for-each select="catalog/cd">      <xsl:sort select="artist"/>      <tr>        <td><xsl:value-of select="title"/></td>        <td><xsl:value-of select="artist"/></td>      </tr>    </xsl:for-each>  </table>  </body>  </html></xsl:template></xsl:stylesheet>

Page 40: What is XML? 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.

<xsl:for-each select="catalog/cd">    <tr>      <td><xsl:value-of select="title"/></td>      <xsl:choose>        <xsl:when test="price &gt; 10">          <td bgcolor="#ff00ff">          <xsl:value-of select="artist"/></td>        </xsl:when>        <xsl:otherwise>          <td><xsl:value-of select="artist"/></td>        </xsl:otherwise>      </xsl:choose>

Page 41: What is XML? 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.

A JavaScript Solution

• We have explained how XSLT can be used to transform a document from XML to XHTML. We did this by adding an XSL style sheet to the XML file and let the browser do the transformation. Even if this works fine, it is not always desirable to include a style sheet reference in an XML file (e.g. it will not work in a non XSLT aware browser.)

• A more versatile solution would be to use a JavaScript to do the transformation.

• By using a JavaScript, we can:• do browser-specific testing

• use different style sheets according to browser and user needs

Page 42: What is XML? 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.

<html> <body>

<script type="text/javascript">

function loadXMLDoc(dname)

{

if (window.XMLHttpRequest) { xhttp=new XMLHttpRequest(); }

else

{ xhttp=new ActiveXObject("Microsoft.XMLHTTP"); }

xhttp.open("GET",dname,false);

xhttp.send("");

return xhttp.responseXML;

}

xml=loadXMLDoc("cd_catalog.xml");

path="/CATALOG/CD[PRICE > 9]/TITLE"

// code for IE

if (window.ActiveXObject)

{var nodes=xml.selectNodes(path);

for (i=0;i<nodes.length;i++)

{ document.write(nodes[i].childNodes[0].nodeValue);

document.write("<br />");

} }

Page 43: What is XML? 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.

// code for Mozilla, Firefox, Opera, etc.

else if (document.implementation && document.implementation.createDocument)

{

var nodes=xml.evaluate(path, xml, null, XPathResult.ANY_TYPE, null);

var result=nodes.iterateNext();

while (result)

{

document.write(result.childNodes[0].nodeValue);

document.write("<br />");

result=nodes.iterateNext();

}

}

</script>

</body>

</html>

Page 44: What is XML? 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.

• Try2.html

• Try3.html

Page 45: What is XML? 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.

What is XPath?• XPath is a syntax for defining parts of an

XML document

• XPath uses path expressions to navigate in XML documents

• XPath contains a library of standard functions

• XPath is a major element in XSLT

• XPath is a W3C recommendation

Page 46: What is XML? 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.

XPATH terminologies

• Parent

• Children

• Siblings

• Ancestors

• Descendants

Page 47: What is XML? 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.

XPATH

Loading the XML Document

• Using XMLHttpRequest to load XML documents is supported in all modern browsers.

• Code for most modern browsers:

var xmlhttp=new XMLHttpRequest()

• Code for old Microsoft browsers (IE 5 and 6):

var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")

Page 48: What is XML? 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.

Selecting Nodes

• Internet Explorer uses the selectNodes() method to select nodes from the XML document:

xmlDoc.selectNodes(xpath);

• Firefox, Chrome, Opera and Safari use the evaluate() method to select nodes from the XML document:

xmlDoc.evaluate(xpath, xmlDoc, null, XPathResult.ANY_TYPE,null);

• var xpathResult = document.evaluate( xpathExpression, contextNode, namespaceResolver, resultType, result );

Page 49: What is XML? 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.

• xpathExpression: A string containing the XPath expression to be evaluated. • contextNode: A node in the document against which the xpathExpression should be evaluated,

including any and all of its child nodes. The document node is the most commonly used. • namespaceResolver: A function that will be passed any namespace prefixes contained within

xpathExpression which returns a string representing the namespace URI associated with that prefix. This enables conversion between the prefixes used in the XPath expressions and the possibly different prefixes used in the document. The function can be either:

– null, which can be used for HTML documents or when no namespace prefixes are used. Note that, if the xpathExpression contains a namespace prefix, this will result in a DOMException being thrown with the code NAMESPACE_ERR.

– A custom user-defined function. See the Using a User Defined Namespace Resolver section in the appendix for details.

• resultType: A constant that specifies the desired result type to be returned as a result of the evaluation. The most commonly passed constant is XPathResult.ANY_TYPE which will return the results of the XPath expression as the most natural type. There is a section in the appendix which contains a full list of the available constants. They are explained below in the section "Specifying the Return Type."

• result: If an existing XPathResult object is specified, it will be reused to return the results. Specifying null will create a new XPathResult object.

Page 50: What is XML? 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.

• The following example selects all the title nodes:

Example /bookstore/book/title

• Select the title of the first book

The following example selects the title of the first book node under the bookstore element: Example /bookstore/book[1]/title

• Select title nodes with price>35

• The following example selects all the title nodes with a price higher than 35:

• Example

• /bookstore/book[price>35]/title

Try4.html

Page 51: What is XML? 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.

Expressions Path Expression Result

bookstore Selects all the child nodes of the bookstore element

/bookstore Selects the root element bookstore Note: If the path starts with a slash ( / ) it always represents an absolute path to an element!

bookstore/book Selects all book elements that are children of bookstore

//book Selects all book elements no matter where they are in the document

bookstore//book Selects all book elements that are descendant of the bookstore element, no matter where they are under the bookstore element

//@lang Selects all attributes that are named lang

Page 52: What is XML? 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.

• Predicates

– Finds exact node

Page 53: What is XML? 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.

Path Expression Result/bookstore/book[1] Selects the first book element that is the child of the

bookstore element. Note: IE5 and later has implemented that [0] should be the first node, but according to the W3C standard it should have been [1]!!

/bookstore/book[last()] Selects the last book element that is the child of the bookstore element

/bookstore/book[last()-1] Selects the last but one book element that is the child of the bookstore element

/bookstore/book[position()<3] Selects the first two book elements that are children of the bookstore element

//title[@lang] Selects all the title elements that have an attribute named lang

//title[@lang='eng'] Selects all the title elements that have an attribute named lang with a value of 'eng'

/bookstore/book[price>35.00] Selects all the book elements of the bookstore element that have a price element with a value greater than 35.00

/bookstore/book[price>35.00]/title Selects all the title elements of the book elements of the bookstore element that have a price element with a value greater than 35.00

Page 54: What is XML? 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.

Wild card characters

Path Expression Result/bookstore/* Selects all the child nodes of the bookstore element//* Selects all elements in the document//title[@*] Selects all title elements which have any attribute

Page 55: What is XML? 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.

Selecting several paths

Path Expression Result

//book/title | //book/price Selects all the title AND price elements of all book elements

//title | //price Selects all the title AND price elements in the document

/bookstore/book/title | //price Selects all the title elements of the book element of the bookstore element AND all the price elements in the document

Page 56: What is XML? 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.

SAX - Simple API for XML Parsing

• Being the first in the evolution ladder, it obviously had only the basic support for XML processing.

• It is an event-based technology, which uses callbacks to load the parts of the XML document in a sequential way. This effectively means you can't go back to some part which was read/processed previously - if you do have such a requirement then you would need to store/manage the relevant data yourself.

• Since this API does require to load the entire XML doc and also because it offers only a sequential processing of the doc hence it is quite fast. Another reason of it being faster is that it does not allow modification of the underlying XML data.

Page 57: What is XML? 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.

DOM - Document Object Model

• The Java binding for DOM provided a tree-based representation of the XML documents - allowing random access and modification of the underlying XML data. Not very difficult to deduce that it would be slower as compared to SAX.

• The event-based callback methodology was replaced by an object-oriented in-memory representation of the XML documents.

Page 58: What is XML? 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.

SAX DOMBoth 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.

Parses node by nodeStores the entire XML document into memory before processing

Doesn’t store the XML in memory Occupies more memory

We cant insert or delete a node We can insert or delete nodes

Top to bottom traversing Traverse in any direction.SAX is an event based parser DOM is a tree model parser

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

import javax.xml.parsers.*;import org.xml.sax.*;import org.xml.sax.helpers.*;

import javax.xml.parsers.*;import org.w3c.dom.*; 

doesn’t preserve comments preserves commentsSAX generally runs a little faster than DOM

SAX generally runs a little faster than DOM

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.