1.
Example for XML External DTD
DTD for sample.xml file, employ.dtd
<!ELEMENT employee (empno,name,salary)>
<!ELEMENT empno (#PCDATA)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT salary (#PCDATA)>
Sample.xml
<!DOCTYPE employee SYSTEM "employ.dtd">
<employee>
<empno>102</empno>
<name>MECS</name>
<salary>20000</salary>
</employee>
Note: <employee> is the root element. In employ.dtd file,(#PCDATA) used to specify the content inside the
element.root element has 3 child elements .
OUTPUT:
2.Example for XML Internal DTD
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE students [<!ELEMENT students (student+)>
<!ELEMENT student EMPTY>
<!ATTLIST student sid ID #REQUIRED>
<!ATTLIST student sname CDATA #REQUIRED> ]>
<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:
3.Example for Enumerated datatype:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE payments [<!ELEMENT payments (payment+)>
<!ELEMENT payment EMPTY>
<!ATTLIST payment payment-type (DD|CHECK|CASH #REQUIRED)>
]>
<payments>
<payment payment-type="DD"/>
<payment payment-type="CASH"/>
</payments>
Things to remember: Cardinality operators:
In a xml document,one element can occur o times or one time or n number of times
To specify how many no.of times ,an element can occur in a xml document
Can use cardinality
In DTD- *,+,?
* -> INDICATES 0 to n
+ -> indicates 1 to n
? -> 0 or 1
4.Example for external DTD
XML DTD for Bookdemo.xml file below,
bk.dtd
<!ELEMENT books (book*)>
<!ELEMENT book (book-name|title,price,author+,publication?)>
<!ELEMENT book-name (#PCDATA)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT publication (#PCDATA)>
Note:book* indicates ,xml file can contain zero or more occurrences of that element, here i.e book element
Bookdemo.xml
<!DOCTYPE books SYSTEM "bk.dtd">
<books>
<book>
<book-name>corejava</book-name>
<price>500</price>
<author>jamesgoswling</author>
<publication>NIT</publication>
</book>
</books>
OUTPUT:
5.Example for Internal DTD.
<?xml version="1.0" encoding="iso-8859-1" standalone="no" ?>
<!DOCTYPE employees
[<!ELEMENT employees (employee+)>
<!ELEMENT employee (name+,record+)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT record
(id?,hire_date+,department?,title?,sale+ ) >
<!ELEMENT id (#PCDATA)>
<!ELEMENT hire_date (#PCDATA)>
<!ELEMENT department (#PCDATA)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT sale (date+,amount+)>
<!ELEMENT date (#PCDATA)>
<!ELEMENT amount (#PCDATA)>
<!ATTLIST amount currency (Rs | Doller) #REQUIRED>
]>
<employees>
<employee>
<name>Tony</name>
<record>
<id>1234</id>
<hire_date>4-1-2001</hire_date>
<department>Shipping</department>
<title>Packer</title>
<sale>
<date>4-1-2002</date>
<amount currency="Rs">19000.00</amount>
</sale>
<sale>
<date>5-1-2002</date>
<amount currency="Rs">12500.00</amount>
</sale>
</record>
</employee>
<employee>
<name>Ed</name>
<record>
<id>1235</id>
<hire_date>4-1-2001</hire_date>
<department>Programming</department>
<title>Programmer</title>
<sale>
<date>4-1-2002</date>
<amount currency="Rs">5900.00</amount>
</sale>
</record>
</employee>
</employees>
OUTPUT:
6.Example for Entity declaration in DTD:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note [
<!ENTITY nbsp " ">
<!ENTITY writer "Writer: Donald Duck.">
<!ENTITY copyright "Copyright: W3Schools.">
]>
<note>
<footer>&writer; ©right;</footer>
</note>
OUTPUT:
7.Example for XMLNamespace.
Html table information xml table consisting of furniture information
<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.
<root xmlns:h="http://www.w3.org/TR/html4/" xmlns:f="furniture:table">
<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:
10.XML using CSS
<?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:-
12. Insert Table
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:-
13. using Prepared Statement
import java.sql.*;
public class PreparedInsert{
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
{
PreparedStatement pstmt=conn.prepareStatement("insert into sdetails values(?,?,?,?)");
pstmt.setString(1,'jyothir adithya');
pstmt.setInt(2,8096);
pstmt.setString(3,'CSE-2');
pstmt.setString(4,'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:-
14. Retrieve Data
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:-