473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Handling Multiple Elements within Record

1
Hi!

I have been working on parsing an XML file and have run into a problem. I am using Java SAX, and while parsing, I am feeding data to an oracle database.

So, here is my problem:

In the XML file I have, there are sometimes many of the same tags within a record, for example, one line of the xml might read:
Expand|Select|Wrap|Line Numbers
  1. <actor><stagename>Julia Roberts</stagename><relationtype><relationname>Hugh Grant</relationname></relationtype><dateofBirth/><relationtype><relationname>someotherguy</relationname>
  2. </relationtype></actor>
What I have been doing up to now is parsing the data such that if the qualified name is actor, then it prepares to insert a record into the database. The code utilizes SAX to get all the records parsed.
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2.  
  3. import java.sql.*;
  4.  
  5. import org.xml.sax.*;
  6. import org.xml.sax.helpers.DefaultHandler;
  7.  
  8. import javax.xml.parsers.SAXParserFactory;
  9. import javax.xml.parsers.ParserConfigurationException;
  10. import javax.xml.parsers.SAXParser;
  11.  
  12. public class Actors extends DefaultHandler
  13. {
  14.     static Connection conn;
  15.     static Statement insertActorStatement;
  16.  
  17.     public static void main(String argv[]) throws SQLException
  18.     {
  19.         DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
  20.         conn = DriverManager.getConnection
  21.         ("jdbc:oracle:thin:@oracle.csc.uvic.ca:1521:TEACH", "ASDF",*****");
  22.         insertActorStatement = conn.createStatement();
  23.  
  24.         // Use an instance of ourselves as the SAX event handler
  25.         DefaultHandler handler = new Actors();
  26.         // Use the default (non-validating) parser
  27.         SAXParserFactory factory = SAXParserFactory.newInstance();
  28.         try {
  29.             // Parse the input
  30.             SAXParser saxParser = factory.newSAXParser();
  31.             saxParser.parse( new File(argv[0]), handler);
  32.  
  33.         } catch (Throwable t) {
  34.             t.printStackTrace();
  35.         }
  36.         System.exit(0);
  37.     }
  38.  
  39.  
  40.     int count = 0;
  41.     String insertActorString = null;
  42.  
  43.     //===========================================================
  44.     // SAX DocumentHandler methods
  45.     //===========================================================
  46.  
  47.     public void startDocument() throws SAXException
  48.     {
  49.     }
  50.  
  51.     public void endDocument() throws SAXException
  52.     {  }
  53.  
  54.     boolean isActor = false;
  55.     boolean isStagename = false;
  56.     boolean isFamilyname = false;
  57.     boolean isDob = false;
  58.     boolean isStudio = false;
  59.     boolean isRelationships = false;
  60.     //    boolean isNotes = false;
  61.  
  62.  
  63.     public void startElement(String namespaceURI,
  64.                              String lName, // local name
  65.                              String qName, // qualified name
  66.                              Attributes attrs) throws SAXException
  67.     {
  68.  
  69.     if ( qName.equals("actor") ) {
  70.             this.isActor = true;
  71.             this.insertActorString =
  72.                     "INSERT INTO Actors(StageName, FamilyName, DateOfBirth, Studio, Relationships) " +
  73.                     "VALUES (stagenameval, familynameval, dobval, studioval, relationshipsval)";
  74.             System.out.print(++count + " ");
  75.         }
  76.         if ( qName.equals("stagename") ) this.isStagename = true;
  77.         if ( qName.equals("familyname") ) this.isFamilyname = true;
  78.         if ( qName.equals("dob") ) this.isDob = true;
  79.         if ( qName.equals("studio") ) this.isStudio = true;
  80.         if ( qName.equals("relationships") ) this.isRelationships= true;
  81.     //    if ( qName.equals("notes") ) this.isNotes = true;
  82.  
  83.     }
  84.  
  85.     public void endElement(String namespaceURI,
  86.                            String sName, // simple name
  87.                            String qName  // qualified name
  88.                           ) throws SAXException
  89.     {
  90.         if ( qName.equals("stagename") ) this.isStagename = false;
  91.         if ( qName.equals("familyname") ) this.isFamilyname = false;
  92.         if ( qName.equals("dob") ) this.isDob = false;
  93.         if ( qName.equals("studio") ) this.isStudio = false;
  94.         if ( qName.equals("relationships") ) this.isRelationships= false;
  95.     //        if ( qName.equals("notes") ) this.isNotes = false;
  96.  
  97.  
  98.         if ( qName.equals("actor") ) {
  99.  
  100.             //Replace any missing field value by NULL
  101.             this.insertActorString = this.insertActorString.replaceAll("stagenameval", "NULL");
  102.             this.insertActorString = this.insertActorString.replaceAll("familynameval", "NULL");
  103.             this.insertActorString = this.insertActorString.replaceAll("dobval", "NULL");
  104.             this.insertActorString = this.insertActorString.replaceAll("studioval", "NULL");
  105.             this.insertActorString = this.insertActorString.replaceAll("relationshipsval", "NULL");
  106.         //  this.insertActorString = this.insertActorString.replaceAll("notesval", "NULL");
  107.  
  108.             System.out.println(this.insertActorString);
  109.             try {
  110.                 insertActorStatement.executeUpdate(this.insertActorString);
  111.             }
  112.             catch (SQLException ex) {
  113.                 System.out.println("execution**************************: " + ex);
  114.                 System.exit(1);
  115.             }
  116.  
  117.         }
  118.     }
  119.  
  120.  
  121.     public void characters(char buf[], int offset, int len) throws SAXException
  122.     {
  123.         if ( this.isStagename == true ) {
  124.             String s = new String(buf, offset, len);
  125.             this.insertActorString = this.insertActorString.replaceAll("stagenameval",
  126.                                                             "'" + s.replaceAll("'","''") + "'");
  127.         }
  128.  
  129.         if ( this.isFamilyname == true ) {
  130.             String s = new String(buf, offset, len);
  131.             this.insertActorString = this.insertActorString.replaceAll("familynameval",
  132.                                             "'" + s.replaceAll("'", "''") + "'");
  133.         }
  134.  
  135.         if ( this.isDob == true ) {
  136.             String s = new String(buf, offset, len);
  137.             this.insertActorString = this.insertActorString.replaceAll("dobval",
  138.                                                                 "'" + s.trim() + "'");
  139.         }
  140.  
  141.         if ( this.isStudio == true ) {
  142.             String s = new String(buf, offset, len);
  143.             this.insertActorString = this.insertActorString.replaceAll("studioval",
  144.                                                                 "'" + s.replaceAll("'","''") + "'");
  145.         }
  146.  
  147.         if ( this.isRelationships == true ) {
  148.             String s = new String(buf, offset, len);
  149.             this.insertActorString = this.insertActorString.replaceAll("relationshipsval",
  150.                                             "'" + s.replaceAll("'", "''") + "'");
  151.         }
  152.     //        if ( this.isNotes == true ) {
  153.     //         String s = new String(buf, offset, len);
  154.     //         this.insertActorString = this.insertActorString.replaceAll("notesval",
  155.     //                                        "'" + s.replaceAll("'","''") + "'");
  156.         //}
  157.     }
  158.  
  159. } //end of class
  160.  

So what I would like to be able to do, is for situations where there is more than one relationship for an actor, I would like to insert a new Actors touple into my database for each one. The specific problem that I am having is that i'm not sure how I would insert the touple because the way my code is set up currently, it's only one touple per instance of actor.

If you could point me in the right direction I'd be grateful.
Thanks
Dan
Nov 26 '06 #1
0 1334

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Sans Spam | last post by:
Greetings! I have a table that contains all of the function permissions within a given application. These functions are different sections of a site and each has its own permissions (READ, WRITE,...
12
by: zig | last post by:
I've posted this on alt.comp.lang.coldfusion, but is predominantly a javascript problem: I have a CF query which returns several rows of records. I wanted to have a checkbox on each record of...
5
by: Frame | last post by:
I'm looking for tutorials or articles considering HTML Frames and how to handle them with Javascript. E.g. samples how Frames can exchange information, can a Frame instruct other Frame to update...
3
by: darrel | last post by:
My understanding is that using the FILE form element to allow a file upload is limited to one unique file per page. Is that correct? Any thoughts on how best to design an interface to allow...
2
by: dinkle | last post by:
Hi Y'all, I am pretty new to js and am hitting a few snags. I need to process a multiple select list and pass it onto a PHP script. I can only get the first value in the JS and have no idea how...
7
by: =?Utf-8?B?TG9zdEluTUQ=?= | last post by:
Hi All :) I'm converting VB6 using True DBGrid Pro 8.0 to VB2005 using DataGridView. True DBGrid has a MultipleLines property that controls whether individual records span multiple lines. Is...
5
by: Neil | last post by:
"lyle" <lyle.fairfield@gmail.comwrote in message news:48c3dde7-07bd-48b8-91c3-e157b703f92b@f3g2000hsg.googlegroups.com... Question for you. I'm doing something similar, only, instead of opening...
2
ADezii
by: ADezii | last post by:
The incentive for this Tip was an Article by the amazing Allen Browne - I considered it noteworthy enough to post as The Tip of the Week in this Access Forum. Original Article by Allen Browne ...
1
by: javediq143 | last post by:
Hi All, This is my first post in this forum. I'm developing a CMS for my latest website. This CMS is also in PhP & MySQL. I'm done with the ADD section where the Admin can INSERT new records in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.