Example 12-34: Programmatically inserting news stories

import oracle.xml.parser.v2.*;
import java.io.InputStream;
import org.w3c.dom.*;
import java.net.URL;
import java.sql.Connection;
import oracle.xml.sql.dml.OracleXMLSave;
import Examples;

public class MoreoverIntoNewsstory {

  public static void main( String[] arg ) throws Exception {

    String theNews = "http://www.moreover.com/cgi-local/page?index_xml+xml",
           theXSL  = "moreover-to-newsstory.xsl";

      // Create a DOM Parser to Parse the News Document
      DOMParser dp = new DOMParser();
      dp.parse( theNews );
      // Parse the document at the URL specified in theURLString
      XMLDocument moreoverNewsDoc = dp.getDocument();
      // Search for a list of all the matching articles and print the count
      NodeList nl = moreoverNewsDoc.selectNodes("moreovernews/article");
      int articleCount = nl.getLength();
      System.out.println("Received " + articleCount + " articles...");
      // Load the XSL Stylesheet from the top-level directory on CLASSPATH
      InputStream xslstream = Object.class.getResourceAsStream("/"+theXSL);
      XSLStylesheet transform = new XSLStylesheet(xslstream,null);
      // Create an instance of XSLProcessor to perform the transformation
      XSLProcessor  processor = new XSLProcessor();
      // Transform moreoverNewsDoc by theXSL and get result as a DOM Document Fragment
      DocumentFragment df = processor.processXSL(transform, moreoverNewsDoc);
      // Create a new XML Document and append the fragment to it
      Document result = new XMLDocument();
      result.appendChild(df);
      // Pass the transformed document (now in canonical format) to OracleXMLSave
      Connection   conn = Examples.getConnection();
      OracleXMLSave oxs = new OracleXMLSave(conn,"newsstoryview");
      int rowsInserted = oxs.insertXML( result );
      conn.commit();
      conn.close();
      System.out.println("Inserted " + rowsInserted + " articles...");
  }
}