Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

XML SaxParser problem

 
Jayesh Netravali
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have an odd problem. I am using a SAX parser to parse an XML file which has a root and a number of repeating elements in the root. For eg. It is like representing a Database table in the XML format. The format is
<table-name>
<row>
<column1>value1</column1> <column2>value2</column2>...
</row>
<row>
.....
</row>
</table-name>

I parse this XML using the javax.xml.parsers.SAXParser. My parser class extends the DefaultHandler. I have to display this info in a JTable. Hence I make a Vector of Vectors by reading the String by overriding the characters() method of the DefaultHandler

The problem is after some 90-95 <row> tags the characters() method returns me the value of the text content for a tag in two parts. i.e for eg for the 91st row if the tag is <column1>This is-a test message</column1> it returns me 'This is' and '-a test message' as two Strings. Thus one String is stored in my vector in two parts, which screws up my table display.

If I delete one row from the xml file then the problem shifts to the next row in the file. I dont know if it some problem with the capacity of a vector?

Any pointers to the solun of the above problem would be helpful.

Thanks in advance,
Jayesh.
 
Rovas Kram
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you overrode the 'characters' method, you should know what values are being passed to. Is it being called twice for the String in question?
[ August 23, 2004: Message edited by: Rovas Kram ]
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rovas is exactly right. The characters method may be called ANY NUMBER of times for a given text field. You must provide for accumulating characters (say in a StringBuffer) until the endElement event.
The reason is that the parser reads in buffer-loads so if your text field is not complete in a buffer-load, the call to characters will have only the characters in that buffer.
Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic