0% found this document useful (0 votes)
17 views

Ip - XML

Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
17 views

Ip - XML

Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 51
Invernet Programming ML. Introduction to XML XML stands for eXtensible Markup Language. This scripting language is similar to HTML, That means, this scripting language contains various tags, But these tags are not predefined tags, in-fact user can define his own tags. Thus HTML is designed for representation of data on the web page whereas the XML is designed for transport or to store data. Uses of XML np XML is used to display the meta contents i.c. XML describes the content of the document. XML is useful in exchanging data between the applications. 3. The data can be extracted from database and can be used in more than one application. Different applications can perform different tasks on this data. Advantages of XML 1. XML document is human readable and we can edit any XML document in simple text editors. 2. The XML document is language neutral. That means a Java program can generate an XML document and this document can be parsed by Perl. 3. Every XML document has a tree structure. Hence complex data can be arranged systematically and can be understood in simple manner. 4, XML files are independent of an operating system. Goals of XML Following are the goals of XML - User must be able to define and use his own tag. This allows us to restrict the use of -"t of tags defined by proprietary vendors. 2. Allow user to build his own tag library based on his web requirement. 3. Allow user to define the formatting rules for the user defined tags. 4. XML must support for storage or transport of data. Features of XML Following are some features which are most suitable for creating web related applications. XML is EXtesible Markup Language intended for transport or storage of the data. The most important feature of XML is that user is able to define and use his own tag. XML contains only data and does not contain any formatting information. Hence do »ment developers can decide how to display the data. - Technical Publications” - An up thrust far knowledge YM, eternct Programming © XML permits the document writer to ercate the tags for any type nf information, Due ta thie virtually any kind of information can be such ax mathematical formulae, chemical structures, o some other kind of data can be deseribed using, XML. + Searching sorting, rendering oF manipulating the XML document ix possible wing Kxtended Stylesheet Language (XSL). + The XML document can be validated using, sosne external tools * Some commonly used web browsers like Internet Explorer and Firefox Mozilla provide support to the tags in XML. Hence XML is not at all vendor specific or braver specific, Difference between XML and HTML Sr.No. TML XML 1. HTML stands for Hypertext Markup XML stands for eXtensible Markup Language. Language. TITML is designed to display data with the XML ts used to transport and sure data, | focus on look and fee! of data. focus on what data is. HTML is case insensiti XML is & HTML has predefined tags XML tas custora waxy can be defined and te tays are invented by the author of the document. ‘As HTML is for displaying the data itis As XML is for carrying the data nis _ static. dynamic, ‘can not preserve white space. ‘It can preserve the white space. EJ Building Block of XML Document Elements and Attributes « Various building blocks of XML are - 4. Elements The basic entity is element. The elements are used for defining the tags. The elements typically consist of opening and closing tag. Mostly only one element is used to define a single tz 2. Attribute ‘The attributes are generally used to specify the values of the element. These are specified within the double quotes. For example - ‘The type attribute of the element flag is having the value True. 3. CDATA CDATA stands for Character Data. This character data will be parsed by the parser. " Fectnical Pbécatans Arup ii fr trowledze Internet Programming 8-4 4, PCDATA It stands for Parsed Character Data (i Declaring Elements * The element type data can be declared using the ELEMENT. + The syntax for defining the ELEMENT is, ‘SIELEMENTelt_name (elt1,elt2,..)> XML Document [Internal_Elt.xml] Here we are defining the DTD internally iba Anand
Pune
Second
70 percent
Diam examples\nternal itami View Favortes Teck Help #2 Ocam_examples\tnternal Eka! ="1.0" encoding ="UTF-8* ?> SIDOCTYPE student (View Source for full doctype...)> + Anand kaddresesPune /addreos> Second/std> sTharks>70 percent e/student> 17 "F Technical Publications” - An up thrust for knowledge XML | Internet Programming Nate * The ELEMENT must be defined in the beginning of the XML file. It must be enclosed within * There are various ways by which the actual element can be defined. It is enlisted in the following table - rr = : z | Category Syntax Description - Empty npty elements can be ? declared. © Character. ‘Any combination of data can |“ content. be declared. j Sequence of elements that must appear in specific order, | * Sometimes we can define the ELEMENT along with the iterator characters. The iterator characters are the special character which are associated with some meaning. © Some commonly used iterator characters are as given below - “Iterator Meaning, Example ‘character 2 The preceding choice appears zero or once. The child clement marks can appear for zero or one time. * The preceding choice appears for any number of time(zero or more times). The person can appear for any number of times. s The preceding choice appears for at . least once (one or more times). “The person cha dopeng,farral etl once. ee (B22 Declaring Attributes ¢ The attribute type declaration is done using the string ATTLIST. + The attribute type specifies the type of data which can be used for specifying the attribute. The syntax for specifying the attribute declaration is as follows - 7 Tir _,- Technical Publications” - An up thrust for knowledge Internet Programming 8-6 XML + For example sIATTLIST address phone CDATA #REQUIRED> Element_name | Attribute_type Attribute_name Default_value * The XML document containing attributes is as given below - XML Document [Internal_Attr.xml] ‘ Using ATTLIST the a I> Anand address phone="9898123123">Pune ‘ Tenth
70 percent
Output | Favorites ert examples\Interal_Atteam! ~ Anand saddress phona="9098129123">Pune sstd>Tenth arf emarks>70 percent e/student> i Internet Programming * Following are some attribute type ~ Attribute Type Synlax Description 4 Atiribute val s character data. Character data. = CDATA Enumerated (stelstr2)...) Attribute value must be one from list. ID Identifier IDREF reference Identifier IDREFS, ‘Attribute value is a list of other reference list, ‘Name token NMTOKEN ‘Attribute value is a valid XML name. ‘Name token list. NMTOKENS ‘Attribute value is a list of valid XML names. The default_values are - Default_Value Meaning Value Default value can be defined. For example - In XML document Ifthe flag is not assigned with any value then ‘by default it will be 0. #REQUIRED Required attribute forces the attribute to be present even ifthere is no default value. #IMPLIED ‘The default value is not provided and attribute can be optionally used. | #FIXED value ‘The attribute value is fixed and cannot be changed. Declaring Entities «The attribute type declaration is done using the string ENTITY. + Inside the XML file at the beginning the entity can be defined using the and with the help of string ENTITY. Following XML document demonstrates it - XML Document [EntityDemo.xml] « ae Tecfrical Publcatons - An up trast hnowledge Iniernet Programming . en AMI. SIDGCTYPE Book cue Seer. Mmbeckar Web Technologies =/bo0k~ Be Computer | Protected Meste: Off Note that we can define the predefined entities   along with the other entity declaration. The entity can be used using following syntax. &name_of_entity; Im above script we have used two entities namely writer and title. For displaying the space within these two entities we have used an entity nbsp and for defining it we have used #160, 1. What are the features of elements and attributes in XML? ES Rules ¢ Rules that must be followed while writing XML - Here are some rules that must be followed while writing the XML programs - 1, XML is a case sensitive. For example - ! like drawingor I like drawing is not allowed because the words hobby and Hobby are treated differently. 2. In XML each start tag must have matching end tag. For example- ‘ XML is funny to write It is simple to implement That means a closing tag is a must, 3. The elements in XML must be properly nested. For example- Hello how are you? is wrong but Hello how are you? is correct 7 Technical Publications” - An up thrust fer knowledge Internet Programming a wt Eg Namespace ao ees tc two different elements by the same name. The xml document * Sometimes we need to creat ie is allows us to create different elements which are having the common name. This techniqu known as namespace. + In some web documents it becomes necessary to have the same name Here different elements mean the elements which are intended for different purposes. In such & for two different elements. case support for namespace technique is very much helpful. + For example : Consider the following xml document - namespacedemo.xml It is a text file It is an image file The above document does not produce any error although the element text is used for two different attribute values. The output will be, Realises Ele Ect Yew Hitory Bookmarks Tools elo GD SX ee (teen zeccnenenntis 5 tnt [care is |) ea) Wd Bi nos vated MP Getting started ‘This KML file does not appear to have any style information associated with it The document, tree ie shown below = = ~ describe>Itis an image fle i etl} 1. Why should namespaces be used in XML ? Explain with an example. 2. Explain the role of XML name space with examples, CULE eee ma TeetnialPubleatons Anup trust for knowicdge Internet Programming. 8-10 I Document Type Definition * The document type definition is used to define the basic building block of any xml document, Using DTD we can specify the various elements types, attributes and their relationship with one another, Basically DTD is used to.specify the set of rules for structuring data in any XML file For example : If we want to put some information about some students in XML file then, generally we use tag student followed by his/her name, address, standard and marks. That means we are actually specifying the manner by which the information should be arranged in the XML file. And for this purpose the Document Type Definition is used. Various building blocks of XML are — |. Elements The basic entity is element. The elements are used for defining the tags. The elements typically consist of opening and closing tag. Mostly only one element is used to define a single tag. 2. Attribute The attributes are generally used to specify the values of the element. These are specified within the double quotes. For example - The type attribute of the element flag is having the value True. 3. CDATA CDATA stands for character data, This character data will be parsed by the parser. 4, PCDATA It stands for Parsed Character Data (i.e. text). Any parsable character data should not contain the markup characters. The markup characters are < or > or &. If we want to use less than, greater than or ampersand characters then make use of < , > or & * There are two ways by which DTD can be defined. Internal DTD Consider following xml document — XML Document [DTDDemol.xml] Here we are defining the DTD internally Technical Publications " . An up thrust for knowtedge Internet Programming bell XML > Anand
Pune
Second 70 percent
Output | Tis XML file does not appear to have any style information associated | with it. The document tree is shown below. | - | | Anand | |
Pune
| | Second | 70 percent | | External DTD In this type, an external DTD file is created and its name must be specified in the corresponding XML file. Following XML document illustrates the use of external DTD. Step 1: Creation of DTD file [student.dtd] Open some suitable text editor or a notepad. Type following code into it - Now save this file as student.dtd Step 2: Creation of XML document - XML Document [DTDDemo.xml] Now create a XML document as follows - 5 Anand The external DTD file is created Technical Publications” - An up thrust for knowledge Internet Programming 8-12 AML
Pune
Second 70 percent
Step 3: Using some web browser open the XML document, Output Sead TX Mew toy Geometers GEE © te (Amit semceiiome at a i Most vated (> Getting Sterted |i. Latest Headines Gone iis 2 ree retmat wedew hs OMI Ble does not appear to have acy syle efermaton assocuted wath it The document Fee is chown below - sname>Anandoiname> saddress>Puse std>Second
smarks>70 percestsiinsaks> SIATTLIST accessories disc-brake (yes |no) #REQUIRED auto-start (yes|no) #REQUIRED Tadio (yes|no) #REQUIRED> engine,chasis-num,accessories ) > ae Technical Publications An um thnicr toe barsatnann Ex. 8.5.2 : Consider a hospital management system. Write a DTD program to consolidate and show the bill (o be paid by the inpatients (Assume your own data), EO Sol. : Patients.xmt Anuradha
Pune
ECG 1-1-2010 Mr-X¥Z Rs.500
Archana
Mumbai
Blood Test 10-3-2010 Mr.POR Rs.100
Merits of DTD 1, DTDs are used to define the structural components of XML document 2. These are relatively simple and compact. 3, DTDs can be defined inline and hence can be embedded directly in the XML document. Demerits of DTD 1. The DTDs are very basic and hence cannot be much specific for complex document: 2. The language that DTD uses is not an XML document. Hence various frameworks used by XML cannot be supported by the DTDs. Internet Programming 8-l4 XML 3. The DTD cannot define the type of data contained within the XML document. Hence using DTD we cannot specify whether the clement is numeric, or string or of date type. 4, - There are some XML processor which do not understand DTDs. 5. The DTDs are not aware of namespace concept. RINGS ACU 1. Explain the types of DTD in XML with an example. AU!: May-19, Marks 13 GI XL schema ERE Geter + The XML schemas are used to represent the structure of XML document. + The goal or purpose of XML schema is to define the building blocks of an XML document. ‘These can be used as an alternative to XML DTD. * The XML schema language is called as XML Schema Definition (XSD) language. * XML schema defines elements, attributes, elements having child elements, order of child elements. It also defines fixed and default values of elements and attributes. + XML schema also allows the developer to use data types. How to write a simple schema 7 Step 1: We will first write a simple xsd file in which the desired structure of the XML document is defined. I have named it as StudentSchema.xsd This file contains the complex type with four child simple type elements. XML Schema [StudentSchema.xsd] element name="std" typ ‘xs:string'/> lement name="marks" type="xs:string'/> Internet Programming. 6-15 XML ‘Script Explanation : 1) The xs is qualifier used to identify the schema elements and types. hema is the root element. Tt 2) The document element of schema is xs:schema. The xs:s¢ has the value takes the ~— attribute. «= xmlns:xs._—— which hetp://www.w3.org/2001/XMLSchema, This declaration indicates that document should follow the rules of XML schema. The XML schema rules are defined by the W3 recommendation in year 2001. 3) Then comes xs:element which is used to define the xml element. In above case the clement Student is of complex type who have four child elements : name, address, std and marks. All these elements are of simple type string. Step 2: Now develop XML document in which the desired values to the XML elements can be given. I have named this file as MySchema.xml XML Document /MySchema.xml] Anand
Pune
Second 70 percent
Program Explanation : 1) In above XML document, the first line is typical in which the version and encoding is specified. 2) There is xmlns:xsi attribute of document which indicates that XML document is an instance of XML schema. And it has come from the namespace “http://www.w3.org/2001/XMLSchema-instance.” 3) To tie this XML document with the some Schema definition we use the attribute xsi:noNamespace SchemaLocation. The value that can be passed to this attribute is the name of xsd file “StudentSchema.xsd”. 4) After this we can define the child elements. Internet Pro -16 AML Step 3: See the output in the browser window as follows - E ves Fretn : [Be ka Yew we footmats. Teel Help @- 49 {est tani fer i Mlecinc Latest Headlines Ba oa ic The document wees | Second
‘smarks>70 percent 1 / Ly | | | i EXGHI Data Types Various data types that can be used to specify the data types ofan element are : + String * Date © Numeric * Boolean Let us discuss each of these data types with the help of examples - 1. String data type ‘The string data type is used to define the element containing characters, lines, tabs or white spaces. Following schema definition illustrates this data type. We can write a schema definition in which the data type of clement Student_Name is declared as of string type. XML Schema [stringType.xsd] ‘ ‘Sxsischema smnlns:xs="http://www.w3.org/2001/KMLSchoma" > - Techrical Publications” - An up tvust lr Knowledge Jaternet Progecammniny Hed? eu YM 2, Date data typo. Hor specifying the date we use date data type. The format of this dite is yyyy-nmedd where cries the day, While specitying the date yyyy denotes the year, mm denotes the ruontty and i 9 ele digit then AC ainat be with fending zero, All the entries (ie. yyyy.rim and dd) iF any entry is 9 must be present Here isn itustration XML Schoma [datoTypo.dtd) 2009-09-01 We can also specify the time by specifying the time as follows ~ XML Document 3. Numeric If we want to use numeric value for some element then we can use the data types as either decimal or integer. XML Schema [decimal Type.xsd } <‘taml version ="1.0° encoding ="UTI “Surechema xanlne:xs ="hutp://www.w3.o1g/2001/XMLSchoma" XML Document [decimal Type Demo.xml] <2aml version ="1,0" encoding ="UTP-B?> ="xs:integer"/> 4. Boolean For specifying the true or false values we must use the boolean data type XML Schema [booleantype.xsd] EGE] Advantages and Disadvantages of Schema Advantages of schema 1. The schemas are more specitic. The schema provide the support for data types. The schema is aware of namespac es. 4. The XML schema is written in XML itself and has a large number of built in and derived types. 5. The XML schema is the W3C recommendation. Hence it is supported by various XML validator and XML processors. Disadvantages of schema 1. The XML schema is complex to design and hard to learn. 2. The XML document cannot be if the corresponding, schema file is absent, 3. Maintaining the schema for large and complex operations sometimes slows down the processing of XML document WES a Tecteieal Publications” - An up trust for knowledge Fe eens saw sewn 4 Pragriemmine Ativantages of aohema aver DTO 1. Both the schemas and DTDs are usetal for defining structural components of NME But the TDS are basic and cumnot be much specific Ror comptes operations, On the other hand schemas are more specific: 2. The schemas provide support for defining the type ot data. The DTDs do not have this ability, Neace content definition ts possible using scheme 3. The schemas are namespace aware and DTDs are not 4. The XML schema is written in XML jtsell’ and has a forge number of built in and derived types fon. Hence it is supported: by various: XML The schema is the W3C recom Validator and XML processors but there are some XML processors which do not support pip. an be built using NML schema, On the other hand 6. Large number of web application relatively simple and compact operations can be built using DTD. 1. Explain the role of NML schenta ir building web services in « eet 2. Briefly discuss hors data types are represented in XML [Ed DoM and Presenting XML AUS May-i0, Marks &, Dec.-18, Marks 13 What is DOM ? neutral Document Object Model (DOM) Application Programming Interface (API) which describes how to ac set of platform independent and langu and manipulate the anformation stored in XML or in HTML documents, Thus XML DOM is for + Loading the XML document, «Accessing the elements of NML document + Deleting the elements of NML document. + Changing the elements of) AL document aa ee ae ta Tectincal Pubicetions «An up (o> st tnowtaige Internet Programming 8-20 XML 1, Loading an XML file We will write a simple script by which any desired XML file can be loaded. Note that these scripts are dependant upon the browser in which these are getting opened. We will write the following script which can be opened in any browser like internet explorer or Mozilla Firefox. Step 1: Open some simple text editor like Notepad and type the following script. I have named it as first.hitml IB ACR eearpienuetned - Wendows Ivo apare’ TWong Ome So. 1B OOM marepteinatniet > #7) Elkie -Ren & aD me Phowe 1221567690 Sebject Mathers Make" 100 Script Explanation In above given script, we have specified an external file my_funetion_file,js in the head section. This is a file in which the function My Function is defined. This function performs the task of loading an XML document student.xml We will first obtain a document object using the statement, sanlDocument=My_Function("student.xm1"); Note that xmlDocument is a document object referring the document student.xml, Using this ‘object we can use the properties and methods. The method getElementsByTagName is used and to this method the parameter will be a tag names such as “Roll_No” ‘Name” “class” and so on. Cs Technical Publications” - An up theust for knowledge Internet Programming 8-26 XML. xamlDocument.getElementsByTagName("Roll_No*){0].childNodes(0|.nodeValue); t t This method is used for Tag name The first The value of accessing the element by from child of the node Roll_No specifying the tag name student.xm (icc. text) Hence as an output we can see the value of every element. That means the contents of student.xml file will be displayed. Wess ees iil 1. Explain about DOM with the XML data processing. XML Parsers and Validation i Poon The primary goal of any XML processor is to parse the given XML document. Java has a rich source of in-built APIs for parsing the given XML document. It is parsed in two ways - fen * Parsing using DOM(tree based) + Parsing using SAX(event based) Difference between DOM and SAX Sr. No. DOM SAX 1 DOM is a tree based parsing method SAX is an event based parsing method | used (o parse the given XML document. used to parse the given XML | document. os In this method, the entire XML In this method, the parsing is done by document is stored in the memory generating the sequence of events or it : before actual processing, Hence it calls handler functions. requires more mi ory. The DOM approach is useful for Although SAX development is much smaller applications because it is more challenging, it is useful for simpler to use but it is certainly not parsing the large XML document used for larger XML documents because the approach is event based, because it will then require larger xml gets parsed node by node and does : amount of memory. not require large amount of memory. We can insert or delete a node. We can insert or delete a node. “Traversing is done in any direction in. ‘Top to bottom traversing is done in this DOM approach. 4 approach, eS An up thst for krawledge Internet Programming Features of XML Parsers 1. XML parser is a Jibrary that provides methods (or interfaces) for cli { applications to work with XML documents. 2. It validates the document. 3. Itcchecks for well formedness. 4, DOM is a tree based parsing method. SAX is event based parsing method. 6. In DOM approach the entire XML document is stored in the memory before actual processing, In SAX the sequence of events or handler functions are called for parsing the XML document, 8. In DOM, the XML document is traversed in any direction and in SAX the XML document is traversed from top to down. DOM based XML Processing Using DOM API the XML document can be processed. The DOM API is defined in the jom.*. Using this API a DOM object tree is created from the input XML package or.w3 document and this tree helps in parsing the XML document. Ex. 8.8.1 : Write XML document for checking Well Formedness of XML document using DOM APL. Sol. : Java Program [parsing_DOMDemo,java] import java.io.*; import javax.xml_parsers.*; import org.w3c.dom.*; import org.xml.sax.*; public class Parsing DOMDemo { static public void main(String[] arg) { uy iu System out.print("Enter the name of XML document "); BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); String file_neme = input readLine(); File fp = new Filo(Gile_name): —— Tecteical Publications up theust for knowiedoe FE XML Internet Programming iflfp.exists()) { uy { DocumentBuilderFactory Factory_obj = DocumentBuilderFactory.newlnstance(); DocumentBuilder builder = Factory_obj.newDocumentBuilder(); @) InputSource ip_sre = new InputSource(file_name); Document doc = builder.parse(ip_src); System.outprintin(file name + " is well-formed!"); } catch (Exception e) { System.outprintin(file_name +" isn't wellformed!"); System.exit(1); } } else { System.out.print(‘File not found!"); } } catch(IOException ex) { ex.printStackTrace(); } y Program Explanation |. In above Java program we have to import some useful packages initially. Those are described as follows - ‘The java.io package provides the interface for perforin, ig simple input and output operations. + The javax.xmbparsers.* Package provides the classes allowing the processing of KML documents. It supports __yarious classes such as DocumentBuilder and DocumentBuilderFactory. * The pacakge org.w3e.dom.* Provides the interfaces for Document Object Model which is a component API of JAVA API for XML processing, * The package orgaml.sax." provides the classes a ind interfaces for the Simple API for XML (SAX) which is a component API of JAVA API. Internet Programming aap arset. This parser 3. DocumentBuilderFactory isa fictory APL an application can obtain basivally produces DOM object tree from given XML document. Using the object of DocumentBuilderFactory an instance for DocumentBuilder is created. The object of DocumentBuilder is used to invoke a method parse, This method takes as XML document fas an input, parses it. I'the XML document is well formed then appropriate message will be displayed. In an XML document if every starting tag has an ending tag then that docurment 1s said to be well formed otherwise itis not. Inthe try block we are calling the method parse for parsing the XML document. the XML document is not well formed then the control of the program will go to eateh block We will consider following XML document for executing above program - studentt.xml 10 Parth
Pune
1234567890
Second «Subject>Mathomatics 100 Purposely made this statement like this! (I is not well formed) 20 Anuradha
Banglore
90901233
Fifth English 90 30 Anand
Mumbai «90901256 Fifth English 90 1 Techical Pubications «Anup trust fr knowledge 1 XML 2 cor shown in the For getting the output go to the command prompt and give the commands a following screenshot. Output WW OWecbwsutenddimten a ea oer) 3 aia erat erate a) nye Naturally if we correct the statement like this, 100 Then we will get the output as follows ~ D,SAX_examples>javac Parsing DOMDemo java DASAX_examples>java Parsing DOMDemo Enter the name of XML document student xan! student1 zon! is well-formed! DASAX_examples> Event-Oriented Parsing : SAX ie Now we will discuss simple Java programs that can be built to parse the given XML document using the event based API ie, SAX Ex, 8.8.2 : Write XML document for checking Well Formedness of XML document using SAX. APL. Sol. : Java Program [Parsing_SAXDemo java] import java.io.*; import org.xml.sax.*; import org.anl.sax.helpors.*; CG) Public class Parsing SAXDemo Technica Pubic atons An up test tr knontodgo Internet Programming 8-3 XML { public static void main(String|] args) throws IOException t uy < System.out.print("Enter the namo of KML document"), > BufferedReader input = new BulloredReader{new i InputStreamReader(System.in)); r String file_namo = input readLine(), | File fp = new File(file_name); = if (fp exists()) { uy { XMLReader reader = XMLReadorF actory.createXMLReader(); reader parso(file_name); ‘System out printin(file_name +" is well-formed.’); catch (Exception e) { System outprintine_name + *is not well-formed’) @) System.exit(1); + else { + + catch (IOException ex){ex printStackTrace();} } System.out printin(‘File is not present: " + file name), } Program Explanation (The numbered steps are explained as follows) 1. In above Java program we have to import some usefill packages initially. Those are described as follows - The java.io package provides the interface for performing s mple input and output operations. The package org.xml.sax.* provides the classes and interfaces for the Simple API tor XML (SAX) which is a component API of JAVA API. The package org.xml.sax.helpers.* provides the helper classes for Simple API for XML(ie. SAX) which is a component of JAVA API for processing XML. 2. Reading the name of XML document using the command prompt, Using the input stream for the BufferReader class we can read the contents from the command prompt 3. The XMLReaderFactory helps in creating an XML. reader. This reader then parses xml document using the appropriate callbacks, Using ereateXMLReader an object reader is created using which we can call the method parse for parsing the XML document, Technical Publications" - An up teust for knowledge Internet Programming Hiz32 XML 4. In the try block we are calling the method parser for parsing the XML document. If the XML document is not well formed then the control of the program will go to cateh block A sample XML document that can be parsed using above program is as given below - student.xml 10 Parth
Pune
1234567890
Second Mathematics 100
(ieeacrrens 1. Explain XML parsers and validation. (area emcee) 2. List the essential features of XML parsers. epee Discuss the merits and demerits of DOM and SAX parsers with neat examples. : x PSV dnternet Programmine. BEX) xsi and XSLT Transformation * The XSLT stands for XSL Transformations and XSL stands for eXtensible Stylesheet Language. * An XSLT ts a W3C recommendation. * The XSLT is used for defining the XML document transformations and presentations. Hence XSL decides how an XML document should look on the web browser. + XSL consists of three parts : 1. XSLT is a language for transforming XML documents. 2. XPath is a language for navigating in XML documents. In other words we can reach to any node of XML document using XPath, 3. XSL-FO is a language for formatting XML documents for displaying it in desired manner. [BERD Transforming XML into XSLT + Using XSLT we can decide the way by which we want the clement to get displayed. + XSLT uses XPath to find information in an XML document. XPath is used to navigate through elements and attributes in XML documents. We can sort these elements, hide or display particular element and can apply some decision making logic on these elements. + XSLT processors take two input documents one is XML document and another is XSLT document. The XSLT document is nothing but a program and XML document is nothing but the input data, thus this program works on the XML input data. Then some or whole part of the XML document is selected, modified and merged with XSLT program document in order to produce another document. ‘+ This newly produced document is provided as input to the XSLT processor which in tim produce another document called the XSL document. + The XSL document is used along with the application so that particular application can be displayed on the web browser in some desired manner. XML document }— XSLT document 8.9.1 Processing of XSLT document - Technical Publications”. An up thrust for knowlege Internet Programming 8-34 XML * The XSLT document makes use of templates using which particular code can be described in XML document. » XML document and then particular code in XSLT is executed when the match is found. + XSLT processor processes the XML document sequentially by reading cach line one by one. * The XSLT model can be described as template driven or data driven model. XSL Elements + The is for building the templates. + The match attribute is associated with the template element. The match attribute can also be used to define a template for the entire XML document, The match="/" defines the whole document, + In the following example we store the XML elements in the tabular form using the and Step 1: Write a simple XML document. One sample example is as below - Open Notepad and write the following code. Save it by the name SimpleXml.xml XML Document[SimpleXml.xml] Anand
Pune
Second 70 percent
Anuradha
Chennai
Second ‘90 percent
Archana
Mumbai
Forth 80 percent
We have defined this file in step 2 "ir Technical Publications” - An up trust for knowledge XML Internet Programming Monika
Delhi
Tenth 77 percent
‘Step 2: Now create a XSL stylesheet. Note that this document must have file name extension as .xsl. Open notepad and save following code in it. XSLT Document [SimpleXmI.xs!]

Students Database

“gray"> Name Address Standard Marks ** ** "* ** a * Technical Publications” - An up thrust for knowledge Internet Programmin 4-36 ——__*M. Step 3 : Open the web browser and the output will be as follows (Cains ee Be [88 Yew Hat Boras Tolle fae Splint [+ ca 8 tesassmplain | Students Database = 2. The ‘The value-of is used to extract the value of xml element and add it to output stream of the XSL transformation. Following example illustrates this Step 1: Keep the same xml[SimpleXmLxml] file which you have created in section 8.9.2 (a) Step 1. Step 2: Create the xsl file as follows -

Students Database

itudent/Person-Details/std'/> itudent/Person-Details/marks"/> + Technical Pubicauons”” - An up trust for knowledge Laternet Programming 8-37 AME */xsttomplate> */aak:styloshoot> Step 3: Open the web browser and the output will be as follows - ee fat Yor toy ests J nesnasergttiant € 8 teinassmqiin > c co Students Database Fld ae aad Sie] } ete een oper 3. The The for-each clement allows to traverse through each element of the node Step 1: Keep the same xml[SimpleXml.xml] file which you have created in section 8.9.2(1) Step 1. Step 2: Now create .xsl document illustrating . It is as follows -

Students Database

Name Address Standard Marks
<
Name Address Standard Marks

Students Database

> Internet Programming 8-39 ‘ i See ee See a seem pg onminn @ catntatens gine ary eet Students Database ¥ Technical Publications" - An up thrust for knowledge Internet Programming -40
Name Address Standard Marks *pink">
6. The The element is used along with and to multiple conditional tests. XML express Step 1: Keep the same xml[SimpleXml.xml] file which you have created in section 8.9.2 (1) Step 1. Step 2: Create the xsl file as follows — <®anl version="1.0" encoding="IS0-8859-1"7>

Students Database

Name Marks
Step 2: Create a CSS file and name it as library.css Catalog il font-family:arial; color:red; font-size:16pt iy Book { display:block; font-family:times new roman; color:blue; font-size:14pt } Title { font-family-arial; color:maroon; font-size:12pt } Author { font-family:arial; color:magenta; 17 - Technical Publications - An up thrust for knowledge deems Prvgeemetons Sea Ratitlon,SAN,Price { aigplay block, font-famnitysarial; color:black; font-size:10pt margin-loft: 20pt } Step 3: Finally create a XML document and name it as CSSDemo.css as follows - XML Document XML Bible Winston Wiely Fifth Edition 0-7645-4760-7 $40.5 Artificial Intelligence S.Russel Princeton hall Sixth Edition 0-13-1038-5-2 $63 Java 2 Watson BPB Publications Third Edition 0-41-1058-7-2 $63 An, Here both CSS and DTD. files are used externally HTML in 24 hours Sam PeterSAM Publications Fifth Edition 0-672-32841-0 a Techical Publican’ «an up trust for knowledge Internet Hernet Programming XML. $50 Step 4: Finally execute the XML document on the web browser and you can see following sort of output - Cree le Et Mew Hetery Gookrhs Tooth te QB Se 1 tease eters “(GF , as wo ted @ cng ated hte des) Girton ke een |} wow Fith Eaton 07646-47607 05 Artifical inteligence S.Russel Princeton hall Sixth Edition 0-13-103852 $63 |Java 2 Watson {BPE Publications Thied Edtion 0-41-1058-7-2 33 FTML in 24 hours Sam Peter ‘SAM Publications Fith Edition oa723041.0 50 Doe [ERIM News Feed (RSS and ATOM) News feed or Web feed is a kind of document(or some data format) which is provit jing updated contents to its users. The news feed lets web sites continuously feed you the updated news. in Cricket. You could visit the websites that show information on information about the players and so on. Depending on | websites to get the information you want. By Suppose you are interested i Cricket, Cricket match schedule, Score, your interests you may have to visit several subscribing to a News feed or web feed, you can conveniently get a feed on the topic “ericker” without having to go to particular website as well as catch up on all the history sites you are interested in without visiting each site. How it works ? «There are content providers or distributors. «Several web feeds are collected together at one spot. This s «The end users can register with an aggregator program running on as dragging the link from the web browser to the aggregator. They publish feed links on their sites. pot is called aggregator. their own machines. This is just similar to the task ar * Technical Publications" - An up thrust for knowledge Internet Programming 8-44 XML + When instructed, the aggregator asks all the servers in its feed list if they have new content; if 0, the aggregator either makes a note of the new content or downloads it. + Aggregators can be scheduled to check for new content periodically. + Web feeds are an example of pull technology although they may appear to push content to the user. + The kinds of content delivered by a web feed are typically HTML (webpage content) or links to webpages and other kinds of digital media, + These Web sites are operated by many news websites and web blogs. + Thus news feeds work like newsletters by delivering the latest news to your desktop. EREEI Rss + RSS is a kind of web feed. It stands for o Really Simple Syndication © Rich Site Summary © Real time Simple Summary * RSS is used to publish frequently updated information. For instance - blog entires, news headlines, videos all these can be RSS feeds. * The RSS format can be specified using XML. A standardized XML format allows the information to be published once. Then it is viewed by many different programs. How it works 7 + RSS headlines are simple text files. + The web master submits these files to a special feed server. + This RSS feed server, in turn, pushes the text file to the screens of its subscribers. + Time span is usually 30 seconds to 30 minutes before the subscribers see the updates, In most cases, the lag is not even noticeable. + Most RSS readers are free to use and easy to lear, User can setup the screen using the reader tool. The users subscribe to a feed by using the feed’s URL or by clicking the RSS icon. Thus the subscription process is initiated. * Various examples of RSS reader are - Newsgator.com, Google Reader, My Yahoo Reader. Example of RSS element + The most commonly used elements in RSS are ,, and . + The element describes the RSS feed. This element has three child elements, + : This element is for defining the title of channel element. a Technical Publications” - An up teust for knowledge Internet Programming ; has XML * slink> : This clement defines the hyperlink to channel. ‘Sdescription> : This clement describes the channel, * Each <channel> clement has one or more <item> elements. The item element defines the article or story in RSS field. * Following is a simple XML document which makes used of RSS<item> to describe the book store collection. ‘“<?anl version="1.0" encoding="IS0-8859-1" 7> ‘<tss version="2,0"°> <channel> <title>Books Store http://www_myreadings.com ‘This book store has a huge collection of books ‘title> Engineering Books http://www.myreadings.com/engineering Collection of books on Engineering Subjects Advantages of using web feeds 1. User need not have to specify their email addresses while subscribing the web feed. Hence there are no unlike email threats such as spam or phishing. 2. User need not have to send the unsubscribe information. He simply removes the feed from aggregator in order to stop getting news from the feed. 3. The feeds are obtained in sorted manner. ATOM « Atom is an XML - based file format used to syndicate content. «Atom was originally developed as an alternative to RSS 2.0, and is designed for improving the shortcomings of the RSS format. ‘Atom is much more robust and feature rich than RSS. Web feeds are used by the blogging community to share recent entries’ headlines even attached multimedia files. Users visiting a web site with an atom feed can discover a file described as “atom.xml" in the full text, and URL that can be copied and pasted into an aggregator to subscribe to the feed. In particular, many blog and wiki sites offer their web feeds in the atom format. Tr Technical Publications - An up thrust for knowledge Internet Programming 8-46 Difference between RSS and ATOM RSS contain cither a plain text or escaped HTML as a payload RSS shows the date timestamp of data when the feed was created and last updated. RSS vocabulary elements are unusual in other XML vocabularies. RSS makes use of two publishing : protocols - Blogger protocol and - MetaWeblog. RSS adopts loose approach about data. RSS has more complicated aggregating and extracting process. RSS is not a standardized feature, The browser is left to figure out whether the RSS feed contains plain text or escaped HTML, Ue crc | | | Atom contains variety of payloads such as escaped HTML, XML, DHTML, references to extemal contents stich as documents, audio or video streams, and soon. Atom shows the date timestamp when website was last updated. a | Atom allows the reuse of elements outside the atom feed document. ‘Atom has only one standardized i protocol. Atom uses restrictive approach about data, ‘Atom has easier aggregating and extracting process. Atom is a standardized feature. | aaa. Atom feeds explicitly indicates the | content 1. Discuss the following in detail. i) RSS, ii) ATOM BE ICMC Ur Cue nea ite ‘May=19, 7+6iMark: Q.1 What is XML ? or to store the data. Q2 What is the difference between XML and HTML ? T Technical Pubiications™ Ans: The XML stands for eXtensible Markup Language, The XML. is designed to to" An up thrust far knowtedgo Internet Programming ie 8-97 ne AML ‘Ans. : The difference between HTML and XML is that HTML is designed for representation of data on the web page whereas XML is designed transport or to store the data, ‘The HTML has its predefined tags whereas the XML does not have predefined tags. Q.3 What are goals of XML? Ans. : Following are the goals of XML - 1. User must be able to define and use his own tag. This allows us to restrict the use of the set ‘of tags defined by proprietary vendors. 2, Allow user to build his own tag library based on his web requirement. 3. Allow user to define the formatting rules for the user defined tags. 4, XML must support for storage or transport of data Q.4 Explain the following terms related to XML: i) XML parsing ii) XML browsers iii) XML editors iv) XML validators Ans. i) XML parsing : This is a technique in which the X! memory and converts into XML DOM object. ii) XML browser : The XML browser visualizes the elements in XML document. The user fe external interfaces with the XML elements. For ML parser reads the XML file into the can browse through or can associat examples the XML file can be viewed using Internet Explorer or Firefox browser. iii) XML editors : XML editors allow the developer to write error free XML document by editing it. The plain text editor like notepad can also be used for editing the XML document. But there are some graphical XML editors like XML Spy or oXygen that present the content to the user in a more user-friendly format. iv) XML validators : The validation of XML document is done by XML parser. It can be done using two approaches. 3) Checking the well formedness of XML document - That means if any bracket is missing or additional is tested by checking the well formedness of the XML document. ii) Checking the validity of XML elements. Q5 Discuss the important features of XML which makes it more suitable for creating web related services. st suitable for creating web related ‘Ans. : Following are some features which are mo applications. |. XMLis EXtesible Markup Language intended for transport or storage of the data. “a7 ~Fechnical Publications _- An up thrust fer knowledge ramming << — XML is that user is able to define and © his own ~ ature 0 of the set of tags defined by proprietary vendors, Oy y. | { | | pene mene? 2, The most important g to restrict the Use rand other markup languages like ITTMIL- or SG ' (a Sere | XML contains only dats and does not c tv play the data. create the tags for any type of information. Due y 4. | - Due to thi, | ontain any formatting information. Hen, . Hence dey, Ment developers can decide how to dis XML permits the document writer (0. virtually any kind of information can be such as mathematical formulae. chemica | ‘ome other kind of data can be described using XML. = structures, oF S¢ 5, Searching sorting, rendering or manipulating the XML document is possible using ten | Stylesheet Language (XSL). | 6. The XML document can be validated using some external tools. 7. Some commonly used web browsers like Intemet Explorer and Firefox Mozilla provice suppor tothe tgs in XML. Henee XML is not at all vendor specific or browser specie, | Q.6 What are the advantages of XML ? } zy Ans. : | 1. XML document is human readable and we can edit any XML document in simple text editors. 2. The XML document is language neutral. That means a Java program can generate an XML document and this document can be parsed by Perl. 3. Every XML document has a tree structure. Hence complex data can be arranged systematically and can be understood in simple manner. 4. XML files are independent of an operating system. 7 What is XML element ? Ans. : The basic entity in XML is element. The elements are used to define tags. The elements typically consist of opening and closing tag. Mostly only one element is used 19 define a single tag. The syntax of writing any element for opening tag is The syntax of writing any closing element for closing tag is Every XML must have a single root element. 8-49 XML programming ~ Innere go way DTDs? ans. : The document type definition is used to define the basic building block of any xml document. Using DTD we can specify the various elements types, attributes and their relationship with one another. Basically DTD is used to specify the set of rules for structuring data in any XML file. For example : If we want to put some information about some students in XML file then, generally we use tag student followed by his/her name, address, standard and marks. That means we are actually specifying the manner by which the information should be arranged in the XML file. And for this purpose the Document Type Definition is used. Q9 What is meant by namespace ? Ans. : The xml document allows us to create different elements which are having the common name. This technique is known as namespace. For example : Consider the following XML document XML Document It is a text file It is an image file The above document does not produce any error although the element text is used for two different attribute values. Q.10 What is CDATA ? Ans. : The CDATA stands for Character Data. The character data will be parsed by the parser. Q.11 What is PCDATA ? Ans. : It stands for Parsed Character Data (i.e. text). Any Parsable character data should not contain the markup characters. The markup characters are < or > or &. If we want to use less than, greater than or ampersand characters then make use of < , > or & Q.12 What are the merits of DTD ? Ans. : 1, DTDs are used to define the structural components of XML document 2. These are relatively simple and compact. 3. DTDs can be defined inline and hence can be embedded directly in the XML document. a Technical Publications” - An up thrust for knowledge XML Internet Programming 8-50 x Q.13 What are the demerits of DTD ? Ans. : 13 3 The DTDs are very basic and hence cannot be much specific for complex documents. . The language that DTD uses is not an XML document. Hence various frameworks used by XML cannot be supported by the DTDs. The DTD cannot define the type of data contained within the XML document. Hence using DTD we cannot specify whether the element is numeric, or string or of date type. 4. 5 There are some XML processor which do not understand DTDs. The DTDs are not aware of namespace concept. Q.14 What is XSL and why it is used ? Ans. : XSL stands for eXtensible Stylesheet Language. The XSL decided how an XML document should look on web browser. Q.15 What is the difference between DOM and SAX ? [Refer section 8.8.1] (VERE) Q.16 What are the two methods of parsing the XML document ? Ans. : The XML document is parsed using two approaches - 1) Tree based 2) Event based. ‘The tree based parsing is done using DOM and th Event based parsing is done using SAX. Q.17 What is the importance of SAX ? Ans. he SAX stands for Simple API for XML. It allows to access the information of XML document using sequence of events. This API is basically used for parsing the XML document. Q.18 How is XML parsing done with SAX ? Ans. : The XML parsing can be done with SAX using following steps - A Java program can make use of following steps. 1) Using the BufferedReader class the contents of XML document are read, 7) Then using XMLReadFactory XML reader is created. This reader parses the XML document using appropriate callbacks, 3) Using ereateXMLReader an object reader is created, 4) With the help of this object the method parse is invoked for parsing the XML document. Q.19 What does XSLT mean ? (ESE Ans. : The XSLT stands for eXtensible Stylesheet Language Transformation. It is a W3C recommendation, ap! mie 7 Internet Programming 2 Q.20 What is the purpose of XSLT aaa Ans. : The purpose of XSLT is to define the XML document transformations and presentations. Thus using XSLT the XML document can be transformed into XHTML documents or some other XML document. Q.21 What is Xpath ? TE Ans. : The XSLT uses the Xpath to navigate in XML document. In other words we can reach to any node of XML document using XPath. Q.22 What is XSL-FO ? Ans. : The XSL-FO is a language for formatting XML document for displaying it in desired manner. Q.23 What is the use of CSS for XML document ? Ans. : In XML user defined tags are used. The cascading style sheet can be embedded within the XML document using which the desired style can be applied to the user defined tags. Thus the XML document can be presented in the desired style using the CSS. Q.24 What is XML Parse Tree ? Ans. : The XML document form a tree structure that starts from root node to branches. The XML parse tree is a DOM model in which everything from XML is treated as node. This tree is created for parsing the XML document. Various properties such as nodeName, nodeValue, parentNode, childNodes and so on are used while parsing the XML document. Q.25 Why is XSLT an important tool in development of web applications ? Ans. : The XSLT stands for XSL Transformations. It is used for defining the XSL document transformations and presentations. Thus XSLT decides how an XML document should look on web browser. Q.26 What is XML parser ? LURDE XML parser is a parser that is designed to read XML and check the well formedness of Ans. the document, The XML parser also validates the document. Q.27 What is Well formed XML document ? Ans. : Refer section 8.3. Internet Programming & -52 XML Q.28 What are the different data types used in XML Schema ? Ans. : DataTypes | Description String The string is a collection of characters. This data type is used to define the elements containing characters, lines, tabs or white spaces. It is denoted by type=xs:string Date For specifying the date we use this data type. The format is yyyy-mm- dd where y denotes year, m denotes month and d denotes the day. It is specified using type=xs:date Numeric For specifying the numeric values this data type is used. This can be denoted as cither type=xs:decimal or type=xs:integer Boolean For specifying the true or false values the Boolean data type is used. It is denoted as type=xs:boolean Q.29 Compare DTD and Schema. Ans. : 1. Both the schemas and DTDs are useful for defining structural components of XML. But the DTDs are basic and cannot be much specific for complex operations. On the other hand schemas are more specific. 2. The schemas provide support for defining the type of data. The DTDs do not have this ability. Hence content definition is possible using schema. 3. The schemas are namespace aware and DTDs are not. 4, The XML schema is written in XML itself and has a large number of built in and derived types. 5. The schema is the W3C recommendation. Hence it is supported by various XML validator and XML processors but there are some XML processors which do not support DTD. 6. Large number of web applications can be built using XML schema. On the other hand relatively simple and compact operations can be built using DTD. Q.30 What is the purpose of XML schema ? CUES Ans. : The XML schemas are used to represent the structure of XML document. The purpose of XML schema is to define the building blocks of an XML document, Q.31 Explain the term XML schema. Ans. : The XML schema are used to represent the structure of XML document, The goal of XML schema is to define building blocks of an XML document. =m Technical Publications» - An up thrust for knawdedlge:

You might also like