Download XML and Databases

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Extensible Storage Engine wikipedia , lookup

Database wikipedia , lookup

Functional Database Model wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Relational model wikipedia , lookup

Clusterpoint wikipedia , lookup

Database model wikipedia , lookup

Transcript
XML and Databases
Introduction
• XML: Extensible Markup Language
• Defined by the WWW Consortium (W3C)
• Derived from SGML (Standard Generalized
Markup Language), but simpler to use than
SGML
• Documents have tags giving extra information
about sections of the document
– E.g. <title> XML </title> <slide> Introduction
…</slide>
• Extensible, unlike HTML
– Users can add new tags, and separately specify how
the tag should be handled for display
XML Introduction (Cont.)
• The ability to specify new tags, and to create
nested tag structures make XML a great way to
exchange data, not just documents.
– Much of the use of XML has been in data exchange applications, not as a replacement for
HTML
• Tags make data (relatively) self-documenting
– E.g.
<university>
<department>
<dept_name> Comp. Sci. </dept_name>
<building> Taylor </building>
<budget> 100000 </budget>
</department>
<course>
<course_id> CS-101 </course_id>
<title> Intro. to Computer Science </title>
<dept_name> Comp. Sci </dept_name>
<credits> 4 </credits>
</course>
</university>
Structure of XML Data
• Tag: label for a section of data
• Element: section of data beginning with <tagname>
and ending with matching </tagname>
• Elements must be properly nested
– Proper nesting
• <course> … <title> …. </title> </course>
– Improper nesting
• <course> … <title> …. </course> </title>
– Formally: every start tag must have a unique matching
end tag, that is in the context of the same parent element.
• Every document must have a single top-level element
Attributes
• Elements can have attributes
<course course_id= “CS-101”>
<title> Intro. to Computer
Science</title>
<dept name> Comp. Sci. </dept name>
<credits> 4 </credits>
</course>
• Attributes are specified by name=value pairs
inside the starting tag of an element
• An element may have several attributes, but
each attribute name can only occur once
<course course_id = “CS-101” credits=“4”>
XML Document Schema
• Database schemas constrain what information can be
stored, and the data types of stored values
• XML documents are not required to have an associated
schema
• However, schemas are very important for XML data
exchange
– Otherwise, a site cannot automatically interpret data received
from another site
• Two mechanisms for specifying XML schema
– Document Type Definition (DTD)
• Widely used
– XML Schema
• Newer, increasing use
Document Type Definition (DTD)
• The type of an XML document can be specified using a
DTD
• DTD constraints structure of XML data
– What elements can occur
– What attributes can/must an element have
– What subelements can/must occur inside each element,
and how many times.
• DTD does not constrain data types
– All values represented as strings in XML
• DTD syntax
– <!ELEMENT element (subelements-specification) >
– <!ATTLIST element (attributes) >
University DTD
<!DOCTYPE university [
<!ELEMENT university ( (department|course|instructor|teaches)+)>
<!ELEMENT department ( dept name, building, budget)>
<!ELEMENT course ( course id, title, dept name, credits)>
<!ELEMENT instructor (IID, name, dept name, salary)>
<!ELEMENT teaches (IID, course id)>
<!ELEMENT dept name( #PCDATA )>
<!ELEMENT building( #PCDATA )>
<!ELEMENT budget( #PCDATA )>
<!ELEMENT course id ( #PCDATA )>
<!ELEMENT title ( #PCDATA )>
<!ELEMENT credits( #PCDATA )>
<!ELEMENT IID( #PCDATA )>
<!ELEMENT name( #PCDATA )>
<!ELEMENT salary( #PCDATA )>
]>
Steps
•
•
•
•
•
•
•
Mapping XML into relational data
Generating XML using Java and JDBC
Storing XML
XML on the Web
XML support in Oracle
XML API for databases
Conclusion
Mapping XML into relational data
The database
• We can model the database with a document
node and its associated element node:
<?xml version=“1.0” ?>
<myDatabase>
table1
table2
...
tablen
</myDatabase>
• Order of tables is immaterial
The table
• Each table of the database is represented by an
element node with the records as its children:
<customer>
record1
record2
...
recordm
</customer>
• Again, order of the records is immaterial, since
the relational data model defines no ordering on
them.
The record
• A record is also represented by an element node,
with its fields as children:
<custRec>
field1
field2
...
fieldm
</custRec>
• The name is arbitrary, since the relational data
model doesn't define a name for a record type
The field
• A field is represented as an element node with a
data node as its only child:
<custName type="t">
d
</custName>
• If d is omitted, it means the value of the fields is
the empty string.
• The value of t indicates the type of the value
Example
<?xml version=“1.0” ?>
<myDatabase>
<customers>
<custRec>
<custName type=“String”>Robert Roberts</custName>
<custAge type=“Integer”>25</custAge>
</custRec>
<custRec>
<custName type=“String”>John Doe</custName>
<custAge type=“Integer”>32</custAge>
</custRec>
</customers>
</myDatabase>
Generating XML using Java and JDBC
Generating XML from relational data
Step 1 : Set up the database connection
// Create an instance of the JDBC driver so that it has
// a chance to register itself
Class.forName(sun.jdbc.odbc.JdbcOdbcDriver).newInstance();
// Create a new database connection.
Connection con =
DriverManager.getConnection(jdbc:odbc:myData, “”, “”);
// Create a statement object that we can execute queries with
Statement stmt = con.createStatement();
Generating XML (cont.)
Step 2 : Execute the JDBC query
String query = “Select Name, Age from Customers”;
ResultSet rs = stmt.executeQuery(query);
Generating XML (cont.)
Step 3 : Create the XML!
StringBuffer xml = “<?xml
version=‘1.0’?><myDatabase><customers>”;
while (rs.next()) {
xml.append(“<custRec><custName>”);
xml.append(rs.getString(“Name”));
xml.append(“</custName><custAge>”);
xml.append(rs.getInt(“Age”));
xml.append(“</custAge></custRec>”);
}
xml.append(“</customers></myDatabase>”);
Storing XML
Storing XML in relational tables
Step 1 : Set up the parser
StringReader stringReader = new
StringReader(xmlString);
InputSource inputSource = new
InputSource(stringReader);
DOMParser domParser = new DOMParser();
domParser.parse(inputSource);
Document document = domParser.getDocument();
Storing XML (cont.)
Step 2 : Read values from parsed XML document
NodeList nameList =
doc.getElementsByTagName(“custName”);
NodeList ageList =
doc.getElementsByTagName(“custAge”);
Storing XML (cont.)
Step 3 : Set up database connection
Class.forName(sun.jdbc.odbc.JdbcOdbcDriver).newInsta
nce();
Connection con =
DriverManager.getConnection(jdbc:odbc:myDataBase,
“”, “”);
Statement stmt = con.createStatement();
Storing XML (cont.)
Step 4 : Insert data using appropriate JDBC update query
String sql = “INSERT INTO Customers (Name, Age) VALUES
(?,?)”;
PreparedStatement pstmt = conn.prepareStatement(sql);
int size = nameList.getLength();
for (int i = 0; i < size; i++) {
pstmt. setString(1,
nameList.item(i).getFirstChild().getNodeValue());
pstmt.setInt(2,
ageList.item(i).getFirstChild().getNodeValue());
pstmt.executeUpdate(sql);
}
XML on the Web
XML on the Web (Servlets)
public void doGet(HttpServletRequest req,
HttpServletResponse resp)
{
resp.setContentType("text/xml");
PrintWriter out = new
PrintWriter(resp.getOutputStream());
… generate XML here, as before…
out.println(xmlGenerated);
out.flush();
out.close();
}
• Appropriate XSL can be inserted for display
XML in IE 5.0
insert the XSL…
<?xml version=“1.0” ?>
<?xml-stylesheet type="text/xsl"
href="http://myServer/Customer.xsl"?>
<myDatabase>
<customers>
<custRec>
<custName type=“String”>Robert Roberts</custName>
<custAge type=“Integer”>25</custAge>
</custRec>
… other records here …
</customers>
</myDatabase>
XML with XSL in IE 5.0
XML API with database