• Post Reply Bookmark Topic Watch Topic
  • New Topic

SAX parser problem

 
Rob MacKay
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • 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
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Henry Wong
author
Sheriff
Posts: 22514
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!