veenasinghpres.ppt

35
CS 8630 Database Administration, Dr. Guimaraes XML Veena Singh CS 8630, Summer 2004 Transferring Data between DBMSs

Upload: databaseguys

Post on 12-Feb-2017

195 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: VeenaSinghpres.ppt

CS 8630 Database Administration, Dr. Guimaraes

XML

Veena Singh

CS 8630, Summer 2004

Transferring Data between DBMSs

Page 2: VeenaSinghpres.ppt

CS 8630 Database Administration, Dr. Guimaraes

Different ways to use XML

• I am going to demonstrate two different ways to transfer data between DBMSs using XML.

– 1) First way with Procedures (MS Access 2002, Oracle 9i)

– 2) Second way with XMLSPY

Page 3: VeenaSinghpres.ppt

CS 8630 Database Administration, Dr. Guimaraes

Save in Access as XML

Page 4: VeenaSinghpres.ppt

CS 8630 Database Administration, Dr. Guimaraes

Export XML Select Options

• You need the Data (XML) file and the Schema of the data (data definition, similar to DDL) to transfer data.

• You need to select the Presentation of your data (XSL) also if you want to display data with a web browser.

• This will create a xml and a xsd file.

Page 5: VeenaSinghpres.ppt

CS 8630 Database Administration, Dr. Guimaraes

Sample MS Access XML file

• <?xml version = "1.0" encoding = "UTF-8"?>• <dataroot>• <employee>• <eID>456</eID>• <eLast>Last456</eLast>• <eFirst>First456</eFirst>• <eTitle>Software Engineer</eTitle>• <eSalary>45456</eSalary>• </employee>• <employee>• <eID>789</eID>• <eLast>Last789</eLast>• <eFirst>First789</eFirst>• <eTitle>Database Administrator</eTitle>• <eSalary>78789</eSalary>• </employee>•  • .• .• .•  • <employee>• <eID>999</eID>• <eLast>Last999</eLast>• <eFirst>First999</eFirst>• <eTitle>Manager</eTitle>• <eSalary>100999</eSalary>• </employee>• </dataroot>•  •  • Code snippet for Access XML for employee table

Page 6: VeenaSinghpres.ppt

CS 8630 Database Administration, Dr. Guimaraes

MS Access XML Schema• <?xml version="1.0" encoding="UTF-8"?>• <xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema" xmlns:od="urn:schemas-microsoft-com:officedata">• <xsd:element name="dataroot">• <xsd:complexType>• <xsd:choice maxOccurs="unbounded">• <xsd:element ref="employee"/>• </xsd:choice>• </xsd:complexType>• </xsd:element>• <xsd:element name="employee">• <xsd:annotation>• <xsd:appinfo>• <od:index index-name="PrimaryKey" index-key="eID " primary="yes" unique="yes" clustered="no"/>• <od:index index-name="eID" index-key="eID " primary="no" unique="no" clustered="no"/>• <od:index index-name="eTitle" index-key="eTitle " primary="no" unique="no" clustered="no"/>• </xsd:appinfo>• </xsd:annotation>• <xsd:complexType>• <xsd:sequence>• <xsd:element name="eID" od:jetType="text" od:sqlSType="nvarchar" od:nonNullable="yes">• <xsd:simpleType>• <xsd:restriction base="xsd:string">• <xsd:maxLength value="3"/>• </xsd:restriction>• </xsd:simpleType>• </xsd:element>• .• .• .•  • </xsd:sequence>• </xsd:complexType>• </xsd:element>• </xsd:schema>•  • Code snippet of XSD format used by Access

Page 7: VeenaSinghpres.ppt

CS 8630 Database Administration, Dr. Guimaraes

MS Access XSL File

• <?xml version = "1.0" encoding = "UTF-8"?>• <xsl:transform xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"

version = "1.0">• <xsl:template match = "dataroot">• <xsl:element name = "ROWSET">• <xsl:apply-templates select = "child::*"/>• </xsl:element>• </xsl:template>• <xsl:template match = "child::*">• <xsl:element name = "ROW">• <xsl:attribute name = "num">• <xsl:value-of select = "position()"/>• </xsl:attribute>• <xsl:for-each select="child::*">• <xsl:copy-of select = "."/>• </xsl:for-each>• </xsl:element>• </xsl:template>• </xsl:transform>•   • Code snippet of XSL file

Page 8: VeenaSinghpres.ppt

CS 8630 Database Administration, Dr. Guimaraes

Conversion

To input the XML file from Access to Oracle, run the XML file output from Access through TIBCO XML Transform, XMLSPY, or some other conversion software.

This is required because the XML format in Access is different than the XML format used by Oracle.

Page 9: VeenaSinghpres.ppt

