Solved

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

Posted on 2007-03-30
6
320 Views
Last Modified: 2013-12-19
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])));
    }
}
0
Comment
Question by:badour_ma
  • 2
  • 2
  • 2
6 Comments
 
LVL 3

Expert Comment

by:harry_hendrata
ID: 18822331
could you provide sample XML and to-be data ?
0
 

Author Comment

by:badour_ma
ID: 18823094
<?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
0
 
LVL 3

Accepted Solution

by:
harry_hendrata earned 500 total points
ID: 18823334
What you could do is to create an oracle connection before parsing the XML, and after printing the data, you could insert the information into the database, and then after parsing, you close the database connection.

As for how to do create connection, insert into db, and close connection, you may want to refer to the following link:

http://www.oracle.com/technology/sample_code/tech/java/codesnippet/jdbc/namedparam/UsingOracleNamedParameterAPIs.html

regards



0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 30

Expert Comment

by:Mayank S
ID: 18825656
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)
0
 

Author Comment

by:badour_ma
ID: 18828383
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
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18829116
Then you have to read it into objects on your own and then write queries to insert their data members into a database
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
servlet and mdb, jms error 1 32
Oracle Distributed Transaction Lock Error ORA-01591 8 52
Fill Null values 5 28
oracle date format checking 7 26
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question