XML Pull Parser Exception - XML fails to read

Monday, May 12, 2014

I have got to process data from a XML. This is the type of XML and I will read:



<Networks>
<Network>
<ID_REDE>1</ID_REDE>
<ID_BRIDGE>1</ID_BRIDGE>
<ID_MACRO_REDE>1</ID_MACRO_REDE>
<NOME>Sede</NOME>
<DESCRICAO>Rede teste</DESCRICAO>
<DATA_REGISTO>2014-04-14T14:17:21</DATA_REGISTO>
<ACTIVO>1</ACTIVO>
<NETID>2</NETID>
</Network>
<Network>
<ID_REDE>2</ID_REDE>
<ID_BRIDGE/>
<ID_MACRO_REDE/>
<NOME>TEMP(20140428|0953)</NOME>
<DESCRICAO/>
<DATA_REGISTO/>
<ACTIVO>0</ACTIVO>
<NETID>-1</NETID>
</Network>
<Network>
<ID_REDE>3</ID_REDE>
<ID_BRIDGE>2</ID_BRIDGE>
<ID_MACRO_REDE>2</ID_MACRO_REDE>
<NOME>VIA NORTE 1</NOME>
<DESCRICAO/>
<DATA_REGISTO>2014-04-28T21:24:27</DATA_REGISTO>
<ACTIVO>1</ACTIVO>
<NETID>100</NETID>
</Network>
</Networks>


And I want the ID_Rede, Nome and ID_Macro_Rede


I am using this to process the XML:



private void parseXML(XmlPullParser parser) throws XmlPullParserException,IOException
{
int eventType = parser.getEventType();
Network currentNetwork = null;

while (eventType != XmlPullParser.END_DOCUMENT){
String name = null;
switch (eventType){
case XmlPullParser.START_DOCUMENT:
networks = new ArrayList();
break;
case XmlPullParser.START_TAG:
name = parser.getName();
if (name.equals("Network")){
currentNetwork = new Network(null, null, 0);
Toast.makeText(getActivity().getApplicationContext(), "entrei no name == network: " + name, Toast.LENGTH_LONG).show();
} else if (currentNetwork != null){
Toast.makeText(getActivity().getApplicationContext(), "entro aqui ", Toast.LENGTH_LONG).show();
if (name.equals("ID_REDE")){
currentNetwork.setId(parser.nextText());
Toast.makeText(getActivity().getApplicationContext(), "entrei ID_Rede: " + parser.nextText(), Toast.LENGTH_LONG).show();
} else if (name.equals("ID_MACRO_REDE")){
currentNetwork.setMacro(Integer.parseInt(parser.nextText()));

} else if (name.equals("NOME")){
currentNetwork.setName(parser.nextText().toString());
Toast.makeText(getActivity().getApplicationContext(), "entrei no NOME: " + parser.nextText(), Toast.LENGTH_LONG).show();
}
}
break;
case XmlPullParser.END_TAG:
name = parser.getName();
if (name.equalsIgnoreCase("network") && currentNetwork != null){
networks.add(currentNetwork);
Toast.makeText(getActivity().getApplicationContext(), "size: " + networks.size(), Toast.LENGTH_LONG).show();
}
}
eventType = parser.next();
}
}


But I'm getting error in this line:



Toast.makeText(getActivity().getApplicationContext(), "entrei ID_Rede: " + parser.nextText(), Toast.LENGTH_LONG).show();


This is my logcat:



05-12 11:20:21.268 10512-10512/pt.smartgeo.aees W/System.err﹕ org.xmlpull.v1.XmlPullParserException: precondition: START_TAG (position:END_TAG </ID_REDE>@3:21 in java.io.InputStreamReader@41f9e2b0)
05-12 11:20:21.278 10512-10512/pt.smartgeo.aees W/System.err﹕ at org.kxml2.io.KXmlParser.nextText(KXmlParser.java:2063)
05-12 11:20:21.278 10512-10512/pt.smartgeo.aees W/System.err﹕ at pt.smartgeo.aees.NetworksList.parseXML(NetworksList.java:132)
05-12 11:20:21.278 10512-10512/pt.smartgeo.aees W/System.err﹕ at pt.smartgeo.aees.NetworksList.access$000(NetworksList.java:27)
05-12 11:20:21.278 10512-10512/pt.smartgeo.aees W/System.err﹕ at pt.smartgeo.aees.NetworksList$2.onClick(NetworksList.java:80)
05-12 11:20:21.278 10512-10512/pt.smartgeo.aees W/System.err﹕ at android.view.View.performClick(View.java:4438)
05-12 11:20:21.278 10512-10512/pt.smartgeo.aees W/System.err﹕ at android.view.View$PerformClick.run(View.java:18422)
05-12 11:20:21.278 10512-10512/pt.smartgeo.aees W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:733)
05-12 11:20:21.278 10512-10512/pt.smartgeo.aees W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
05-12 11:20:21.278 10512-10512/pt.smartgeo.aees W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
05-12 11:20:21.278 10512-10512/pt.smartgeo.aees W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5017)
05-12 11:20:21.278 10512-10512/pt.smartgeo.aees W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
05-12 11:20:21.278 10512-10512/pt.smartgeo.aees W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
05-12 11:20:21.278 10512-10512/pt.smartgeo.aees W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-12 11:20:21.278 10512-10512/pt.smartgeo.aees W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-12 11:20:21.278 10512-10512/pt.smartgeo.aees W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)






http://ift.tt/1olnP36