[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Parsing XML & JSP problems

  • To: xml-dev@l...
  • Subject: Parsing XML & JSP problems
  • From: Meichun Li <lim@m...>
  • Date: Wed, 5 Jun 2002 16:30:46 -0400 (EDT)

jsp parse xml
Hi,

I have troubles in Paring XML element into database
using JSP. The data couldn't be inserted into the table.
More detail about the questions are as below.The JavaBean class is 
converted from a class that works well for parsing XML.
Would you please give me some hints or recommend some books/websites
about parsing XML& JSP for references?
Thanks a lot in advance!

-Meichun

*****************************
My jsp page:

<%@ page language="java" contentType="text/html" %>

<%@ include file="../includes/header.html" %>

        <jsp:useBean id="xmlbean" scope="application" 
                    class="org.ibiblio.osprey.DocumentTemp" />
        <% xmlbean.saveXmlfile(); %>       
        </jsp:useBean>

<%@ include file="../includes/footer.html" %>

*****************************************
The JavaBeans is:

package org.ibiblio.osprey;

import java.sql.*;                      /* JDBC Classes */
import org.w3c.dom.*;                   /* W3C Interfaces */
import org.apache.xerces.dom.*;         /* Xerces DOM Classes */
import org.apache.xml.serialize.*;      /* Xerces serializer */
import java.io.*;                       /* Java io classes for file access 
*/
import java.util.*;                     /* Java util class */
import org.apache.xerces.parsers.DOMParser; /* Paser import */
import org.ibiblio.util.*;

public class DocumentTemp implements java.io.Serializable {

  //Properties
  private String xmlfile;

  //constructor
  public void document() {
    xmlfile = "test.xml";
  }
  
  
/**************************************************************************
   *Parse XMl file data into database
**************************************************************************
  */
  public void saveXmlfile() throws Exception {

    try{

        // Get the DOM tree as a Document object
            DOMParser parser = new DOMParser();
            parser.parse("/osrt/osprey/webapps/WEB-INF/classes/org/ibiblio/osprey/test.xml");
            Document doc = parser.getDocument();

        // Making DBC Connection
            DBConnect c=new DBConnect();
            Connection conn = c.createConnect();
            Statement statement = conn.createStatement();

       // Handle Dom and process data into tables in database
        NodeList resource_list=doc.getElementsByTagName("resource");
        if( resource_list!=null)
        {
              for (int i=0; i<resource_list.getLength(); i++)
              {
                //convert resource to element
                Element resource=(Element)resource_list.item(i);

                //insert document date and title into table document
                insertsDoc(resource, statement);

              }
        }

       // Closing DB connection
        c.closeConnect();

    }
    catch (Exception e)
    {
       e.printStackTrace();
    }

  }//end of saveXmlfile method

 /*********************************************************
   *Private method:  to insert document date and document title into 
   *                 document table
   *************************************************
   */
   private  void insertsDoc(Element resource, Statement st) throws Exception
   {
   if( resource!=null)
   {
       String doc_date="";  //to hold document date
       String doc_title=""; // to hold document title
       String insert_doc=""; //to hold insert statement to  insert data into doc table

       //get document date
       NodeList dates=resource.getElementsByTagName("date");
       if(dates!=null)
       {
             // there is only one date tag for a resource, so it is item 0
             Node date=dates.item(0);
             if(date!=null)
                    doc_date= (date.getFirstChild()).getNodeValue();
             //format doc_date
             doc_date=formatString(doc_date);
       }

       //get doc_title
       NodeList titles=resource.getElementsByTagName("title");
       if(titles!=null)
       {
              Node title=titles.item(0);
              if(title!=null)
                    doc_title= (title.getFirstChild()).getNodeValue();
              doc_title=formatString(doc_title);
       }

       //built insert statement
       if(!(doc_date.equals("") && doc_title.equals("")))
              insert_doc="insert into document(docdate,title) values("+"'"+doc_date+"', "+"'"+doc_title+"')";
       if(doc_date.equals("") && doc_title.equals(""))
              insert_doc="insert into document(docdate,title) values(NULL, NULL)";
       if(doc_date.equals("")&&!doc_title.equals(""))
              insert_doc="insert into document(docdate,title) values(NULL"+", '"+doc_title + "')";
       if(!doc_date.equals("")&&doc_title.equals(""))
              insert_doc="insert into document(docdate,title) values("+"'"+doc_date+"', "+ "NULL )";

        //insert data into document table 
       int resultInsertDoc= st.executeUpdate(insert_doc);
       
    }
  }//end of method insertsDoc

 }//end of DocumentTemp
  







PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.