XML Lab

Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

SAGARMATHA COLLEGE FOR HIGHER STUDIES

(Maitidevi, Kathmandu, Nepal)

BCA
XML AND WEB SERVICES LAB

Name : _____________________________

Roll No : _____________________________

Semester :

Academic Year :
INDEX
Exp Page
Date Name of the Experiment Signature
No No
1 XML Document Creation

Importing and Exporting XML


2
Document in Database

3 XSL Transformation

4(a) Internal DTD Creation

4(b) External DTD Creation

5 XML Schema Creation

Parsing XML Document Using


6
DOM/SAX Parser
Exp No:1 XML DOCUMENT CREATION
Date:

Aim:
To create a simple XML document to display the address book.

Procedure:
Step-1: Create the xml document using notepad.
Step-2: Create the addressbook as a root tag.
Step-3: Followed by create a three employees address in detail using different tag.
Step-4: Save the file as “Employee.xml”.
Step-5: Finally execute the program in Internet Explorer to view the output.

Source Code:
Filename: “Employee.xml”

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


<addressbook>
<emp1>
<name>Karthick</name>
<doorno>No-2/57</doorno>
<street>Vinayakam St</street>
<area>Teynampet</area>
<city>Chennai-600020</city>
<state>Tamil Nadu</state>
<country>India</country>
<phoneno>909583625</phoneno>
</emp1>
<emp2>
<name>Murali</name>
<doorno>No-25/7</doorno>
<street>ParthasarathyKoil Street</street>
<area>Triplecane</area>
<city>Chennai-600005</city>
<state>Tamil Nadu</state>
<country>India</country>
<phoneno>9962589632</phoneno>
</emp2>
<emp3>
<name>Jagagish</name>
<doorno>No-25</doorno>
<street>Palani Street,</street>
<area>Manali</area>
<city>Chennai-600055</city>
<state>Tamil Nadu</state>
<country>India</country>
<phoneno>9003862541</phoneno>
</emp3>
</addressbook>
Output:

Result:
Thus, the simple xml document creation has been executed successfully.
Exp No:2 IMPORTING AND EXPORTING XML DOCUMENT
Date: IN DATABASE

Aim:
To create a xml document and database for importing and exporting xml document
into database.

Procedure:
Step-1: Start the process.
Step-2: Create the xml document using notepad and write the following code:

Source Code:
Filename: “Hello.xml”

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


<student-data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<record>
<Name>Karthi</Name>
<Age>19</Age>
<Major>Chemistry</Major>
<GPA>8.8</GPA>
</record>
<record>
<Name>Raj</Name>
<Age>23</Age>
<Major>Maths</Major>
<GPA>5.5</GPA>
</record>
</student-data>

Step-3: Save the xml document as .xml extension.


Step-4: Click start->All programs->Microsoft office->Microsoft Excel 2007.
Step-5: Enable the Developer Tab:

If the Developer tab is not available, you need to enable it in the options. Click on File, and then go
all the way down to Options. Click on the Customize Ribbon section, and in the column on the right,
there should be a list of tabs called Main Tabs that you can check or uncheck.
Find where it says Developer, and make sure the box is checked. Then click OK. The Developer tab
should now be up top, to the right of the View tab.
Step-6: To import the xml document click->Developer tab ->Sources.
Step-7: Add an XML Map:
In the XML Source task pane, click the XML Maps option at the very bottom right. A window will
pop up.

Click Add to add a browse the location of xml document and click ok.
Step-8: Drag and drop XML elements into the spreadsheet and import the XML file into Excel.
Add the new record into the database.
Step-9: Once finished converting each column into an XML element, return to the Developer tab,
find the XML section again, and click Export. Save your XML file, and then open it in Notepad to
see the results:

Step-10: Stop the process.

Result:
Thus, the program to importing and exporting XML into Excel-database has been executed
successfully.
Exp No:3 XSL TRANSFORMATION
Date:

Aim:
To create a simple XSLT transformation from XSL to XML.

Procedure:
Step-1: Create a XSLT document using notepad.
Step-2: Using XSLT document, we can create a style sheet like Font Style, Font Size, bgcolor,..etc.
and write the following code:

Source Code:

Filename:“cdcatalog.xsl”

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


<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">
<trbgcolor="#9acd32">
<th style="text-align:left">Title</th>
<th style="text-align:left">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>

