问题
I am trying to parse an XML file using the DOM parser. The DocumentBuilder.parse() function throws a StringIndexOutOfBoundsException.
public class ParseCountryInfo {
public static ArrayList<CountryInfo> getCountryInfo(Context context) {
ArrayList<CountryInfo> countryInfoList = new ArrayList<CountryInfo>();
try {
InputStream fXmlFile = context.getResources().openRawResource(R.raw.country_data);
InputSource is = new InputSource(fXmlFile);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(is);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("country");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
countryInfoList.add(new CountryInfo(eElement.getAttribute("name"),
eElement.getAttribute("capital"), eElement.getAttribute("population")));
}
}
fXmlFile.close();
} catch (Exception e) {
e.printStackTrace();
}
return countryInfoList;
}
Error is encountered at the following line: Document doc = dBuilder.parse(is)
I have also tried: Document doc = dBuilder.parse(fXmlFile) which gives the same error.
Detailed Log:
02-25 01:13:36.326: W/System.err(19634): java.lang.StringIndexOutOfBoundsException: length=34; index=34
02-25 01:13:36.334: W/System.err(19634): at org.kxml2.io.KXmlParser.setInput(KXmlParser.java:1680)
02-25 01:13:36.334: W/System.err(19634): at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:111)
02-25 01:13:36.350: W/System.err(19634): at com.example.worldlymobile.ParseCountryInfo.getCountryInfo(ParseCountryInfo.java:28)
02-25 01:13:36.350: W/System.err(19634): at com.example.worldlymobile.DbHelper.onCreate(DbHelper.java:43)
02-25 01:13:36.350: W/System.err(19634): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
02-25 01:13:36.350: W/System.err(19634): at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
02-25 01:13:36.350: W/System.err(19634): at com.example.worldlymobile.CountryInfoActivity.initCountryInfoIfNeeded(CountryInfoActivity.java:48)
02-25 01:13:36.358: W/System.err(19634): at com.example.worldlymobile.CountryInfoActivity.onResume(CountryInfoActivity.java:37)
02-25 01:13:36.358: W/System.err(19634): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1187)
02-25 01:13:36.358: W/System.err(19634): at android.app.Activity.performResume(Activity.java:5318)
02-25 01:13:36.365: W/System.err(19634): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2595)
02-25 01:13:36.365: W/System.err(19634): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2633)
02-25 01:13:36.373: W/System.err(19634): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2100)
02-25 01:13:36.373: W/System.err(19634): at android.app.ActivityThread.access$600(ActivityThread.java:135)
02-25 01:13:36.373: W/System.err(19634): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
02-25 01:13:36.389: W/System.err(19634): at android.os.Handler.dispatchMessage(Handler.java:99)
02-25 01:13:36.389: W/System.err(19634): at android.os.Looper.loop(Looper.java:137)
02-25 01:13:36.389: W/System.err(19634): at android.app.ActivityThread.main(ActivityThread.java:4849)
02-25 01:13:36.389: W/System.err(19634): at java.lang.reflect.Method.invokeNative(Native Method)
02-25 01:13:36.389: W/System.err(19634): at java.lang.reflect.Method.invoke(Method.java:511)
02-25 01:13:36.397: W/System.err(19634): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
02-25 01:13:36.397: W/System.err(19634): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
02-25 01:13:36.397: W/System.err(19634): at dalvik.system.NativeStart.main(Native Method)
I am not sure if the XML file itself is incorrect. The contents of file are as follows:
<?xml version="1.0" encoding="UTF-8"?>
<data>
<country>
<name>Algeria</name>
<capital>Algiers</capital>
<population>36000000</population>
</country>
<country>
<name>Angola</name>
<capital>Luanda</capital>
<population>19000000</population>
</country>
<country>
<name>Benin</name>
<capital>Porto-Novo</capital>
<population>9800000</population>
</country>
<country>
<name>Botswana</name>
<capital>Gaborone</capital>
<population>1800000</population>
</country>
<country>
<name>Burkina Faso</name>
<capital>Ouagadougou</capital>
<population>16200000</population>
</country>
<country>
<name>Burundi</name>
<capital>Bujumbura</capital>
<population>8500000</population>
</country>
<country>
<name>Cameroon</name>
<capital>Yaoundé</capital>
<population>20000000</population>
</country>
<country>
<name>Cape Verde</name>
<capital>Praia</capital>
<population>500000</population>
</country>
<country>
<name>Central African Republic</name>
<capital>Bangui</capital>
<population>4800000</population>
</country>
<country>
<name>Chad</name>
<capital>N'Djamena</capital>
<population>11500000</population>
</country>
<country>
<name>Comoros</name>
<capital>Moroni</capital>
<population>727000</population>
</country>
<country>
<name>Republic of Congo</name>
<capital>Brazzaville</capital>
<population>67800000</population>
</country>
<country>
<name>Democratic Republic of the Congo</name>
<capital>Kinshasa</capital>
<population>3900000</population>
</country>
<country>
<name>Cote d'Ivoire</name>
<capital>Yamoussoukro</capital>
<population>22000000</population>
</country>
<country>
<name>Djibouti</name>
<capital>Djibouti</capital>
<population>900000</population>
</country>
<country>
<name>Egypt</name>
<capital>Cairo</capital>
<population>80400000</population>
</country>
<country>
<name>Equitorial Guinea</name>
<capital>Malabo</capital>
<population>700000</population>
</country>
<country>
<name>Eritrea</name>
<capital>Asmara</capital>
<population>5200000</population>
</country>
<country>
<name>Ethiopia</name>
<capital>Addis Ababa</capital>
<population>85000000</population>
</country>
<country>
<name>Gabon</name>
<capital>Libreville</capital>
<population>1500000</population>
</country>
<country>
<name>The Gambia</name>
<capital>Banjul</capital>
<population>1800000</population>
</country>
<country>
<name>Ghana</name>
<capital>Accra</capital>
<population>24000000</population>
</country>
<country>
<name>Guinea</name>
<capital>Conakry</capital>
<population>10800000</population>
</country>
<country>
<name>Guinea-Bissau</name>
<capital>Bissau</capital>
<population>1600000</population>
</country>
<country>
<name>Kenya</name>
<capital>Nairobi</capital>
<population>40000000</population>
</country>
<country>
<name>Lesotho</name>
<capital>Maseru</capital>
<population>1900000</population>
</country>
<country>
<name>Liberia</name>
<capital>Monrovia</capital>
<population>4100000</population>
</country>
<country>
<name>Libya</name>
<capital>Tripoli</capital>
<population>6500000</population>
</country>
<country>
<name>Madagascar</name>
<capital>Antananarivo</capital>
<population>20100000</population>
</country>
<country>
<name>Malawi</name>
<capital>Lilongwe</capital>
<population>15400000</population>
</country>
<country>
<name>Mali</name>
<capital>Bamako</capital>
<population>15200000</population>
</country>
<country>
<name>Mauritania</name>
<capital>Nouakchott</capital>
<population>3400000</population>
</country>
<country>
<name>Mauritius</name>
<capital>Port Louis</capital>
<population>1300000</population>
</country>
<country>
<name>Morocco</name>
<capital>Rabat</capital>
<population>31900000</population>
</country>
<country>
<name>Mozambique</name>
<capital>Maputo</capital>
<population>23400000</population>
</country>
<country>
<name>Namibia</name>
<capital>Windhoek</capital>
<population>2200000</population>
</country>
<country>
<name>Niger</name>
<capital>Niamey</capital>
<population>15900000</population>
</country>
<country>
<name>Nigeria</name>
<capital>Abuja</capital>
<population>158300000</population>
</country>
<country>
<name>Réunion</name>
<capital>Saint-Denis</capital>
<population>800000</population>
</country>
<country>
<name>Rwanda</name>
<capital>Kigali</capital>
<population>10400000</population>
</country>
<country>
<name>Saint Helena</name>
<capital>Jamestown</capital>
<population>6000</population>
</country>
<country>
<name>São Tomé and Príncipe</name>
<capital>São Tomé</capital>
<population>200000</population>
</country>
<country>
<name>Senegal</name>
<capital>Dakar</capital>
<population>12500000</population>
</country>
<country>
<name>Seychelles</name>
<capital>Victoria</capital>
<population>100000</population>
</country>
<country>
<name>Sierra Leone</name>
<capital>Freetown</capital>
<population>5800000</population>
</country>
<country>
<name>Somalia</name>
<capital>Mogadishu</capital>
<population>9400000</population>
</country>
<country>
<name>South Africa</name>
<capital>Pretoria</capital>
<population>49900000</population>
</country>
<country>
<name>South Sudan</name>
<capital>Juba</capital>
<population>9000000</population>
</country>
<country>
<name>Sudan</name>
<capital>Khartoum</capital>
<population>36000000</population>
</country>
<country>
<name>Swaziland</name>
<capital>Mbabane </capital>
<population>1200000</population>
</country>
<country>
<name>Tanzania </name>
<capital>Dodoma</capital>
<population>45000000</population>
</country>
<country>
<name>Togo</name>
<capital>Lomé</capital>
<population>6800000</population>
</country>
<country>
<name>Tunisia</name>
<capital>Tunis</capital>
<population>10500000</population>
</country>
<country>
<name>Uganda</name>
<capital>Kampala</capital>
<population>33800000</population>
</country>
<country>
<name>Western Sahara</name>
<capital>El Aaiún</capital>
<population>500000</population>
</country>
<country>
<name>Zambia</name>
<capital>Lusaka</capital>
<population>13300000</population>
</country>
<country>
<name>Zimbabwe</name>
<capital>Harare</capital>
<population>12600000</population>
</country>
</data>
回答1:
Change this condition...
for (int temp = 0; temp < nList.getLength(); temp++) {
to....
for (int temp = 0; temp < nList.getLength()-1; temp++) {
The problem is that...you are trying to access an index of string which doesn't exist.
A String's index starts with 0...So, its last index must be less than it length by 1. That's why when you try to get the last index of a string you will get it using string.getLength()-1 as follows...
int last_index = string.getLength()-1;
回答2:
Try this..
Document doc = dBuilder.parse(fXmlFile);
Instead of this..
Document doc = dBuilder.parse(is);
回答3:
I just tried with 5 elements in the XML and the code above worked flawlessly! Since DOM parsers load the entire XML in memory, I believe it was actually an OutOfMemory error. Strange that I got an StringIndexOutOfBounds Exception. Anyway, I used a SAX parser and things worked fine. I am sharing the code for the same here.
SAX Handler:
public class SAXXMLHandler extends DefaultHandler {
private final ArrayList<CountryInfo> countryInfoList;
private String tempVal;
private CountryInfo countryInfo;
public SAXXMLHandler() {
countryInfoList = new ArrayList<CountryInfo>();
}
public ArrayList<CountryInfo> getCountryInfoList() {
return countryInfoList;
}
// Event Handlers
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
// reset
tempVal = "";
if (qName.equalsIgnoreCase("country")) {
// create a new instance of employee
countryInfo = new CountryInfo();
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
tempVal = new String(ch, start, length);
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equalsIgnoreCase("country")) {
// add it to the list
countryInfoList.add(countryInfo);
} else if (qName.equalsIgnoreCase("name")) {
countryInfo.setCountryName(tempVal);
} else if (qName.equalsIgnoreCase("capital")) {
countryInfo.setCountryCapital(tempVal);
} else if (qName.equalsIgnoreCase("population")) {
countryInfo.setCountryPopulation(tempVal);
;
}
}
}
Parsing:
ArrayList<CountryInfo> countryInfoList = new ArrayList<CountryInfo>();
try {
// create a XMLReader from SAXParser
XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
// create a SAXXMLHandler
SAXXMLHandler saxHandler = new SAXXMLHandler();
// store handler in XMLReader
xmlReader.setContentHandler(saxHandler);
InputStream fXmlFile = context.getResources().openRawResource(R.raw.country_data);
// the process starts
xmlReader.parse(new InputSource(fXmlFile));
// get the `Employee list`
countryInfoList = saxHandler.getCountryInfoList();
} catch (Exception ex) {
Log.d("XML", "SAXXMLParser: parse() failed");
}
Hope this is helpful for anyone facing similar issue.
来源:https://stackoverflow.com/questions/22006333/encountered-stringindexoutofboundsexception-while-parsing-xml-file-in-android