OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

 


 

   Parsing XML & JSP problems

[ Lists Home | Date Index | Thread Index ]

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
  










 

News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 2001 XML.org. This site is hosted by OASIS