Download presentation
Presentation is loading. Please wait.
Published byHugo Howard Modified over 8 years ago
1
XML and JAVA Joon C. Ho
2
Why is XML ? 7654 MARTIN SALESMAN 1250 7788 SCOTT ANALYST 3000 HTMLCorresponding XML 7654 MARTIN SALESMAN</td 1250 7788 SCOTT ANALYST 3000 The tag semantics and the tag set are fixed. Limitation of Creating Rich Documents. No predefined tag set. Richly structured documents over the web.
3
Similarity Between Object & XML 1234 Microsoft 29.10.00 A-10 12 10.95 B-43 600 3.99 Java ObjectXML object SalesOrder { number = 1234; customer = ”Microsoft"; date = 29.10.00; items = {ptrs to Item objects}; } / \ / \ object Item { object Item { number = 1; number = 2; part = "A-10"; part = "B-43"; quantity = 12; quantity = 600; price = 10.95; price = 3.95; } }
4
Big Picture to Save Java Object XML Doc. Oracle8 Java Beans Class Java Class Original 4 classes need be modified as Java Beans Place Transition Arch (Token) Using JOX Convert Beans to XML JOX DTD Parser & Middleware Jox115.jar Jaxp.jar Lib Xmlparserv2.jar DTD Parser(Oracle) Using XML-DBMS (Ronald Bourret*) or XDK(Oracle), JDBC-Thin Xmldbms.jar (XML-DBMS) Lib Xmlparserv2.jar XML Parser(Oracle) * Ronald Bourret is a freelance programmer JDBC
5
Beans to XML – JOX JOX is a set of Java libraries that make it easy to transfer data between XML documents and Java beans. You must use Java Beans (get/set methods specifically). JOX uses introspection to figure out the property names. XML tag names must match bean property names within reason. JOX compares XML tags to bean properties ignoring capitalization, dashes, underscores, colons and dots. The XML tag will map successfully to firstName, first_name and fIrSTn_aME. Because XML data is in the form of a tree structure, JOX can't handle bean structures that have circular references unless you use a DTD when writing the XML. JOX tries to convert XML data to the type of the bean property. There is no facility for custom conversions. Without a DTD, JOX uses bean property names as XML tag names.
6
Converting Object to XML XML Doc.Java Beans Name Java Object Java Object needs to be Java Beans set/get methods Using JOX Convert Beans to XML JOX* Jox115.jar Jaxp.jar Library Xmlparserv2.jar DTD Parser(Oracle) *Java Object in XML
7
JOX - Example public class TestBean implements java.io.Serializable { protected int foo; protected String bar; protected TestSubbean subbean; //Nested Class public TestBean() { } public int getFoo() { return foo; } public void setFoo(int aFoo) { foo = aFoo; } public String getBar() { return bar; } public void setBar(String aBar) { bar = aBar; } public TestSubbean getSub() { return subbean; } public void setSub(TestSubbean aSub) { subbean = aSub; } } public class TestSubbean implements java.io.Serializable { protected String name; protected int age; public TestSubbean() { } public String getName() { return name; } public void setName(String aName) { name = aName; } public int getAge() { return age; } public void setAge(int anAge) { age = anAge; } Top Level Java BeanNested Java Bean
8
JOX – Example (Cont’d) public class TestSer { public static void main(String[] args) { try { TestBean b = new TestBean(); b.setFoo(5); b.setBar("This is the bar value"); TestSubbean sub = new TestSubbean(); sub.setName("Mark"); sub.setAge(35); b.setSub(sub); FileOutputStream fileOut = new FileOutputStream(" bean.xml "); JOXBeanOutputStream joxOut = new JOXBeanOutputStream(fileOut); joxOut.writeObject("MarkTest", b); joxOut.close(); } catch (Exception exc) { exc.printStackTrace(); } } ?xml version="1.0" encoding="ISO-8859-1"?> 5 This is the bar value 35 Application to ConvertBean.xml
9
XML to/from Oracle8 XML Doc. Oracle8 DTD Parser & Middleware Database Connection : JDBC (Thin Driver) Middleware : XML-DBMS (Ronald Bourret*) “xmldbms.jar” (XML-DBMS) “xmlparserv2.jar” (Oracle DTD parser) * Ronald Bourret is a freelance programmer JDBC
10
Two Ways to Connect to Oracle8 Note : We are going to use Thin Driver
11
JDBC -Thin Driver(Oracle) Use TCP/IP Port to access database UserId, Password, Host name and database SID are required to connect database The driver is written completely in Java, requires NO pre-installation of any software. Support for Oracle-specific datatypes such as ROWIDs and REFCURSORs Supports all Oracle7 database versions and can also access Oracle8 databases
12
JDBC -Thin Example Connection dbConnection=null; String driver="oracle.jdbc.driver.OracleDriver"; try{ Class.forName(driver); dbConnection = DriverManager.getConnection("jdbc:oracle:thin:@hostname:1521:SID", ”userid",”passwd"); Statement statement = dbConnection.createStatement(); ResultSet results = statement.executeQuery(query); while(results.next()){ …………………. } } catch(Exception e){..} hostname : orabox.utdallas.edu SID : last
13
XML-DBMS - Getting Started To run XML-DBMS, you need the following software: *XML-DBMS (Download: http://www.rpbourret.com/xmldbms/index.htm)http://www.rpbourret.com/xmldbms/index.htm *JDK (Java Development Kit) 1.1.x or 1.2.x (Download: http://java.sun.com/products/jdk/1.1/index.htmlhttp://java.sun.com/products/jdk/1.1/index.html *A relational database such as Oracle, DB2, Informix, MS Access, or MySQL *A JDBC driver for your database (http://industry.java.sun.com/products/jdbc/drivers)http://industry.java.sun.com/products/jdbc/drivers *An XML parser written in Java (Oracle DTD Parser) *A DOM level 1 implementation written in Java *SAX (Simple API for XML) version 1(http://www.megginson.com/SAX/index.html)http://www.megginson.com/SAX/index.html System Requirements To Download XML-DBMS: http://www.rpbourret.com/xmldbms/index.htm To install XML-DBMS: Unzip the downloaded file Add xmldbms.jar to your CLASSPATH. For example: c:\Java\xmldbms\xmldbms.jar;c:\Java\jdk1.2.1\bin;c:\Java\xerces-1_1_1\xerces.jar;.\
14
How to Use XML-DBMS XML Doc. Consist of DTD and BODY DTDBODY DTD Generate map Generate sql statement Save/Retrieve XML to DB Create Schema to DB Using Bean.map and Bean.xml Bean.dtd Bean.map Bean.dtdBean.xml
15
XML-DBMS – Example 5 This is the bar value 35 Bean.dtdBean.xml
16
XML-DBMS – Example(Cont’d) ………………………. CREATE TABLE "MarkTest" ( "MarkTestPK" INTEGER, "BAR" VARCHAR(255), "Foo" VARCHAR(255), "BAROrder" INTEGER, "FooOrder" INTEGER, "MarkTestOrder" INTEGER) CREATE TABLE "S-U-B" ( "MarkTestFK" INTEGER, "age" VARCHAR(255), "name" VARCHAR(255), "nameOrder" INTEGER, "ageOrder" INTEGER, "S-U-BOrder" INTEGER) Bean.mapBean.sql Command> java GenerateMap Bean.dtd (This creates Bean.map and Bean.sql files) *There is no “;” after each “CREATE TABLE” Statement. So, It should be attached while using this SQL command.
17
XML-DBMS – To Oracle8 1. Create table schema to Oracle8 using Bean.sql 2. Command> java Transfer –todbms Bean.map Bean.xml *In order to test, just use SQL command in the Oracle8 server. But, in the actual system design we need to use programming using JDBC API’s, such as Statement, ResultSet. *As can be seen in the previous slide, Bean.map file looks for “xmldbms.dtd”, which is pre-defined DTD file. So, put the path information to that file.
18
XML-DBMS – From Oracle8 Next…
19
JDBC/XML Team Schedule JOX Test : Bean to/from XML XML-DBMS/XDK Test : XML to/from Oracle8 Modify 4 classes to Beans and JOX Test Place/ JOX Test Transition/ JOX Test Arch/ JOX Test (Token)/ JOX Test Place/ XML-DBMS/XDK Test Transition/ XML-DBMS/XDK Test Arch/ XML-DBMS/XDK Test (Token)/ XML-DBMS/XDK Test JOX Test : Bean to Java Object GUI/System Test 6/307/30 8/10 Joon/Hitendra Joon/Terence Joon Hitendra Terence Joon Hitendra Terence Joon/Hitendra All Members
20
Conclusion
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.