Link to home
Start Free TrialLog in
Avatar of badour_ma
badour_ma

asked on

how to insert data to database from parsed xml document??

I have the follwing code and I want to insert the printed data in the database using oracle10g how can I do that??
 so the first data element is the coulmn name and the second data elemnt is the coulumn data??

package com.psol.xbe2;

import java.io.*;

import org.xml.sax.*;
import org.xml.sax.helpers.*;

public class MySAXApp1
    extends DefaultHandler
{
    private final static String[] searchData = {"XML", "oracle Magazine", "Java", "sql"};

    private String prevData;
    private boolean insideTag;

    public MySAXApp1()
    {
        this.prevData = null;
        this.insideTag = false;
    }

    public void startElement(String uri, String name, String qName, Attributes atts)
    {
        this.insideTag = true;
    }

    public void endElement(String uri, String name, String qName)
    {
        this.insideTag = false;
    }

    public void characters(char ch[], int start, int length)
    {
        String data = String.valueOf(ch, start, length).trim();

        if(this.insideTag && this.prevData != null)
        {
            System.out.println("Data in next tag after founding '"+prevData+"': "+data);

            this.prevData = null;
        }

        for(int i=0;i<searchData.length;i++)
        {
            if(data.equalsIgnoreCase(searchData[i]))
            {
                this.prevData = data;
                break;
            }
        }
    }

    //Program entry
    public static void main (String args[])
        throws Exception
    {
        MySAXApp1 handler = new MySAXApp1();

        // Create xml reader
        XMLReader xr = XMLReaderFactory.createXMLReader();
        xr.setContentHandler(handler);
        xr.setErrorHandler(handler);

        // Parse each file provided on the command line.
        for(int i=0;i<args.length;i++)
            xr.parse(new InputSource(new FileReader(args[i])));
    }
}
Avatar of harry_hendrata
harry_hendrata

could you provide sample XML and to-be data ?
Avatar of badour_ma

ASKER

<?xml version = '1.0' encoding = 'UTF-8'?>
   <catalog>
     <journal>
       <journal_title>Open:</journal_title>
       <publisher>Oracle Publishing</publisher>
       <edition>Sept-Oct 2003</edition>
       <article_section>XML</article_section>
       <title>4.44</title>
       <author>Julie Basu</author>
    </journal>
    <journal>
      <journal_title>Oracle Magazine</journal_title>
      <publisher>Oracle Publishing</publisher>
      <edition>Nov-Dec 2003</edition>
      <article_section>SQL</article_section>
      <title>The Active Database</title>
      <author> Cameron ORourke </author>
    </journal>
   </catalog>

the code will search all "XML"," oracle Magazine", "Java", "sql" data and if it found it will print the secound data elemnet
the output of the code:
Data in next tag after founding 'XML': 4.44
Data in next tag after founding 'Oracle Magazine': Oracle Publishing
Data in next tag after founding 'SQL': The Active Database
ASKER CERTIFIED SOLUTION
Avatar of harry_hendrata
harry_hendrata

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mayank S
If you're open to usage of already available tools, you can convert your XML to a Java object using something like Castor (www.castor.org) and store your object to a database using something like Hibernate or Toplink (www.hibernate.org)
hi mayankeagle,
thanks alot for helping me
I do not want to use the avaliable tools I wnat to do this from my java code
Then you have to read it into objects on your own and then write queries to insert their data members into a database