gpt4 book ai didi

java - Android Xml Pull Parser 尝试解析 xml 文件时发生异常

转载 作者:行者123 更新时间:2023-12-01 14:07:46 24 4
gpt4 key购买 nike

当我尝试使用 XML Pull 解析器解析 xml 文件时遇到异常。异常显示 XmlPullParserException: 意外的 token ( 位置: TEXT @ 1:2 in java.io.stringreader@40e06970请给我一个解决方案,我的代码是,

MainAvtivity.java

package com.example.simplexmlpullapp;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.ListView;
import android.widget.TextView;




public class MainActivity extends Activity {
ListView list;
String str;
ArrayList<Data> arrdata;
ArrayList<String> arrayofString;
String textforinput = "<foo>Hello World!</foo>";
String parsedData = "";


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
readXML();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

private void readXML() throws XmlPullParserException {

try {
Log.i("String","in readXML");
InputStream is = getAssets().open("countries.xml");
BufferedReader br=new BufferedReader(new InputStreamReader(is));
StringBuilder total=new StringBuilder();
String line;

while((line=br.readLine())!=null)
{
total.append(line);
}


Log.i("String","going in xml parsing"+total);
xmlParse(total.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


public void xmlParse(String file) throws XmlPullParserException, IOException
{
Log.i("String","going in xml parse");
boolean demoflag=false;
Data d=null;

XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();

xpp.setInput(new StringReader(file));
int eventType = xpp.getEventType();

while (eventType!=XmlPullParser.END_DOCUMENT) {
Log.i("String","in while");
switch(eventType)
{
case XmlPullParser.START_DOCUMENT:
break;
//System.out.println("Start document");

case XmlPullParser.START_TAG:
if(xpp.getName().equalsIgnoreCase("country"))
{
demoflag=true;
d=new Data();
}
break;
case XmlPullParser.TEXT:
if(demoflag)
{
d.setCountry(xpp.getText().trim());
Log.i("country", ""+xpp.getText().trim());
}
break;
case XmlPullParser.END_TAG:
if(xpp.getName().equalsIgnoreCase("country"))
{
arrdata.add(d);
demoflag=false;
}
break;
default :
break;
}

eventType=xpp.next();
}

arrayofString=new ArrayList<String>();
for(int i=0;i<arrdata.size();i++)
{
Data data=arrdata.get(i);
arrayofString.add(data.getCountry());
Log.i("parse_example", "country"+data.getCountry());
}

}




@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

国家/地区.xml

<CountriesList>

<countries>

<country>
Afghanistan
</country>
</countries>

<countries>

<country>
Albania
</country>
</countries>

<countries>

<country>
Algeria
</country>
</countries>

<countries>
</CountriesList>

最佳答案

国家列表标签未结束

</CountriesList>

您可以在此处验证 xml

http://www.xmlvalidation.com/

尝试这个 xml

<CountriesList>

<countries>

<country>
Afghanistan
</country>
</countries>

<countries>

<country>
Albania
</country>
</countries>

<countries>

<country>
Algeria
</country>
</countries>


</CountriesList>

更新这将在 logcat 中打印国家/地区

public void xmlParse(String file) throws XmlPullParserException, IOException {
Log.i("String", "going in xml parse");
boolean demoflag = false;
Data d = null;

XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();

xpp.setInput(new StringReader(file));
int eventType = xpp.getEventType();

while (eventType != XmlPullParser.END_DOCUMENT) {
Log.i("String", "in while");
switch (eventType) {
case XmlPullParser.START_DOCUMENT:
break;

case XmlPullParser.START_TAG:
if (xpp.getName().equalsIgnoreCase("country")) {
demoflag = true;
}
break;
case XmlPullParser.TEXT:
if (demoflag) {
Log.i("country", "" + xpp.getText().trim());
}
break;
case XmlPullParser.END_TAG:
if (xpp.getName().equalsIgnoreCase("country")) {
demoflag = false;
}
break;
default:
break;
}

eventType = xpp.next();
}

}

关于java - Android Xml Pull Parser 尝试解析 xml 文件时发生异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18761546/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com