I am using JAVA SAX parser to parse a XML file and write the contents to a text file.My program is working fine and contents are retrieved correctly when the XML is small. But when the XML file size is large(around 1.5 mb) few bytes are missing / split in contents randomly.
public void characters(char[] char, int start, int length) throws SAXException {
String str = new String(char, start, length);
}
How can I over come this issue?
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 32767
posted
0
From the javadocs of ContentHandler.characters:
SAX parsers may return all contiguous character data in a single chunk, or they may split it into several chunks;
Is that what you're talking about? If so, then your code needs to account for this possibility.
You mean we need to make changes to the characters function ???
Can you please help...It's very urgent.
Thanks in advance,
Mahesh
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 32767
posted
0
Yes. The one you have apparently assumes that all text data is returned in a single chunk (I can't tell for sure since you didn't post the full code of the method). You need to change it so that it still works if the text is returned in several chunks. One way to do that would be to append the text to a StringBuffer (which would be field in your handler class). Once the end element of whatever tag surrounds this text is reached, you can handle the text itself.
Mahesh Mamani
Ranch Hand
Joined: Jun 25, 2001
Posts: 110
posted
0
Below is the sample code which is being referred to...Actual code is based on this code itself....
We did a System.out.println in the characters function and there itself it prints out partial characters...
Hope this info is enough
Thanks again,
Mahesh
a) Create a Sax Parser and parse the xml
private void parseDocument() {
//get a factory
SAXParserFactory spf = SAXParserFactory.newInstance();
try {
//get a new instance of parser
SAXParser sp = spf.newSAXParser();
//parse the file and also register this class for call backs
sp.parse("employees.xml", this);