-2

I need some help understanding how to read the following XML:

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soap:Body>
      <GetRouteSummaryForStopResponse xmlns="http://octranspo.com">
         <GetRouteSummaryForStopResult>
            <StopNo xmlns="http://tempuri.org/">5212</StopNo>
            <StopDescription xmlns="http://tempuri.org/">BANNER / PARKMOUNT</StopDescription>
            <Error xmlns="http://tempuri.org/" />
            <Routes xmlns="http://tempuri.org/">
               <Route>
                  <RouteNo>82</RouteNo>
                  <DirectionID>1</DirectionID>
                  <Direction>Westbound</Direction>
                  <RouteHeading>Bayshore</RouteHeading>
               </Route>
               <Route>
                  <RouteNo>282</RouteNo>
                  <DirectionID>0</DirectionID>
                  <Direction>Inbound</Direction>
                  <RouteHeading>Mackenzie King</RouteHeading>
               </Route>
            </Routes>
         </GetRouteSummaryForStopResult>
      </GetRouteSummaryForStopResponse>
   </soap:Body>
</soap:Envelope>

Im trying to retrieve the text of <StopNo> and <StopDescription> and others after. To debug Im trying to print getName() and getText().

//XmlPullParser - START
        try {
            XmlPullParser parser = Xml.newPullParser();
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
            parser.setInput(in, null);

            while (parser.next() != XmlPullParser.END_DOCUMENT) {
                Log.i("**********", parser.getName() + " " + parser.getText());
                if (parser.getEventType() != XmlPullParser.START_TAG) {
                    continue;
                }
                //Get Tag name
                String name = parser.getName();
                // Starts by looking for the temperature
                if (name.equalsIgnoreCase("StopNo")) {
                    stopNumber = parser.getText();
                    Log.i("***** OS doInBackground", "stopNumber " + stopNumber);
                }
                //look for speed tag
                if (name.equals("StopDescription")) {
                    stopDescription = parser.getText();
                    Log.i("***** OS doInBackground", "stopDescription " + stopDescription);
                }
            }




        } catch (Exception e){
            Log.i("****** Exception XML", e.getMessage());
        }
        if (in!=null) {
            try {
                in.close();
            } catch (IOException e) {
            }
        }

However, I`m getting the following output. First it prints getName() opening tag, getText() = null, then null for tag and getText() = text and so on.

...
I/**********: StopNo null
I/***** OS doInBackground: stopNumber null
I/**********: null 5212
I/**********: StopNo null
I/**********: StopDescription null
I/***** OS doInBackground: stopDescription null
I/**********: null BANNER / PARKMOUNT
I/**********: StopDescription null
I/**********: Error null
I/**********: Error null
I/**********: Routes null
I/**********: Route null
I/**********: RouteNo null
I/**********: null 82
I/**********: RouteNo null
...

I cannot understand why the getName() and getText() are off?

Vishu
  • 326
  • 2
  • 11
Porco
  • 195
  • 1
  • 4
  • 16

1 Answers1

2

The text will be available when you encounter XmlPullParser.TEXT. You are reading the text in XmlPullParser.START_TAG

you can try this code

      while (parser.next() != XmlPullParser.END_DOCUMENT) {
            Log.i("**********", parser.getName() + " " + parser.getText());

            switch (parser.getEventType()){
                case XmlPullParser.START_TAG:
                    String name = parser.getName();
                    Log.i("***** OS doInBackground", "name " + name);
                    break;
                case XmlPullParser.TEXT :
                    String text = parser.getText();
                    Log.i("***** OS doInBackground", "text " + text);


            }
        }
Vishu
  • 326
  • 2
  • 11
  • Thanks! Following your code I did while(parser.next() != XmlPullParser.END_DOCUMENT){ if(parser.getEventType() == XmlPullParser.START_TAG){ if(parser.getName().equalsIgnoreCase("StopNo")){ Log.i("******StopNo", " StopNo " +parser.nextText()); } else if (parser.getName().equalsIgnoreCase("StopDescription")){ Log.i("******StopDescription", " StopDescription " +parser.nextText()); } } – Porco Mar 22 '18 at 04:21