-1
我試圖分析在機器人下面的XML,但我遇到了一個問題:解析XML的Android引發異常
<?xml version="1.0" encoding="utf-8"?>
<ServerInformation>
<ConnectionInfo>
<Command>ConnectToServer</Command>
</ConnectionInfo>
<Devices>
<Device>
<Hostname>Android1</Hostname>
<IP>127.0.0.1</IP>
<MAC>hello</MAC>
<TCPSocket>100</TCPSocket>
</Device>
<Device>
<Hostname>Android2</Hostname>
<IP>127.0.0.1</IP>
<MAC>helo1</MAC>
<TCPSocket>200</TCPSocket>
</Device>
</Devices>
</ServerInformation>
我並不需要找到在XML它的價值。我只是檢查主機名和IP,但當它到達MAC時,會引發異常。
下面是代碼我使用解析XML:
try
{
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(new StringReader(xmlWriter.returnXmlOutput()));
int eventType = xpp.getEventType();
String tagName = xpp.getName();
while (eventType != XmlPullParser.END_DOCUMENT)
{
tagName = xpp.getName();
if (tagName != null)
{
if (tagName.equalsIgnoreCase("Command"))
{
Log.d("Command", xpp.nextText());
}
else if (tagName.equalsIgnoreCase("Hostname"))
{
Log.d("Hostname", xpp.nextText());
}
else if (tagName.equalsIgnoreCase("IP"))
{
Log.d("IP", xpp.nextText());
}
}
xpp.nextTag();
}
}
catch (Exception ex)
{
Log.e("XML Exception", ex.toString());
}
下面是我得到異常:
06-18 18:43:55.168: E/XML Exception(2134): org.xmlpull.v1.XmlPullParserException: unexpected type (position:TEXT [email protected]:14 in [email protected])
感謝您的幫助,您可以提供!
你可以顯示XML文件嗎?你起牀的只有一個字符串。 – TyMarc
抱歉忘記格式化XML,因此被視爲HTML並隱藏標籤 – Boardy
只是一個提示,使用if(eventType == XmlPullParser.START_TAG){ – raybaybay