200 likes | 320 Views
XML and JAVA. Joon C. Ho. Why is XML ?. HTML. Corresponding XML. <? xml version="1.0"?> <EmployeeList> <Employee> <ID>7654</ID> <Name>MARTIN</Name> <Job>SALESMAN</Job> <Salary>1250</Salary> </Employee> <Employee> <ID>7788</ID>
E N D
XML and JAVA Joon C. Ho
Why is XML ? HTML Corresponding XML <?xml version="1.0"?> <EmployeeList> <Employee> <ID>7654</ID> <Name>MARTIN</Name> <Job>SALESMAN</Job> <Salary>1250</Salary> </Employee> <Employee> <ID>7788</ID> <Name>SCOTT</Name> <Job>ANALYST</Job> <Salary>3000</Salary> </Employee> </EmployeeList> <table> <tr> <td>7654</td> <td>MARTIN</td> <td>SALESMAN</td <td>1250</td> </tr> <tr> <td>7788</td> <td>SCOTT</td> <td>ANALYST</td> <td>3000</td> </tr> </table> The tag semantics and the tag set are fixed. Limitation of Creating Rich Documents. No predefined tag set. Richly structured documents over the web.
Similarity Between Object & XML Java Object XML <SalesOrder> <Number>1234</Number> <Customer>Microsoft</Customer> <Date>29.10.00</Date> <Item Number="1"> <Part>A-10</Part> <Quantity>12</Quantity> <Price>10.95</Price> </Item> <Item Number="2"> <Part>B-43</Part> <Quantity>600</Quantity> <Price>3.99</Price> </Item> </SalesOrder> 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; } }
Big Picture to Save Java Object Oracle8 Java Class Java Beans XML Doc. Class DTD Parser & Middleware JOX JDBC • Original 4 classes • need be modified • as Java Beans • Place • Transition • Arch • (Token) • Using JOX • Convert Beans to XML • Using XML-DBMS (Ronald Bourret*) or XDK(Oracle), JDBC-Thin Lib Lib • Jox115.jar • Jaxp.jar • Xmldbms.jar (XML-DBMS) DTD Parser(Oracle) XML Parser(Oracle) • Xmlparserv2.jar • Xmlparserv2.jar * Ronald Bourret is a freelance programmer
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 <first-name> 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.
Converting Object to XML Java Object Java Beans XML Doc. Name JOX* • Java Object needs to be Java Beans • set/get methods • Using JOX • Convert Beans to XML Library • Jox115.jar • Jaxp.jar DTD Parser(Oracle) • Xmlparserv2.jar *Java Object in XML
JOX - Example Top Level Java Bean Nested Java Bean 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; } 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; } }
JOX – Example (Cont’d) Application to Convert Bean.xml • 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"?> <MarkTest> <foo>5</foo> <BAR>This is the bar value</BAR> <S-U-B name="Joon"> <age>35</age> </S-U-B> </MarkTest>
XML to/from Oracle8 Oracle8 XML Doc. DTD Parser & Middleware JDBC • 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
Two Ways to Connect to Oracle8 • Note : We are going to use Thin Driver
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
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
XML-DBMS - Getting Started System Requirements To run XML-DBMS, you need the following software: • XML-DBMS (Download: 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.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) • 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) 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;.\
How to Use XML-DBMS XML Doc. Consist of DTD and BODY DTD Bean.dtd Generate map Bean.map Generate sql statement Create Schema to DB DTD BODY Using Bean.map and Bean.xml Save/Retrieve XML to DB Bean.dtd Bean.xml
XML-DBMS – Example Bean.dtd Bean.xml <?xml version="1.0" encoding="ISO-8859-1"?> <!ELEMENT MarkTest (Foo?, BAR?, S-U-B? )> <!ELEMENT Foo (#PCDATA)> <!ELEMENT BAR (#PCDATA)> <!ELEMENT S-U-B (age, name)> <!ELEMENT age (#PCDATA)> <!ELEMENT name (#PCDATA)> <?xml version="1.0" encoding="ISO-8859-1"?> <MarkTest> <foo>5</foo> <BAR>This is the bar value</BAR> <S-U-B name="Joon"> <age>35</age> </S-U-B> </MarkTest>
XML-DBMS – Example(Cont’d) Bean.map Bean.sql 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) <?xml version='1.0' ?> <!DOCTYPE XMLToDBMS SYSTEM "xmldbms.dtd" > <XMLToDBMS Version="1.0"> <Options></Options> <Maps> <ClassMap> <ElementType Name="MarkTest"/> <ToRootTable> <Table Name="MarkTest"/> <CandidateKey Generate="Yes"> <Column Name="MarkTestPK"/> </CandidateKey> ………………………. • There is no “;” after each “CREATE TABLE” Statement. So, It should be attached while using this SQL command. Command> java GenerateMap Bean.dtd (This creates Bean.map and Bean.sql files)
XML-DBMS – To Oracle8 1. Create table schema to Oracle8 using Bean.sql • 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. 2. Command> java Transfer –todbms Bean.map Bean.xml • 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.
XML-DBMS – From Oracle8 Next…
JDBC/XML Team Schedule JOX Test : Bean to/from XML Joon/Hitendra XML-DBMS/XDK Test : XML to/from Oracle8 Joon/Terence Modify 4 classes to Beans and JOX Test Joon Place/ JOX Test Hitendra Transition/ JOX Test Terence Arch/ JOX Test (Token)/ JOX Test Joon Place/ XML-DBMS/XDK Test Hitendra Transition/ XML-DBMS/XDK Test Terence Arch/ XML-DBMS/XDK Test (Token)/ XML-DBMS/XDK Test Joon/Hitendra JOX Test : Bean to Java Object GUI/System Test All Members 6/30 7/30 8/10