我的目标是从 url 下载 xml 文件并解析它。对于实验,我使用下一个 xml 文件: https ://www.dropbox.com/s/l5qtpcrryuistpk/Adtest.xml
<?xml version="1.0" encoding="UTF-8">
<adsettings>
<adnetwork>AdMob</adnetwork>
<publisherid>12345</publisherid>
</adsettings>
首先,我使用以下代码下载它:
URL xmlUrl = new URL(XML_SETTINGS_URL);
HttpURLConnection urlConnection = (HttpURLConnection) xmlUrl.openConnection();
urlConnection.connect();
FileOutputStream fileOutputStream = adContext.openFileOutput(SETTINGS_FILENAME,
adContext.MODE_PRIVATE);
InputStream stream = urlConnection.getInputStream();
byte[] buffer = new byte[1024];
while (stream.read(buffer) > -1) {
fileOutputStream.write(buffer);
}
fileOutputStream.close();
urlConnection.disconnect();
接下来,我正在尝试解析它:
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser parser = factory.newPullParser();
File file = adContext.getFileStreamPath(SETTINGS_FILENAME);
FileReader fileReader = new FileReader(file);
System.out.println(fileReader);
parser.setInput(fileReader);
int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if(eventType == XmlPullParser.START_DOCUMENT) {
Log.d(TAG, "Start document");
} else if(eventType == XmlPullParser.START_TAG) {
Log.d(TAG, "Start tag "+parser.getName());
} else if(eventType == XmlPullParser.END_TAG) {
Log.d(TAG, "End tag "+parser.getName());
} else if(eventType == XmlPullParser.TEXT) {
Log.d(TAG, "Text "+parser.getText());
}
eventType = parser.next();
}
结果,我只收到 Start 文档、html、head 和脚本,然后是异常:
org.xmlpull.v1.XmlPullParserException: unterminated entity ref (position:TEXT @13:51 in java.io.FileReader@4287eae8)
任何想法如何解决它?先感谢您