Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: SAX Parser will not add object to arrayList

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default SAX Parser will not add object to arrayList

    Hello, I'm trying to parse through XML and while I'm able to parse it, it will not add the final object to the arraylist. The if statement is still accessed though as I've tested it with println's, so the issue is pinpointed to the object not being able to be added to the arraylist. Any help with this would be much appreciated! Thanks in advance!
    import java.awt.List;
    import java.io.*;
    import java.util.ArrayList;
    import java.util.Iterator;
     
    import org.xml.sax.*;
    import org.xml.sax.helpers.*;
     
    	public class SimpleSAXParser3 extends DefaultHandler
    	{
    		ArrayList<CustomOverlay> customOverlays = new ArrayList<CustomOverlay>();
    		boolean registry = false;
    		boolean businessBool   = false;
    		boolean longBool = false;
    		boolean streetBool = false;
    		String businessName = null;
    		String longitude = null;
    		String street1 = null;
     
     
    		public void startElement(String nsURI, String strippedName,
    						String tagName, Attributes attributes) throws SAXException
    		{
    				//move down the tree
    					  if (tagName.equalsIgnoreCase("business-name"))
    					  {
    						  businessBool = true;
    						//  System.out.println("tag found");
     
    					  }
    					  else if (tagName.equalsIgnoreCase("longitude"))
    					  {
     
    						  longBool = true;
    						//  System.out.println("tag found");
     
    					  }
    					  else if (tagName.equalsIgnoreCase("street1"))
    					  {
    						 streetBool = true;
     
    					  }
    					//}
    		}
     
    		public void characters(char[] ch, int start, int length)
    		{
    			if (businessBool)
    			{
    				System.out.println(new String(ch,start,length));
    				businessName =  new String(ch, start, length);
    				businessBool = false;
    			}
    			else if (longBool)
    			{
    				System.out.println(new String(ch,start,length));
    				 longitude = new String(ch, start,length);
    				longBool = false;
    			}
    			else if (streetBool)
    			{
    				System.out.println(new String(ch,start,length));
    				street1 = new String(ch,start,length);
    				streetBool = false;
    			}
     
    		//	addElement();
     
    			[B]// the following if statement is accessed[/B]
    			if ((businessName != null) && (longitude != null) && (street1 != null))
    					{
    				addElement(); //is properly run
    					}
     
    		}
     
     
     
    		public void addElement ()
    		{
    			//neither of these is added according to my toString1() method
    				customOverlays.add(new CustomOverlay(businessName,longitude,street1));
    				customOverlays.add(new CustomOverlay("string","String","String"));
     
     
    				businessName = null;
    				longitude = null;
    				street1 = null;
     
     
     
    		}
     
     
    		public void toString1()
    		{
     
     
     
    			if (customOverlays.size() > 0)
    			{
    				System.out.print("somethings in there");
    			}
    			else System.out.print("nothings there");
     
    		}
     
    	public void list() throws Exception
    	{
    		XMLReader parser = XMLReaderFactory.createXMLReader();
    		parser.setContentHandler(new SimpleSAXParser3());
    		parser.parse("RegistryXML.xml");
    	}
     
    //fixed main, problem still exists
    	public static void main(String[] args) throws Exception
    	{
     
    		SimpleSAXParser3 simp = new SimpleSAXParser3();
    		simp.list();
    		simp.toString1();
     
    	}
     
     
    }
    Last edited by michaelz; July 1st, 2010 at 01:11 PM.


  2. #2
    Member Charlie's Avatar
    Join Date
    Jun 2010
    Location
    Sweden
    Posts
    41
    Thanks
    1
    Thanked 5 Times in 5 Posts

    Default Re: SAX Parser will not add object to arrayList

    Youre only creating new elements for the customOverlays and putting them into the arraylist. I know the Hashtable method works like this, since it works by storing pointers in another way than arraylist does. I dont know if the arraylist stops the garbagecollectors like the Hashtable does.

    I sure as hell dont know if it doesnt either *noob*, but try with a hashtable would be my tip.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: SAX Parser will not add object to arrayList

    The problem lies in your main
    	public static void main(String[] args) throws Exception
    	{
     
    		new SimpleSAXParser3().list();
    		new SimpleSAXParser3().toString1();
     
    	}

    You first create a SimpleSAXParser3 and do the parsing, you then print out the value of a NEW parser rather than the one you just parsed. You should rather keep a reference to the first, then call toString1 on that object.

  4. #4
    Junior Member
    Join Date
    Jul 2010
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: SAX Parser will not add object to arrayList

    Even after I do this, I still get "nothings there" printed out.
    public static void main(String[] args) throws Exception
    	{
     
    		SimpleSAXParser3 simp = new SimpleSAXParser3();
    		simp.list();
    		simp.toString1();
     
    	}

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: SAX Parser will not add object to arrayList

    // the following if statement is accessed
    if ((businessName != null) && (longitude != null) && (street1 != null))
    {
    addElement(); //is properly run
    }
    Is the addElement() method called?

  6. #6
    Junior Member
    Join Date
    Jul 2010
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: SAX Parser will not add object to arrayList

    Yes, the if statement is executed and addElement() is called (i tested it by putting a println in the addElement() method and it printed).

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: SAX Parser will not add object to arrayList

    Why do you create another object here?
    parser.setContentHandler(new SimpleSAXParser3());
    Take out the following from the main() method:
    // simp.list();
    I've tested it with println's
    I think you missed a spot with your println()s. Did you put one in the Constructor?
    If you did you would have seen < 1 call!

  8. The Following User Says Thank You to Norm For This Useful Post:

    michaelz (July 1st, 2010)

  9. #8
    Junior Member
    Join Date
    Jul 2010
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: SAX Parser will not add object to arrayList

    Quote Originally Posted by Norm View Post
    Why do you create another object here?

    Take out the following from the main() method:
    // simp.list();

    I think you missed a spot with your println()s. Did you put one in the Constructor?
    If you did you would have seen < 1 call!
    Thanks!! I fixed the problem. basically in the list method I put a parameter of type SimpleSAXParser3 sax

    public void list(SimpleSAXParser3 sax) throws Exception
    	{
    		XMLReader parser = XMLReaderFactory.createXMLReader();
    		parser.setContentHandler(sax);
    		parser.parse("RegistryXML.xml");
    	}
     
     
    	public static void main(String[] args) throws Exception
    	{
     
    		SimpleSAXParser3 simp = new SimpleSAXParser3();
    		simp.list(simp);
    		simp.toString1();
     
    	}
    Last edited by michaelz; July 1st, 2010 at 03:09 PM.

Similar Threads

  1. XML DOM Parser problem
    By kanishktew in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 10th, 2010, 09:42 PM
  2. recursive descent parser
    By gammaman in forum Algorithms & Recursion
    Replies: 4
    Last Post: March 21st, 2010, 03:50 AM
  3. where can i get a Dom Parser jar ?
    By chinni in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: November 26th, 2009, 03:41 AM
  4. Edit ArrayList Object
    By frankycool in forum Collections and Generics
    Replies: 12
    Last Post: November 16th, 2009, 12:31 AM
  5. Arraylist or Arraylist Object?
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: September 11th, 2009, 02:08 AM