UNIT-3_XML_Examples_dtd_xsd_xslt_jdbc (2)
UNIT-3_XML_Examples_dtd_xsd_xslt_jdbc (2)
Sample.xml
OUTPUT:
<students>
<student sid="NIT101" sname="ajay"/>
<student sid="NIT102" sname="vijay"/>
</students>
Note:<students> is not having any children,so the category is EMPTY type.
Explanation: student+ means ,it may contain one or more student elements.’ID’ datatype accepts only unique
values start with character name .CDATA-Characterdata->used to specify the type of value to be stored in attribute
value.
OUTPUT:
<payments>
<payment payment-type="DD"/>
<payment payment-type="CASH"/>
</payments>
bk.dtd
Bookdemo.xml
</record>
</employee>
</employees>
OUTPUT:
6.Example for Entity declaration in DTD:
<note>
<footer>&writer; ©right;</footer>
</note>
OUTPUT:
7.Example for XMLNamespace.
<table> <table>
<tr> <name>African Coffee Table</name>
<td>Apples</td> <width>80</width>
<td>Bananas</td> <length>120</length>
</tr> </table>
</table>
After combining the two files,using xmlns attribute for providing prefix value.
<h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
<f:table>
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
</root>
Explanation:if there are two files having same element name like <table> element in this example,use xmlns
attribute and give prefix value, and that prefix is prefixed with each element in the output file
Output:
Referencing a Schema in an XML Document
8.
Note.xsd
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/10/XMLSchema">
<xsd:element name="note">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="to" type="xs:string"/>
<xsd:element name="from" type="xs:string"/>
<xsd:element name="heading" type="xs:string"/>
<xsd:element name="body" type="xs:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Demo.xml
<?xml version="1.0"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="C:\note.xsd">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</root>
Output:
9. Retrieval of information in XML document using XSLT Stylesheet
Xml document
Banks.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="bank.xsl"?>
<bank>
<account>
<acc_no>A-101</acc_no>
<branch_name>DOWN TOWN</branch_name>
<balance>500</balance>
</account>
<account>
<acc_no>A-102</acc_no>
<branch_name>Perry ridge</branch_name>
<balance>500</balance>
</account>
<account>
<acc_no>A-103</acc_no>
<branch_name>DOWN TOWN</branch_name>
<balance>1000</balance>
</account>
</bank>
bank.xsl
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl=http://www.w3.org/1999/XSL/Transform version="1.0">
<xsl:template match="/">
<html><body bgcolor="lightseagreen">
<h2>BANK</h2>
<table border="1">
<xsl:for-each select="bank/account">
<tr>
<td><xsl:value-of select="acc_no"/></td>
<td><xsl:value-of select="branch_name"/></td>
<td><xsl:value-of select="balance"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Note: <stylesheet> is the root element.<template> is used to provide match attribute for parent element.
Output:
<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="style.css"?>
<bookstores>
<books category="CSE">
<title>Web Programming</title>
<author>Chris Bates</author>
<publisher>Willey</publisher>
<price>300</price>
</books>
<books category="CSE">
<title>Computer Networks</title>
<author>Tanenbaum</author>
<publisher>Pearson</publisher>
<price>400</price>
</books>
</bookstores>
Interacting with Database using JDBC
There are 5 steps to connect with database
Example for connecting with Oracle database.
11.Create table
import java.sql.*;
public class CreateTable{
public static void main( String args[])
{
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection
conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","manager");
Statement stmt = conn.createStatement();
stmt.executeUpdate("create table sdetails(sname varchar(30),rno int,class varchar(30),college
varchar(30))");
stmt.close();
conn.close();
}
catch(SQLException e) {
e.printStackTrace();
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
System.out.println("Table Created Successfully");
}
}
Output:-
import java.sql.*;
public class Inserttable{
public static void main( String args[])
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch(java.lang.ClassNotFoundException e)
{
System.out.print("ClassNotFoundException: "+e.getMessage());
}
try
{
Connection
conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","manager");
Statement stmt = conn.createStatement();
try
{
stmt.executeUpdate("insert into sdetails values('Veena',8100,'CSE-2','CBIT')");
}catch(SQLException e)
{
System.out.println("could not insert tuple."+e.getMessage());
}
stmt.close();
conn.close();
}catch(SQLException e)
{
e.printStackTrace();
}
System.out.println("Inserted Into Table Successfully");
}
}
Output:-
Output:-
import java.sql.*;
public class Retrievedata{
public static void main( String args[])
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch(java.lang.ClassNotFoundException e)
{
System.out.print("ClassNotFoundException: "+e.getMessage());
}
try
{
Connection
conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","manager");
Statement stmt=conn.createStatement();
try
{
ResultSet reset = stmt.executeQuery("select * from sdetails");
while(reset.next()) {
String s1 = reset.getString("sname");
int i = reset.getInt("rno");
String s2 = reset.getString("class");
String s3 = reset.getString("college");
System.out.println(s1+ "\t\t"+i + "\t\t" + s2+ "\t\t" +s3);
}
}catch(SQLException e) {
System.out.println("could not retrieve data."+e.getMessage());
}
stmt.close();
conn.close();
}catch(SQLException e) {
e.printStackTrace();
}
}
}
Output:-