Solved

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

Posted on 2007-03-30
6
324 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Via a live example, show how to take different types of Oracle backups using RMAN.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
Suggested Courses

631 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