Java Reference
In-Depth Information
A
java.sql.Connection
represents a session with a specific database or, in this case, with a
specific XML document. The XML document is defined by the URL passed to the
Connection
object.
The
Connection
object now attempts to connect to the URL. Depending on the URL protocol, this may
be done in one of the following ways:
If the URL protocol indicates that the document is a file, the
Connection
attempts to open the file.
If the URL protocol indicates an HTTP connection, the
Connection
attempts to connect to the
URL and to open the XML document that way.
Once a connection to an existing file or to an HTTP data source has been established, the XML
document is parsed to a DOM document. In the case of a file URL where the file does not exist, a new
DOM document is created.
Listing 19-4
illustrates the
XMLConnection
class.
Listing 19-4: XMLConnection class
package JavaDatabaseBible.ch19.JDBCforXML;
import java.io.*;
import java.net.*;
import java.sql.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
import org.apache.xerces.parsers.DOMParser;
import org.apache.xerces.dom.DocumentImpl;
import JavaDatabaseBible.ch19.JDBCImpl.JDBCConnectionImpl;
/**
* Set up interface to xml document & create XMLStatement on request
*/
public class XMLConnection extends JDBCConnectionImpl{
private URL url;
private Document xmlDoc;
public XMLConnection() throws SQLException{
}
public XMLConnection(InputSource xml) throws SQLException{
try{
DOMParser p = new DOMParser();
p.parse(xml);
xmlDoc = p.getDocument();
}
catch (Exception e){
System.err.println(e);
}