Solved

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

Posted on 2007-03-30
6
317 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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 21
varialbe initialization 11 30
JQuery to parse xml string and get element by id 4 22
MULTIPLE DATE QUERY 15 49
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that useā€¦
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

912 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

28 Experts available now in Live!

Get 1:1 Help Now