CS 8630 Database Administration, Dr. Guimaraes

XML Format used by Oracle

• <?xml version = "1.0" encoding = "UTF-8"?>• <ROWSET>• <ROW num = "1">• <eID>456</eID>• <eLast>Last456</eLast>• <eFirst>First456</eFirst>• <eTitle>Software Engineer</eTitle>• <eSalary>45456</eSalary>• </ROW>• .• .•  • <ROW num = "19">• <eID>999</eID>• <eLast>Last999</eLast>• <eFirst>First999</eFirst>• <eTitle>Manager</eTitle>• <eSalary>100999</eSalary>• </ROW>• </ROWSET>•  • XML data given out after conversion

Page 10: VeenaSinghpres.ppt

CS 8630 Database Administration, Dr. Guimaraes

Steps to input into Oracle

To input the XML data into Oracle, follow these steps:

• Create a table with the given name in Oracle(e.g. We will be creating an ‘employee’ table.).

• Create procedure insProc. • Run the procedure insProc with the Oracle

XML data.

Page 11: VeenaSinghpres.ppt

CS 8630 Database Administration, Dr. Guimaraes

Create Table & Procedures

• CREATE TABLE Employee (• eID VARCHAR(11),• eLast VARCHAR(20),• eFirst VARCHAR(20),• eTitle VARCHAR(20),• eSalary FLOAT• );

•  • -- insert XML data into table• --proc insProc• create or replace procedure insProc(xmlDoc IN CLOB, tableName IN VARCHAR2) is• insCtx DBMS_XMLSave.ctxType;• rows number;• begin• insCtx := DBMS_XMLSave.newContext(tableName); -- get the context handle• rows := DBMS_XMLSave.insertXML(insCtx,xmlDoc); -- this inserts the document• DBMS_XMLSave.closeContext(insCtx); -- this closes the handle• end;• /

• -- The following example uses default XML mapping.• -- print result• CREATE OR REPLACE PROCEDURE printClobOut(result IN OUT NOCOPY CLOB) is• xmlstr varchar2(32767);• line varchar2(2000);• begin• xmlstr := dbms_lob.SUBSTR(result,32767);• loop• exit when xmlstr is null;• line := substr(xmlstr,1,instr(xmlstr,chr(10))-1);• dbms_output.put_line('| '||line);• xmlstr := substr(xmlstr,instr(xmlstr,chr(10))+1);• end loop;• end;• /•  

Page 12: VeenaSinghpres.ppt

CS 8630 Database Administration, Dr. Guimaraes

Run the procedures • -- to run the procedures • declare • v_doc CLOB;• begin• v_doc := '<?xml version="1.0"?>' || Chr(10) || ' • <ROWSET>• <ROW num = "1">• <eID>456</eID>• <eLast>Last456</eLast>• <eFirst>First456</eFirst>• <eTitle>Software Engineer</eTitle>• <eSalary>45456</eSalary>• </ROW>• .• .• </ROWSET>• ';• --printClobOut(v_doc);• insProc(v_doc, 'scott.employee');• -- queryCtx := DBMS_XMLQuery.newContext('select * from employee' );• end;• / •  • Code snippet to input XML data into Oracle

Page 13: VeenaSinghpres.ppt

CS 8630 Database Administration, Dr. Guimaraes

Export XML data from Oracle

