• 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 Return Values

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm struggling with a way to return a hashmap of parsed XML to a main program.
The issue may not be SAXParser problem specifically, but it is related. A pointer or two would be welcome.

I am receiving an XML payload in a POST variable. To parse that variable I call the function passing it the XML payload and expecting back a HashMap. If I inspect the hashmap on return it is blank. The only way to return a value is to make the temporary hashmap in the function a static variable inside the class. That has potentially bad side effects I believe.



In some test I've found that xmlPayload behaves correctly until the parser finishes (endDocument). At that point, it appears that a new (and empty) xmlPayload is created (unless I use the static keyword). If I use the static keyword though, the returned hashmap contains information from the previous XML (bad!).

I've tried this with two different SAX parsers and so I believe I'm running into a problem with more than SAX. Any suggestions on how to accomplish my goal are more than welcome.

-- Dan
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, xmlPayload should probably not be static.

One oddity I observe that the code creates a new xmlParser object in line 8. But since the myParser method is not static, there must be an object of that type around already - instead of "xp", use "this".

If you're using the same xmlParse object to parse multiple times, simply clear out the xmlPayload object at the beginning of the myParser method (xmlPayload.clear()).

As to what else is happening, that's impossible to say without seeing the actual callback methods.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unrelated to your problem (I think) but may I suggest you avoid converting a String to an array of bytes using a possibly-incorrect encoding and then passing that possibly-malformed array of bytes to your parser? Do this instead:

Not only is this more reliable but it also saves the time and space you use to create the array of bytes.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic