I want to use XML as a small database to store articles. While I am using SAXParser to parse this XML, I got an ArrayIndexOutOfBoundsException.
I make a small example for this. XML File:
<?xml version="1.0" encoding="UTF-8"?>
<a>
<b>
<c>
<![CDATA[
1111111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111111
111111111111111111111111111111111111111111
]]>
</c>
</b>
</a>
the exception I got:
java.lang.ArrayIndexOutOfBoundsException
at java.lang.System.arraycopy(Native Method)
at org.gjt.xpp.impl.tokenizer.Tokenizer.next(Tokenizer.java:1274)
at org.gjt.xpp.impl.pullparser.PullParser.next(PullParser.java:392)
at org.gjt.xpp.sax2.Driver.parseSubTree(Driver.java:415)
at org.gjt.xpp.sax2.Driver.parse(Driver.java:310)
at javax.xml.parsers.SAXParser.parse(SAXParser.java:392)
at javax.xml.parsers.SAXParser.parse(SAXParser.java:328)
at com.sumy.xmlwikimanager.dao.XMLUtil.parserXML(XMLUtil.java:28)
at com.sumy.xmlwikimanager.dao.XMLUtil.main(XMLUtil.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
a parser handler:
package com.sumy.xmlwikimanager.dao;
import com.sumy.xmlwikimanager.bean.WikiItem;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
* Created by Sumy on 2015/11/27 0027.
*/
public class DatabaseParserHandler extends DefaultHandler {
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
System.out.println("startElement: uri[" + uri + "] localName[" + localName + "] qName[" + qName + "]");
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
super.characters(ch, start, length);
System.out.println(length);
}
}
XMLUtil.java just have a test method:
package com.sumy.xmlwikimanager.dao;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.File;
import java.io.IOException;
/**
* Created by Sumy on 2015/11/27 0027.
*/
public class XMLUtil {
public static void parserXML(File file) {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
parser.parse(file, new DatabaseParserHandler());
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
parserXML(new File("Category.xml"));
}
}
I test:
If the length of CDATA content less than 1034, the program work fine. While I add some character, the ArrayIndexOutOfBoundsException will throw.
Is there anything wrong on my program?