• -- The following example uses default XML mapping.• -- print result• CREATE OR REPLACE PROCEDURE printClobOut(result IN OUT NOCOPY CLOB) is• xmlstr varchar2(32767);• line varchar2(2000);• begin• xmlstr := dbms_lob.SUBSTR(result,32767);• loop• exit when xmlstr is null;• line := substr(xmlstr,1,instr(xmlstr,chr(10))-1);• dbms_output.put_line('| '||line);• xmlstr := substr(xmlstr,instr(xmlstr,chr(10))+1);• end loop;• end;• /•  •  • -- generate XML for query result using default mapping• declare• queryCtx DBMS_XMLquery.ctxType;• result CLOB;• begin•  • -- set up the query context...!• queryCtx := DBMS_XMLQuery.newContext('select * from student);• • -- get the result..!• result := DBMS_XMLQuery.getXML(queryCtx);• -- Now you can use the result to put it in tables/send as messages..• printClobOut(result);• DBMS_XMLQuery.closeContext(queryCtx); -- you must close the query handle..• end;• /•  • SQL code to run export data out of Oracle

Page 14: VeenaSinghpres.ppt

CS 8630 Database Administration, Dr. Guimaraes

Oracle XML file

The procedure on the previous slide will print the XML data out on the SQL Plus terminal.

Copy and paste the data into a text file and save it as an XML file.

Page 15: VeenaSinghpres.ppt

CS 8630 Database Administration, Dr. Guimaraes

Sample Oracle XML data• <?xml version = "1.0" encoding = "UTF-8"?>

• <ROWSET> • <ROW num="1"> • <PID>111-11-1111</PID> • <DOB>6/26/1973 0:0:0</DOB> • <FIRSTNAME>Ying</FIRSTNAME>

• <LASTNAME>Ma</LASTNAME>

• <STATUS>Graduate</STATUS>

• <MAJOR>CSE</MAJOR> • </ROW> • <ROW num="2">"> • <MAJOR>NUR</MAJOR> • </ROW> • .• .• .• /ROWSET> •  •  • Code snippet of XML data given out by Oracle

Page 16: VeenaSinghpres.ppt

CS 8630 Database Administration, Dr. Guimaraes

Converting

To convert the Oracle XML into Access XML run the XML file through TIBCO XML Transform, XMLSPY, or some other conversion software.

Save the XML output file after the software has converted the XML file into another XML format.

Page 17: VeenaSinghpres.ppt

CS 8630 Database Administration, Dr. Guimaraes

Sample output after conversion

• <?xml version = "1.0" encoding = "UTF-8"?>• <dataroot>• <student>• <pID>111-11-1111</pID>• <DateOfBirth>6/26/1973 0:0:0</DateOfBirth>• <FirstName>Ying</FirstName>• <LastName>Ma</LastName>• <status>Graduate</status>• <major>CSE</major>• </student>• <student>• <pID>111-22-2222</pID>• <DateOfBirth>5/15/1982 0:0:0</DateOfBirth>• <FirstName>Ana</FirstName>• <LastName>White</LastName>• <status>Freshman</status>• <major>NUR</major>• </student>• .• .• </dataroot>•  •  • Code snippet of XML data given out after transformation

Page 18: VeenaSinghpres.ppt

CS 8630 Database Administration, Dr. Guimaraes

Importing into Access

To import the XML data into Access, go to the MS Access menu option File\Get External Data\Import.

Import the XML file. Access will create a table for the XML data imported. If there is a table already existing with the same name, it will ask if the data needs to be appended to the existing table or if the data needs to replace the existing table with the new data.

Page 19: VeenaSinghpres.ppt

CS 8630 Database Administration, Dr. Guimaraes

XMLSPY

Page 20: VeenaSinghpres.ppt

CS 8630 Database Administration, Dr. Guimaraes

Pick A Source Database

Page 21: VeenaSinghpres.ppt

CS 8630 Database Administration, Dr. Guimaraes

Login

Page 22: VeenaSinghpres.ppt

CS 8630 Database Administration, Dr. Guimaraes

Click Choose database table

Page 23: VeenaSinghpres.ppt

CS 8630 Database Administration, Dr. Guimaraes

Pick a table

Page 24: VeenaSinghpres.ppt

CS 8630 Database Administration, Dr. Guimaraes

Click Import

Page 25: VeenaSinghpres.ppt

CS 8630 Database Administration, Dr. Guimaraes

XML data from Oracle

Page 26: VeenaSinghpres.ppt

CS 8630 Database Administration, Dr. Guimaraes

Exporting

Page 27: VeenaSinghpres.ppt

CS 8630 Database Administration, Dr. Guimaraes

Click Export to database

Page 28: VeenaSinghpres.ppt

CS 8630 Database Administration, Dr. Guimaraes

Choose an option

Page 29: VeenaSinghpres.ppt

CS 8630 Database Administration, Dr. Guimaraes

Where to save it

Page 30: VeenaSinghpres.ppt

CS 8630 Database Administration, Dr. Guimaraes

Saving as MS Access file

Page 31: VeenaSinghpres.ppt

CS 8630 Database Administration, Dr. Guimaraes

Export Completed

Page 32: VeenaSinghpres.ppt

CS 8630 Database Administration, Dr. Guimaraes

Users table is now in MS Access

Page 33: VeenaSinghpres.ppt

CS 8630 Database Administration, Dr. Guimaraes

Table in MS Access

Page 34: VeenaSinghpres.ppt

CS 8630 Database Administration, Dr. Guimaraes

Summary

There are different methods to get the XML data across to different databases.

Two methods were shown in this presentation.

There are other software packages like XMLSPY that will transfer the data from one DBMS to another.

Page 35: VeenaSinghpres.ppt

CS 8630 Database Administration, Dr. Guimaraes

REFERENCES

Internet (from science.kennesaw.edu/~mguimara/8630/07-06-04-Tuesday-B.ppt).

World Wide Web (from www.eas.asu.edu/~advdb/).

World Wide Web (from www.tibco.com).

World Wide Web (from www.xml.com).

World Wide Web (from www.xml.org ).

World Wide Web (from www.xmlspy.com).