• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

SAX parser problem

 
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I will try to explain the problem I am having when parsing with SAX. In my content handler I have the startElement method and in that method I have a check for a specific localName and then based on that name being found, I store the attributes for it in a vector. The idea being that each time that localName is encountered in the XML file, the vector has another element added to it, which is the attibutes found for that element.




Here is where it gets weird. If the element I am searching for is not the last element in the list, the attributes that I get are not correct. Let's say you have just two elements in your xml file:

<myElement name="thisName" data="myData"></myElement>
<newElement stuff="myStuff" otherStuff="myOtherStuff"></newElement>

If I use the code above and populate the vector based on finding "myElement", for some reason when I access the vector later I get the attributes from "newElement" instead.

I inserted debug code in to output the attributes in the if statement and it would display the correct attributes at that time but the vector would always be populated with the attributes from whatever the final element in the xml file was.

Does anyone know why this would happen and how I can prevent that from happening? Any other possible solution would be nice as well. I am basically trying to write a class that uses SAX to parse a given xml file but only return a vector of attributes from a specifically named element in the XML file, ignoring other elements.

Thanks in advance,

Rob
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob, welcome to JavaRanch.

On your way in you may have missed that we have a policy on screen names here at JavaRanch. Basically, it must consist of a first name, a space, and a last name, and not be obviously fictitious. Since yours does not conform with it, please take a moment to change it, which you can do right here.

As to your problem, I'm fairly certain that there is something the matter with the logic in the code, since SAX itself is time-tested and proven. Can you post a minimal code section and XML document that exhibits the problem?
 
Rob MacKay
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ulf.

I have a class called ParseXMLFile.

The ParseXMLFile class has three member variables:

public static Vector elementVector = new Vector();
public static String fileToParse;
public static String searchElement;

there is also a method called:

public static Vector parseFileForElement(String filename, String element)

This method is called from some external class that needs the information from the XML file.

The method sets the two class member variables and instantiates the parser, setting up the content handler and the error handler.

The startElement method of the content handler simply checks to see if localName is the element we are looking for:

if (localName.equals(ParseXMLFile.searchElement))
{
ParseXMLFile.elementVector.addElement(atts);
}

If it is correct, then it will add the elements to the vector. Once the parsing is complete, the parseFileForElement method will return the Vector back to the class that called it.

As I mentioned, I did insert some general debug code to check that the search element was correctly encountered and that the attributes matched what they should have been in the xml file. However, the vector is getting bad data and it only has the attributes which come from whatever the last element of the XML file contains, regardless of the name of that element.

I hope my explanation makes sense.

Rob
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question is probably better answered in the XML forum, where all the XML experts hang out. Moving this topic to the XML forums...

Henry
 
reply
    Bookmark Topic Watch Topic
  • New Topic