Download presentation
Presentation is loading. Please wait.
1
XML and Databases
2
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
3
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> </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>
4
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
5
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”>
6
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
7
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) >
8
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 )> ]>
9
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
10
Mapping XML into relational data
11
The database We can model the database with a document node and its associated element node: <?xml version=“1.0” ?> <myDatabase> table1 table tablen </myDatabase> Order of tables is immaterial
12
The table Each table of the database is represented by an element node with the records as its children: <customer> record1 record recordm </customer> Again, order of the records is immaterial, since the relational data model defines no ordering on them.
13
The record A record is also represented by an element node, with its fields as children: <custRec> field1 field fieldm </custRec> The name is arbitrary, since the relational data model doesn't define a name for a record type
14
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
15
Example <?xml version=“1.0” ?> <myDatabase>
<customers> <custRec> <custName type=“String”>Robert Roberts</custName> <custAge type=“Integer”>25</custAge> </custRec> <custName type=“String”>John Doe</custName> <custAge type=“Integer”>32</custAge> </customers> </myDatabase>
16
Generating XML using Java and JDBC
17
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();
18
Generating XML (cont.) Step 2 : Execute the JDBC query
String query = “Select Name, Age from Customers”; ResultSet rs = stmt.executeQuery(query);
19
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>”);
20
Storing XML
21
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();
22
Storing XML (cont.) Step 2 : Read values from parsed XML document
NodeList nameList = doc.getElementsByTagName(“custName”); NodeList ageList = doc.getElementsByTagName(“custAge”);
23
Storing XML (cont.) Step 3 : Set up database connection
Class.forName(sun.jdbc.odbc.JdbcOdbcDriver).newInstance(); Connection con = DriverManager.getConnection(jdbc:odbc:myDataBase, “”, “”); Statement stmt = con.createStatement();
24
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); }
25
XML on the Web
26
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
27
XML in IE 5.0
28
insert the XSL… <?xml version=“1.0” ?> <?xml-stylesheet type="text/xsl" href=" <myDatabase> <customers> <custRec> <custName type=“String”>Robert Roberts</custName> <custAge type=“Integer”>25</custAge> </custRec> … other records here … </customers> </myDatabase>
29
XML with XSL in IE 5.0
30
XML API with database
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.