Solved

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

Posted on 2007-03-30
6
316 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 30

Expert Comment

by:mayankeagle
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:mayankeagle
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
The viewer will learn how to implement Singleton Design Pattern in Java.

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now