Step-3: Save the document as“ Filename.xsl”.


Step-4: Then create a XML document.
Step-5: Create a Link with the XSLT document using href tag=”Filename.xsl” and write
the following code:

Source Code:
Filename: “Simpxsl.xml”

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


<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
<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>
<cd>
<title>Still got the blues</title>
<artist>Gary Moore</artist>
<country>UK</country>
<company>Virgin records</company>
<price>10.20</price>
<year>1990</year>
</cd>
<cd>
<title>Eros</title>
<artist>Eros Ramazzotti</artist>
<country>EU</country>
<company>BMG</company>
<price>9.90</price>
<year>1997</year>
</cd>
<cd>
<title>One night only</title>
<artist>Bee Gees</artist>
<country>UK</country>
<company>Polydor</company>
<price>10.90</price>
<year>1998</year>
</cd>
<cd>
<title>Sylvias Mother</title>
<artist>Dr.Hook</artist>
<country>UK</country>
<company>CBS</company>
<price>8.10</price>
<year>1973</year>
</cd>
<cd>
<title>Maggie May</title>
<artist>Rod Stewart</artist>
<country>UK</country>
<company>Pickwick</company>
<price>8.50</price>
<year>1990</year>
</cd>
<cd>
<title>Romanza</title>
<artist>Andrea Bocelli</artist>
<country>EU</country>
<company>Polydor</company>
<price>10.80</price>
<year>1996</year>
</cd>
<cd>
<title>When a man loves a woman</title>
<artist>Percy Sledge</artist>
<country>USA</country>
<company>Atlantic</company>
<price>8.70</price>
<year>1987</year>
</cd>
<cd>
<title>Black angel</title>
<artist>Savage Rose</artist>
<country>EU</country>
<company>Mega</company>
<price>10.90</price>
<year>1995</year>
</cd>
<cd>
<title>1999 Grammy Nominees</title>
<artist>Many</artist>
<country>USA</country>
<company>Grammy</company>
<price>10.20</price>
<year>1999</year>
</cd>
<cd>
<title>For the good times</title>
<artist>Kenny Rogers</artist>
<country>UK</country>
<company>Mucik Master</company>
<price>8.70</price>
<year>1995</year>
</cd>
<cd>
<title>Big Willie style</title>
<artist>Will Smith</artist>
<country>USA</country>
<company>Columbia</company>
<price>9.90</price>
<year>1997</year>
</cd>
<cd>
<title>Tupelo Honey</title>
<artist>Van Morrison</artist>
<country>UK</country>
<company>Polydor</company>
<price>8.20</price>
<year>1971</year>
</cd>
<cd>
<title>Soulsville</title>
<artist>JornHoel</artist>
<country>Norway</country>
<company>WEA</company>
<price>7.90</price>
<year>1996</year>
</cd>
<cd>
<title>The very best of</title>
<artist>Cat Stevens</artist>
<country>UK</country>
<company>Island</company>
<price>8.90</price>
<year>1990</year>
</cd>
<cd>
<title>Stop</title>
<artist>Sam Brown</artist>
<country>UK</country>
<company>A and M</company>
<price>8.90</price>
<year>1988</year>
</cd>
<cd>
<title>Bridge of Spies</title>
<artist>T`Pau</artist>
<country>UK</country>
<company>Siren</company>
<price>7.90</price>
<year>1987</year>
</cd>
<cd>
<title>Private Dancer</title>
<artist>Tina Turner</artist>
<country>UK</country>
<company>Capitol</company>
<price>8.90</price>
<year>1983</year>
</cd>
<cd>
<title>Midtomnatten</title>
<artist>Kim Larsen</artist>
<country>EU</country>
<company>Medley</company>
<price>7.80</price>
<year>1983</year>
</cd>
<cd>
<title>Pavarotti Gala Concert</title>
<artist>Luciano Pavarotti</artist>
<country>UK</country>
<company>DECCA</company>
<price>9.90</price>
<year>1991</year>
</cd>
<cd>
<title>The dock of the bay</title>
<artist>Otis Redding</artist>
<country>USA</country>
<company>Atlantic</company>
<price>7.90</price>
<year>1987</year>
</cd>
<cd>
<title>Picture book</title>
<artist>Simply Red</artist>
<country>EU</country>
<company>Elektra</company>
<price>7.20</price>
<year>1985</year>
</cd>
<cd>
<title>Red</title>
<artist>The Communards</artist>
<country>UK</country>
<company>London</company>
<price>7.80</price>
<year>1987</year>
</cd>
<cd>
<title>Unchain my heart</title>
<artist>Joe Cocker</artist>
<country>USA</country>
<company>EMI</company>
<price>8.20</price>
<year>1987</year>
</cd>
</catalog>

Step-6: Save the document as “Simpxsl.xml”.


Step-7: Then execute the xml file and view the output:

Result:
Thus the program to convert the simple XSLT transformation from XSL to XML has
been executed successfully.
Exp No:4(i) INTERNAL DTD CREATION
Date:

Aim:
To create a XML program for Internal DTD(Document Type Definition) creation.

Procedure:
Step-1: Open the notepad.
Step-2: Create a DTD document for book details.
Step-3:Save the document as “Filename.xml”.
Step-4:Type a XML Parser using HTML coding.
Step-5:Save the document as “Filename.html”.
Step-6:Execute the program in Internet Explorer.

Source Code:

File name:“Sche.xml”

<?xml version="1.0" ?>


<!DOCTYPE books [
<!ELEMENT books (book)*>
<!ELEMENT book (title?,isbn+,(zipcode|author),publish*)>
<!ELEMENT title (#PCDATA)> <!ELEMENT isbn
(#PCDATA)>
<!ELEMENT zipcode (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT publish (#PCDATA)>
<!ATTLIST zipcode zname CDATA "232232323">
<!ATTLIST publish year CDATA #FIXED "1995">
<!ATTLIST title name CDATA #IMPLIED>
<!ATTLIST isbn number CDATA #REQUIRED>
]>
<books>
<book>
<title name="C"></title>
<isbn number="2309"></isbn>
<zipcode zname="23244345345"></zipcode>
<publish year="1998"></publish>
</book>
<book>
<title name="C++"></title>
<isbn number="2310"></isbn>
<zipcode zname="45814845585"></zipcode>
<publish year="1995"></publish>
</book>
<book>
<title name="JAVA"></title>
<isbn number="2311"></isbn>
<zipcode zname="553378845585"></zipcode>
<publish year="1995"></publish>
</book>
<book>
<title name="PUZZLES"></title>
<isbn number="2313"></isbn>
<zipcode zname="456644445"></zipcode>
<publish year="1995"></publish>
</book>
<book>
<title name="XML"></title>
<isbn number="2312"></isbn>
<zipcode zname="412684895585"></zipcode>
<publish year="1995"></publish>
</book>
</books>

Filename:“Par.html”

<html>
<body>
<h3>This demonstrates a parser error:</h3>
<script type="text/javascript">
var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.validateOnParse="true";
xmlDoc.load("Sche.xml");
document.write("<br/>Error Code:");
document.write(xmlDoc.parseError.errorCode);
document.write("<br/>Error Reason:");
document.write(xmlDoc.parseError.reason);
document.write("<br/>Error Line:");
document.write(xmlDoc.parseError.line);
</script>
</body>
</html>
Output:

Result:
Thus, the XML program for creating Internal DTD has been executed successfully.
Exp No:4(ii) EXTERNAL DTD CREATION
Date:

Aim:
To create a XML program for External DTD(Document Type Definition) creation.

Procedure:
Step-1: Open the notepad.
Step-2: Create a DTD document for book details.
Step-3: Save the document as “Filename.dtd”.
Step-4: Create a XML document for book details.
Step-5: Save the document as “Filename.xml”.
Step-6: Type a XML Parser using HTML coding.
Step-7: Save the document as “Filename.html”.
Step-8: Execute the program in Internet Explorer.

Source Code:
Filename:“not.dtd”

<!ELEMENT books (book)*>


<!ELEMENT book (title?,isbn+,(zipcode|author),publish*)>
<!ELEMENT title (#PCDATA)> <!ELEMENT isbn
(#PCDATA)>
<!ELEMENT zipcode (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT publish (#PCDATA)>
<!ATTLIST zipcode zname CDATA "232232323">
<!ATTLIST publish year CDATA #FIXED "1995">
<!ATTLIST title name CDATA #IMPLIED>
<!ATTLIST isbn number CDATA #REQUIRED>

Filename: “Sc.xml”

<?xml version="1.0" ?>


<!DOCTYPE books SYSTEM "not.dtd">
<books>
<book>
<title name="C"></title>
<isbn number="2309"></isbn>
<zipcode zname="23244345345"></zipcode>
<publish year="1995"></publish>
</book>
<book>
<title name="C++"></title>
<isbn number="2310"></isbn>
<zipcode zname="23244345345"></zipcode>
<publish year="1995"></publish>
</book>
</books>
Filename:“Pars.html”

<html>
<body>
<h3>This demonstrates a parser error:</h3>
<script type="text/javascript">
var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.validateOnParse="true";
xmlDoc.load("Sc.xml");
document.write("<br/>Error Code:");
document.write(xmlDoc.parseError.errorCode);
document.write("<br/>Error Reason:");
document.write(xmlDoc.parseError.reason);
document.write("<br/>Error Line:");
document.write(xmlDoc.parseError.line);
</script>
</body>
</html>
Output:

Result:
Thus, the XML program for creating External DTD has been executed successfully.
Exp No:5 XML SCHEMA CREATION
Date:

Aim:
To create a program for XML Schema creation and display element and attributes.

Procedure:
Step-1: Open the XMLwriter.
Step-2: Click File->New->XML Document and name the Filename.
Step-3:Type the following program:

Source Code:

Filename:“Sche.xml”

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


<shiporder orderid="889923"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="sche.xsd">
<orderperson>John Smith</orderperson>
<shipto>
<name>Karthick</name>
<address>Langgt 23</address>
<city>4000 Stavanger</city>
<country>Norway</country>
</shipto>
<item>
<title>Empire Burlesque</title>
<note>Special Edition</note>
<quantity>1</quantity>
<price>10.90</price>
</item>
<item>
<title>Hide your heart</title>
<quantity>5</quantity>
<price>9.90</price>
</item>
</shiporder>

Step-4: Click File->New->XML Schema File and name the Filename.


Step-5: Type the following code:

Source Code:
Filename:“sche.xsd”

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


<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="shiporder">
<xs:complexType>
<xs:sequence>
<xs:element name="orderperson" type="xs:string" />
<xs:element name="shipto">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string" />
<xs:element name="address" type="xs:string" />
<xs:element name="city" type="xs:string" />
<xs:element name="country" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="item" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string" />
<xs:element name="note" type="xs:string" minOccurs="0" />
<xs:element name="quantity" type="xs:positiveInteger" />
<xs:element name="price" type="xs:decimal" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="orderid" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:schema>

Step-6: Finally validate the xml file by pressing the F7 key.


Output:

Result:
Thus, the program for XML Schema creation and display element and attributes has
been executed successfully.
Exp No:6 PARSING XML DOCUMENT USING
Date: DOM/SAX PARSER

Aim:
To create a parsing XML document using DOM(Document Object Model) parser.

Procedure:
Step-1: Open the notepad.
Step-2: Create a xml document for book collection.
Step-3: Save the document as “Filename.xml”.
Step-4: Type the following program:

Source Code:

Filename:“Bookcollection.xml”

<bookdb>
<book>
<name>Java Complete Reference</name>
<author>lan</author>
<year>2001</year>
<publication>Tata McDraw Hill</publication>
</book>
<book>
<name>C# Complete Reference</name>
<author>balagurusamy</author>
<year>2006</year>
<publication>Persion Addition</publication>
</book>
<book>
<name>Complete Reference</name>
<author>Kanithkar</author>
<year>2005</year>
<publication>Tata McDraw Hill</publication>
</book>
</bookdb>

Step-5:Create a html program and save the file as “Filename.html”.


Step-6:Type the following program:

Source Code:
Filename: “Bookdetailsdom.html”

<html>
<head>
<script language="javascript">
var root;
var xmldoc=new ActiveXObject("Microsoft.XMLDOM");
xmldoc.load("Bookcollection.xml");
function start()
{
if(xmldoc.readyState=="4")
loading();
else
alert("could not load");
}
function loading()
{
var i;
root=xmldoc.documentElement;
var count;
count=root.childNodes.length;
for(i=0;i<count;i++)
{
document.write(root.childNodes.item(i).text+"<br>");
}
}
</script>
</head>
<body onLoad="start()">
</body>
</html>

Step-7: Finally, execute the html program in Internet Explorer.


Output:

Result:
Thus, the parsing XML document using DOM(Document Object Model) parser has been
executed successfully.

You might also like